Search in sources :

Example 1 with ActionCommand

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);
}
Also used : PrintStream(java.io.PrintStream) HeadlessSessionImpl(org.apache.karaf.shell.impl.console.HeadlessSessionImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 2 with ActionCommand

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);
}
Also used : PrintStream(java.io.PrintStream) HeadlessSessionImpl(org.apache.karaf.shell.impl.console.HeadlessSessionImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Example 3 with ActionCommand

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;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) Command(org.apache.karaf.shell.api.console.Command) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)2 Session (org.apache.karaf.shell.api.console.Session)2 ManagerImpl (org.apache.karaf.shell.impl.action.command.ManagerImpl)2 HeadlessSessionImpl (org.apache.karaf.shell.impl.console.HeadlessSessionImpl)2 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Command (org.apache.karaf.shell.api.console.Command)1 Terminal (org.apache.karaf.shell.api.console.Terminal)1 AnsiPrintingWikiVisitor (org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor)1 WikiParser (org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser)1 WikiVisitor (org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor)1