Search in sources :

Example 1 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.

the class Picocli method addMappedOptionsToArgGroups.

private static void addMappedOptionsToArgGroups(CommandSpec cSpec, List<PropertyMapper> propertyMappers) {
    for (ConfigCategory category : ConfigCategory.values()) {
        List<PropertyMapper> mappersInCategory = propertyMappers.stream().filter(m -> category.equals(m.getCategory())).collect(Collectors.toList());
        if (mappersInCategory.isEmpty()) {
            // picocli raises an exception when an ArgGroup is empty, so ignore it when no mappings found for a category.
            continue;
        }
        ArgGroupSpec.Builder argGroupBuilder = ArgGroupSpec.builder().heading(category.getHeading() + ":").order(category.getOrder()).validate(false);
        for (PropertyMapper mapper : mappersInCategory) {
            String name = mapper.getCliFormat();
            String description = mapper.getDescription();
            if (description == null || cSpec.optionsMap().containsKey(name) || name.endsWith(OPTION_PART_SEPARATOR)) {
                // when key is already added or has no description, don't add.
                continue;
            }
            String defaultValue = mapper.getDefaultValue();
            Iterable<String> expectedValues = mapper.getExpectedValues();
            argGroupBuilder.addArg(OptionSpec.builder(name).defaultValue(defaultValue).description(description).paramLabel(mapper.getParamLabel()).completionCandidates(expectedValues).parameterConsumer(PropertyMapperParameterConsumer.INSTANCE).type(String.class).hidden(mapper.isHidden()).build());
        }
        cSpec.addArgGroup(argGroupBuilder.build());
    }
}
Also used : PersistedConfigSource(org.keycloak.quarkus.runtime.configuration.PersistedConfigSource) ConfigArgsConfigSource.parseConfigArgs(org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource.parseConfigArgs) UnaryOperator(java.util.function.UnaryOperator) Configuration.getRuntimeProperty(org.keycloak.quarkus.runtime.configuration.Configuration.getRuntimeProperty) ArrayList(java.util.ArrayList) Environment.isDevMode(org.keycloak.quarkus.runtime.Environment.isDevMode) ConfigArgsConfigSource(org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource) AUTO_BUILD_OPTION_SHORT(org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.AUTO_BUILD_OPTION_SHORT) PropertyMappers(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers) Build(org.keycloak.quarkus.runtime.cli.command.Build) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) StringUtil.isNotBlank(org.keycloak.utils.StringUtil.isNotBlank) OptionSpec(picocli.CommandLine.Model.OptionSpec) CommandLine(picocli.CommandLine) PrintWriter(java.io.PrintWriter) Environment(org.keycloak.quarkus.runtime.Environment) PropertyMappers.formatValue(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers.formatValue) Configuration.getConfig(org.keycloak.quarkus.runtime.configuration.Configuration.getConfig) OPTION_PART_SEPARATOR(org.keycloak.quarkus.runtime.configuration.Configuration.OPTION_PART_SEPARATOR) Iterator(java.util.Iterator) Main(org.keycloak.quarkus.runtime.cli.command.Main) Predicate(java.util.function.Predicate) Set(java.util.Set) ConfigArgsConfigSource.hasOptionValue(org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource.hasOptionValue) ArgGroupSpec(picocli.CommandLine.Model.ArgGroupSpec) Collectors(java.util.stream.Collectors) ConfigValue(io.smallrye.config.ConfigValue) PropertyMappers.isBuildTimeProperty(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers.isBuildTimeProperty) File(java.io.File) List(java.util.List) Start(org.keycloak.quarkus.runtime.cli.command.Start) Configuration.getBuildTimeProperty(org.keycloak.quarkus.runtime.configuration.Configuration.getBuildTimeProperty) Optional(java.util.Optional) ConfigCategory(org.keycloak.quarkus.runtime.configuration.mappers.ConfigCategory) PropertyMapper(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper) SECTION_KEY_COMMAND_LIST(picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_COMMAND_LIST) CommandSpec(picocli.CommandLine.Model.CommandSpec) Quarkus(io.quarkus.runtime.Quarkus) StartDev(org.keycloak.quarkus.runtime.cli.command.StartDev) AUTO_BUILD_OPTION_LONG(org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.AUTO_BUILD_OPTION_LONG) ConfigCategory(org.keycloak.quarkus.runtime.configuration.mappers.ConfigCategory) ArgGroupSpec(picocli.CommandLine.Model.ArgGroupSpec) PropertyMapper(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper)

Example 2 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.

the class Picocli method createCommandLine.

public static CommandLine createCommandLine(List<String> cliArgs) {
    CommandSpec spec = CommandSpec.forAnnotatedObject(new Main(), new DefaultFactory()).name(Environment.getCommand());
    for (CommandLine subCommand : spec.subcommands().values()) {
        CommandSpec subCommandSpec = subCommand.getCommandSpec();
        // help option added to any subcommand
        subCommandSpec.addOption(OptionSpec.builder(Help.OPTION_NAMES).usageHelp(true).description("This help message.").build());
    }
    addOption(spec, Start.NAME, hasAutoBuildOption(cliArgs), true);
    addOption(spec, StartDev.NAME, true, true);
    addOption(spec, Build.NAME, true, hasAutoBuildOption(cliArgs));
    CommandLine cmd = new CommandLine(spec);
    cmd.setExecutionExceptionHandler(new ExecutionExceptionHandler());
    cmd.setParameterExceptionHandler(new ShortErrorMessageHandler());
    cmd.setHelpFactory(new HelpFactory());
    cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new SubCommandListRenderer());
    cmd.setErr(new PrintWriter(System.err, true));
    return cmd;
}
Also used : CommandLine(picocli.CommandLine) CommandSpec(picocli.CommandLine.Model.CommandSpec) Main(org.keycloak.quarkus.runtime.cli.command.Main) PrintWriter(java.io.PrintWriter)

Example 3 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.

the class Picocli method addOption.

private static void addOption(CommandSpec spec, String command, boolean includeBuildTime, boolean includeRuntime) {
    CommandSpec commandSpec = spec.subcommands().get(command).getCommandSpec();
    List<PropertyMapper> mappers = new ArrayList<>();
    if (includeRuntime) {
        mappers.addAll(PropertyMappers.getRuntimeMappers());
    }
    if (includeBuildTime) {
        mappers.addAll(PropertyMappers.getBuildTimeMappers());
    }
    addMappedOptionsToArgGroups(commandSpec, mappers);
}
Also used : CommandSpec(picocli.CommandLine.Model.CommandSpec) ArrayList(java.util.ArrayList) PropertyMapper(org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper)

Example 4 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.

the class SubCommandListRenderer method render.

@Override
public String render(Help help) {
    CommandSpec spec = help.commandSpec();
    if (spec.subcommands().isEmpty()) {
        return "";
    }
    Column commands = new Column(24, 2, Column.Overflow.SPAN);
    Column descriptions = new Column(spec.usageMessage().width() - 24, 2, Column.Overflow.WRAP);
    TextTable textTable = TextTable.forColumns(help.colorScheme(), commands, descriptions);
    textTable.setAdjustLineBreaksForWideCJKCharacters(spec.usageMessage().adjustLineBreaksForWideCJKCharacters());
    addHierarchy(spec.subcommands().values(), textTable, "");
    return textTable.toString();
}
Also used : Column(picocli.CommandLine.Help.Column) CommandSpec(picocli.CommandLine.Model.CommandSpec) TextTable(picocli.CommandLine.Help.TextTable)

Example 5 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project aion by aionnetwork.

the class CliTest method testAccountCliParseAndRun.

@Parameters(method = "parametersFortestAccountCliParseAndRun")
@Test
public void testAccountCliParseAndRun(String[] options, ReturnType expected) {
    CommandLine commandLine = new CommandLine(Arguments.class).addSubcommand(EditCli.class);
    ParseResult result;
    try {
        result = commandLine.parseArgs(options);
    } catch (Exception e) {
        if (expected.equals(ERROR)) {
            return;
        } else {
            throw e;
        }
    }
    CommandSpec commandSpec = Cli.findCommandSpec(result, AccountCli.class);
    // noinspection ConstantConditions
    AccountCli spiedAccountCLI = spy((AccountCli) commandSpec.userObject());
    doReturn(true).when(spiedAccountCLI).exportPrivateKey(anyString(), any());
    doReturn(true).when(spiedAccountCLI).listAccounts();
    doReturn(true).when(spiedAccountCLI).createAccount(any());
    doReturn(true).when(spiedAccountCLI).importPrivateKey(anyString(), any());
    doCallRealMethod().when(spiedAccountCLI).runCommand(any());
    assertThat(spiedAccountCLI.runCommand(mockpr)).isEqualTo(expected);
    if (spiedAccountCLI.getList()) {
        verify(spiedAccountCLI, times(1)).listAccounts();
    } else {
        verify(spiedAccountCLI, times(0)).listAccounts();
    }
    if (!spiedAccountCLI.getGroup().getImportAcc().isEmpty()) {
        verify(spiedAccountCLI, times(1)).importPrivateKey(anyString(), any());
    } else {
        verify(spiedAccountCLI, times(0)).importPrivateKey(anyString(), any());
    }
    if (!spiedAccountCLI.getGroup().getExport().isEmpty()) {
        verify(spiedAccountCLI, times(1)).exportPrivateKey(anyString(), any());
    } else {
        verify(spiedAccountCLI, times(0)).exportPrivateKey(anyString(), any());
    }
    if (spiedAccountCLI.getGroup().getCreate()) {
        verify(spiedAccountCLI, times(1)).createAccount(any());
    } else {
        verify(spiedAccountCLI, times(0)).createAccount(any());
    }
    Mockito.clearInvocations(spiedAccountCLI);
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) CommandSpec(picocli.CommandLine.Model.CommandSpec) IOException(java.io.IOException) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

CommandSpec (picocli.CommandLine.Model.CommandSpec)8 CommandLine (picocli.CommandLine)6 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 Main (org.keycloak.quarkus.runtime.cli.command.Main)2 PropertyMapper (org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper)2 Quarkus (io.quarkus.runtime.Quarkus)1 ConfigValue (io.smallrye.config.ConfigValue)1 Console (java.io.Console)1 java.util (java.util)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Set (java.util.Set)1