Java Code Examples for io.airlift.airline.Cli#parse()

The following examples show how to use io.airlift.airline.Cli#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudExplorerLiveTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected void call(InputStream instream, String... args) throws Exception {
    ByteArrayOutputStream stdoutStream = new ByteArrayOutputStream();
    ByteArrayOutputStream stderrStream = new ByteArrayOutputStream();
    
    Cli<BrooklynCommand> parser = new Main().cliBuilder().build();
    
    BrooklynCommand command = parser.parse(args);
    
    InputStream origIn = System.in;
    PrintStream origOut = System.out;
    PrintStream origErr = System.err;
    try {
        System.setIn(instream);
        System.setOut(new PrintStream(stdoutStream));
        System.setErr(new PrintStream(stderrStream));

        command.call();
    } finally {
        System.setIn(origIn);
        System.setOut(origOut);
        System.setOut(origErr);
        stdout = new String(stdoutStream.toByteArray());
        stderr = new String(stderrStream.toByteArray());
    }
}
 
Example 2
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testCliSystemPropertyDefines() {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command0 = cli.parse(
        "-Dorg.apache.brooklyn.cli.CliTest.sample1=foo",
        "-Dorg.apache.brooklyn.cli.CliTest.sample2=bar",
        "launch", 
        "-Dorg.apache.brooklyn.cli.CliTest.sample3=baz"
        );
    assertTrue(command0 instanceof LaunchCommand);
    LaunchCommand command = (LaunchCommand) command0;
    assertEquals(command.getDefines().size(), 3, 
        "Command is: "+command);
    assertTrue(command.getDefines().get(0).equals("org.apache.brooklyn.cli.CliTest.sample1=foo"),  
        "Command is: "+command);
    assertTrue(command.getDefines().get(2).equals("org.apache.brooklyn.cli.CliTest.sample3=baz"), 
        "Command is: "+command);
    assertEquals(command.getDefinesAsMap().get("org.apache.brooklyn.cli.CliTest.sample3"), "baz",
        "Command is: "+command);
}
 
Example 3
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchWritesOutApacheBrooklyn() throws Exception {
    InputStream origIn = System.in;
    PrintStream origOut = System.out;
    try {
        InputStream stdin = new ByteArrayInputStream("".getBytes());
        System.setIn(stdin);

        final ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream();
        PrintStream stdout = new PrintStream(stdoutBytes);
        System.setOut(stdout);

        Cli<BrooklynCommand> cli = buildCli();
        BrooklynCommand command = cli.parse("launch", "--noConsole");
        submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
                @Override
                public void run() {
                    String actualStdout = new String(stdoutBytes.toByteArray());
                    assertTrue(actualStdout.contains("Apache Brooklyn"), "stdout="+actualStdout);
                }
            });
    
    } finally {
        System.setIn(origIn);
        System.setOut(origOut);
    }
}
 
Example 4
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchWillStartAppWhenGivenImpl() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("launch", "--noConsole", "--app", ExampleApp.class.getName(), "--location", "localhost");
    submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
            @Override
            public void run() {
                assertTrue(exampleAppConstructed);
                assertTrue(exampleAppRunning);
            }
        });
}
 
Example 5
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchStartsYamlApp() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("launch", "--noConsole", "--app", "example-app-no-location.yaml", "--location", "localhost");
    submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
            @Override
            public void run() {
                assertTrue(exampleEntityRunning);
            }
        });
}
 
Example 6
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchStartsYamlAppWithCommandLineLocation() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("launch", "--noConsole", "--app", "example-app-no-location.yaml", "--location", "localhost:(name=testLocalhost)");
    submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
            @Override
            public void run() {
                assertTrue(exampleEntityRunning);
                assertTrue(Iterables.getOnlyElement(exampleEntity.getApplication().getLocations()).getDisplayName().equals("testLocalhost"));
            }
        });
}
 
Example 7
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchStartsYamlAppWithYamlAppLocation() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("launch", "--noConsole", "--app", "example-app-app-location.yaml");
    submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
            @Override
            public void run() {
                assertTrue(exampleEntityRunning);
                assertTrue(Iterables.getOnlyElement(exampleEntity.getApplication().getLocations()).getDisplayName().equals("appLocalhost"));
            }
        });
}
 
Example 8
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchStartsYamlAppWithYamlAndAppCliLocation() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("launch", "--noConsole", "--app", "example-app-app-location.yaml", "--location", "localhost");
    submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
            @Override
            public void run() {
                assertTrue(exampleEntityRunning);
                assertTrue(Iterables.getFirst(exampleEntity.getApplication().getLocations(), null).getDisplayName().equals("appLocalhost"));
            }
        });
}
 
Example 9
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratePasswordCommandParsed() throws Exception {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("generate-password", "--user", "myname");
    
    assertTrue(command instanceof GeneratePasswordCommand);
}
 
Example 10
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppOptionIsOptional() throws ParseException {
    Cli<BrooklynCommand> cli = buildCli();
    cli.parse("launch", "blah", "my.App");
}
 
Example 11
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testHelpCommand() {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse("help");
    assertTrue(command instanceof HelpCommand, "Command is: "+command);
}
 
Example 12
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testDefaultInfoCommand() {
    Cli<BrooklynCommand> cli = buildCli();
    BrooklynCommand command = cli.parse();
    assertTrue(command instanceof DefaultInfoCommand, "Command is: "+command);
}
 
Example 13
Source File: CliTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected List<String> runCommand(Cli<BrooklynCommand> cli, Iterable<String> args, String input) throws Exception {
    final BrooklynCommand command = cli.parse(args);
    
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    Thread t= new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                command.call();
            } catch (Exception e) {
                exception.set(e);
                throw Exceptions.propagate(e);
            }
        }});
    
    InputStream origIn = System.in;
    PrintStream origOut = System.out;
    try {
        InputStream stdin = new ByteArrayInputStream(input.getBytes());
        System.setIn(stdin);

        ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream();
        PrintStream stdout = new PrintStream(stdoutBytes);
        System.setOut(stdout);

        t.start();

        t.join(10*1000);
        assertFalse(t.isAlive());
        
        if (exception.get() != null) {
            throw new ExecutionException(exception.get());
        }
        
        return ImmutableList.copyOf(Splitter.on(Pattern.compile("\r?\n")).split(new String(stdoutBytes.toByteArray())));
    } finally {
        System.setIn(origIn);
        System.setOut(origOut);
        t.interrupt();
    }
}