use of org.apache.karaf.shell.impl.action.command.ActionCommand in project karaf by apache.
the class ParsingTest method testCommandLineParserMultiLine.
@Test
public void testCommandLineParserMultiLine() {
SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
sessionFactory.getRegistry().register(new CustomParser());
Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
String parsed = CommandLineParser.parse(session, "echo a\necho b");
assertEquals("echo a\necho b", parsed);
}
use of org.apache.karaf.shell.impl.action.command.ActionCommand in project karaf by apache.
the class ParsingTest method testCommandLineParser.
@Test
public void testCommandLineParser() {
SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
sessionFactory.getRegistry().register(new CustomParser());
Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
String parsed = CommandLineParser.parse(session, " foo bar (a + b); another command with spaces ");
assertEquals("foo bar (a + b) ; another \"command with spaces\"", parsed);
}
use of org.apache.karaf.shell.impl.action.command.ActionCommand in project karaf by apache.
the class ShellHelpProvider method getHelp.
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("shell|")) {
path = path.substring("shell|".length());
} else {
return null;
}
}
// Retrieve matching commands
Set<Command> commands = getCommands(session, path);
// Compute the scopes and matching bundles
Set<Bundle> bundles = new HashSet<>();
Set<String> scopes = new HashSet<>();
for (Command command : commands) {
if (command instanceof ActionCommand) {
Class<? extends Action> action = ((ActionCommand) command).getActionClass();
bundles.add(FrameworkUtil.getBundle(action));
}
scopes.add(command.getScope());
}
// use that one instead
if (scopes.size() == 1 && bundles.size() == 1 && path.equals(scopes.iterator().next())) {
Bundle bundle = bundles.iterator().next();
URL resource = bundle.getResource("OSGI-INF/shell-" + path + ".info");
if (resource != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()))) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
int maxSize = 80;
Terminal terminal = session.getTerminal();
if (terminal != null) {
maxSize = terminal.getWidth();
}
WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
WikiParser parser = new WikiParser(visitor);
parser.parse(reader);
return baos.toString();
} catch (IOException e) {
// ignore
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
printShellHelp(session, new PrintStream(baos), path);
return baos.toString();
}
return null;
}
Aggregations