Search in sources :

Example 1 with Args

use of org.dcache.util.Args in project dcache by dCache.

the class ShellApplication method execute.

/**
 * Executes a single command with the output being printed to the console.
 */
public void execute(Args args) throws Throwable {
    if (args.argc() == 0) {
        return;
    }
    String out;
    try {
        if (isAnsiSupported && args.argc() > 0) {
            if (args.argv(0).equals("help")) {
                args.shift();
                args = new Args("help -format=" + HelpFormat.ANSI + " " + args.toString());
            }
        }
        try {
            out = Objects.toString(commandInterpreter.command(args), null);
        } catch (CommandThrowableException e) {
            throw e.getCause();
        }
    } catch (CommandSyntaxException e) {
        Ansi sb = Ansi.ansi();
        sb.fg(RED).a("Syntax error: " + e.getMessage() + "\n").reset();
        String help = e.getHelpText();
        if (help != null) {
            sb.a(help);
        }
        out = sb.toString();
    } catch (CommandExitException e) {
        throw e;
    } catch (CommandPanicException e) {
        Ansi sb = Ansi.ansi();
        sb.fg(RED).a("Bug detected! ").reset().a("Please email the following details to <support@dcache.org>:\n");
        Throwable t = e.getCause() == null ? e : e.getCause();
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        out = sb.a(sw.toString()).toString();
    } catch (Exception e) {
        out = Ansi.ansi().fg(RED).a(e.getMessage()).reset().toString();
    }
    if (!isNullOrEmpty(out)) {
        console.print(out);
        if (out.charAt(out.length() - 1) != '\n') {
            console.println();
        }
    }
    console.flush();
}
Also used : CommandThrowableException(dmg.util.CommandThrowableException) Args(org.dcache.util.Args) StringWriter(java.io.StringWriter) Ansi(org.fusesource.jansi.Ansi) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandExitException(dmg.util.CommandExitException) CommandThrowableException(dmg.util.CommandThrowableException) CommandExitException(dmg.util.CommandExitException) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandException(dmg.util.CommandException) IOException(java.io.IOException) CommandPanicException(dmg.util.CommandPanicException) CommandPanicException(dmg.util.CommandPanicException) PrintWriter(java.io.PrintWriter)

Example 2 with Args

use of org.dcache.util.Args in project dcache by dCache.

the class ShellApplication method console.

/**
 * Start an interactive session.  The user is supplied a prompt and their input is executed as a
 * command.  This repeats until they indicate that they wish to exit the session.
 */
public void console() throws Throwable {
    onInteractiveStart();
    try {
        while (true) {
            String prompt = Ansi.ansi().bold().a(getPrompt()).boldOff().toString();
            String str = console.readLine(prompt);
            if (str == null) {
                console.println();
                break;
            }
            execute(new Args(str));
        }
    } catch (CommandExitException ignored) {
    }
}
Also used : Args(org.dcache.util.Args) CommandExitException(dmg.util.CommandExitException)

Example 3 with Args

use of org.dcache.util.Args in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd.

@Test
public void shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = -2)
            int argument;

            @Override
            public String call() {
                assertThat(argument, is(1));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("1 2"));
}
Also used : Args(org.dcache.util.Args) Argument(dmg.util.command.Argument) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 4 with Args

use of org.dcache.util.Args in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldEnforceRequiredOptions.

@Test(expected = CommandSyntaxException.class)
public void shouldEnforceRequiredOptions() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Option(name = "foo", required = true)
            int bar;

            @Override
            public String call() {
                assertThat(bar, is(2));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args(""));
}
Also used : Args(org.dcache.util.Args) Option(dmg.util.command.Option) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 5 with Args

use of org.dcache.util.Args in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldAcceptCommonTypes.

@Test
public void shouldAcceptCommonTypes() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = 0)
            Integer arg0;

            @Argument(index = 1)
            int arg1;

            @Argument(index = 2)
            Short arg2;

            @Argument(index = 3)
            short arg3;

            @Argument(index = 4)
            Long arg4;

            @Argument(index = 5)
            long arg5;

            @Argument(index = 6)
            Byte arg6;

            @Argument(index = 7)
            byte arg7;

            @Argument(index = 8)
            Float arg8;

            @Argument(index = 9)
            float arg9;

            @Argument(index = 10)
            Double arg10;

            @Argument(index = 11)
            double arg11;

            @Argument(index = 12)
            String arg12;

            @Argument(index = 13)
            Character arg13;

            @Argument(index = 14)
            char arg14;

            @Argument(index = 15)
            AnEnum arg15;

            @Argument(index = 16)
            File arg16;

            @Argument(index = 17)
            Time arg17;

            @Argument(index = 18)
            long[] arg18;

            @Override
            public String call() {
                assertThat(arg0, is(0));
                assertThat(arg1, is(1));
                assertThat(arg2, is((short) 2));
                assertThat(arg3, is((short) 3));
                assertThat(arg4, is(4L));
                assertThat(arg5, is(5L));
                assertThat(arg6, is((byte) 6));
                assertThat(arg7, is((byte) 7));
                assertThat(arg8, is((float) 8.0));
                assertThat(arg9, is((float) 9.0));
                assertThat(arg10, is(10.0));
                assertThat(arg11, is(11.0));
                assertThat(arg12, is("12"));
                assertThat(arg13, is('a'));
                assertThat(arg14, is('b'));
                assertThat(arg15, is(AnEnum.BAR));
                assertThat(arg16, is(new File("/my/file")));
                assertThat(arg17, is(Time.valueOf("12:34:56")));
                assertArrayEquals(arg18, new long[] { 100, 101 });
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("0 1 2 3 4 5 6 7 8.0 9 10 11.00 12 a b BAR /my/file 12:34:56 100 101"));
}
Also used : Argument(dmg.util.command.Argument) Time(java.sql.Time) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Args(org.dcache.util.Args) File(java.io.File) Test(org.junit.Test)

Aggregations

Args (org.dcache.util.Args)69 Test (org.junit.Test)29 Arrays.asList (java.util.Arrays.asList)22 List (java.util.List)22 IOException (java.io.IOException)13 Argument (dmg.util.command.Argument)8 CommandException (dmg.util.CommandException)7 CommandExitException (dmg.util.CommandExitException)6 PoolPreferenceLevel (diskCacheV111.poolManager.PoolPreferenceLevel)5 CommandInterpreter (dmg.util.CommandInterpreter)5 File (java.io.File)5 CellPath (dmg.cells.nucleus.CellPath)4 CommandThrowableException (dmg.util.CommandThrowableException)4 Option (dmg.util.command.Option)4 BufferedReader (java.io.BufferedReader)4 FileNotFoundException (java.io.FileNotFoundException)4 InputStreamReader (java.io.InputStreamReader)4 FileAttributes (org.dcache.vehicles.FileAttributes)4 PoolSelectionUnitV2 (diskCacheV111.poolManager.PoolSelectionUnitV2)3 PoolV2Mode (diskCacheV111.pools.PoolV2Mode)3