diff --git a/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/AtlasDbConfiguredCommand.java b/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/AtlasDbConfiguredCommand.java index e40f7c81ec8..d78cf9ac0f5 100644 --- a/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/AtlasDbConfiguredCommand.java +++ b/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/AtlasDbConfiguredCommand.java @@ -20,7 +20,7 @@ import com.google.common.collect.ImmutableMap; import com.palantir.atlasdb.dropwizard.commands.AtlasDbCommand; import com.palantir.atlasdb.dropwizard.commands.AtlasDbConsoleCommand; -import com.palantir.atlasdb.dropwizard.commands.AtlasDbTimestampCommand; +import com.palantir.atlasdb.dropwizard.commands.AtlasDbCliCommand; import io.dropwizard.Configuration; import io.dropwizard.cli.ConfiguredCommand; @@ -33,16 +33,16 @@ public class AtlasDbConfiguredCommand configurationClass; - private final Map> subCommands; + + private final AtlasDbCommand consoleCommand; + private final AtlasDbCommand cliCommand; protected AtlasDbConfiguredCommand(Class configurationClass) { super("atlasdb", "Run AtlasDB tasks"); this.configurationClass = configurationClass; - this.subCommands = ImmutableMap.>builder() - .put("console", new AtlasDbConsoleCommand<>(configurationClass)) - .put("timestamp", new AtlasDbTimestampCommand<>(configurationClass)) - .build(); + this.consoleCommand = new AtlasDbConsoleCommand<>(configurationClass); + this.cliCommand = new AtlasDbCliCommand<>(configurationClass); } @Override @@ -52,17 +52,21 @@ protected Class getConfigurationClass() { @Override public void configure(Subparser subparser) { - for (AtlasDbCommand subCommand : subCommands.values()) { - Subparser parser = subparser.addSubparsers() - .addParser(subCommand.getName()) - .setDefault(COMMAND_NAME_ATTR, subCommand.getName()) - .description(subCommand.getDescription()); - subCommand.configure(parser); - } + Subparser parser = subparser.addSubparsers() + .addParser(consoleCommand.getName()) + .setDefault(COMMAND_NAME_ATTR, consoleCommand.getName()) + .description(consoleCommand.getDescription()); + consoleCommand.configure(parser); + + cliCommand.configure(subparser); } @Override protected void run(Bootstrap bootstrap, Namespace namespace, T configuration) throws Exception { - subCommands.get(namespace.getString(COMMAND_NAME_ATTR)).run(bootstrap, namespace); + if (consoleCommand.getName().equals(namespace.getString(COMMAND_NAME_ATTR))) { + consoleCommand.run(bootstrap, namespace); + } else { + cliCommand.run(bootstrap, namespace); + } } } diff --git a/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbTimestampCommand.java b/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbCliCommand.java similarity index 52% rename from atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbTimestampCommand.java rename to atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbCliCommand.java index faf29c39a91..61a1ea269b5 100644 --- a/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbTimestampCommand.java +++ b/atlasdb-dropwizard-bundle/src/main/java/com/palantir/atlasdb/dropwizard/commands/AtlasDbCliCommand.java @@ -11,14 +11,14 @@ import com.fasterxml.jackson.datatype.guava.GuavaModule; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; -import com.palantir.atlasdb.cli.command.timestamp.CleanTransactionRange; -import com.palantir.atlasdb.cli.command.timestamp.FastForwardTimestamp; -import com.palantir.atlasdb.cli.command.timestamp.FetchTimestamp; +import com.google.common.collect.Iterables; +import com.palantir.atlasdb.cli.AtlasCli; import com.palantir.atlasdb.config.AtlasDbConfig; import com.palantir.atlasdb.config.ImmutableAtlasDbConfig; import com.palantir.atlasdb.dropwizard.AtlasDbConfigurationProvider; import io.airlift.airline.Cli; +import io.airlift.airline.model.CommandGroupMetadata; import io.airlift.airline.model.CommandMetadata; import io.airlift.airline.model.OptionMetadata; import io.dropwizard.Configuration; @@ -28,16 +28,10 @@ import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Subparser; -public class AtlasDbTimestampCommand extends AtlasDbCommand { - private static final String COMMAND_NAME_ATTR = "timestampSubCommand"; +public class AtlasDbCliCommand extends AtlasDbCommand { + private static final String COMMAND_NAME_ATTR = "airlineSubCommand"; private static final Object ZERO_ARITY_ARG_CONSTANT = ""; - private static final Cli TIMESTAMP_CLI = Cli.builder("timestamp") - .withDescription("Timestamp-centric commands") - .withCommands( - FetchTimestamp.class, - CleanTransactionRange.class, - FastForwardTimestamp.class) - .build(); + private static final Cli CLI = AtlasCli.buildCli(); private static final ObjectMapper OBJECT_MAPPER; static { @@ -47,50 +41,62 @@ public class AtlasDbTimestampCommand configurationClass) { - super("timestamp", "Get timestamp information", configurationClass); + public AtlasDbCliCommand(Class configurationClass) { + super(CLI.getMetadata().getName(), CLI.getMetadata().getDescription(), configurationClass); } @Override public void configure(Subparser subparser) { - for (CommandMetadata subCommand : TIMESTAMP_CLI.getMetadata().getDefaultGroupCommands()) { + for (CommandMetadata subCommand : CLI.getMetadata().getDefaultGroupCommands()) { + addCommandToParser(subparser, subCommand, ImmutableList.of()); + } + + for (CommandGroupMetadata commandGroup : CLI.getMetadata().getCommandGroups()) { Subparser parser = subparser.addSubparsers() - .addParser(subCommand.getName()) - .setDefault(COMMAND_NAME_ATTR, subCommand.getName()) - .description(subCommand.getDescription()); - - List commandOptions = ImmutableList.builder() - .addAll(subCommand.getCommandOptions()) - .addAll(subCommand.getGroupOptions()) - .build(); - - for (OptionMetadata option : commandOptions) { - if (option.isHidden()) { - continue; - } - - List sortedList = option.getOptions().stream() - .sorted((a, b) -> Integer.compareUnsigned(a.length(), b.length())) - .collect(Collectors.toList()); - - String shortOption = sortedList.get(0); - String longOption = sortedList.get(1); - - Argument arg = parser.addArgument(shortOption, longOption) - .required(option.isRequired()) - .help(option.getDescription()) - .dest(longOption); - - if(option.getArity() == 0) { - arg.action(Arguments.storeConst()); - arg.setConst(ZERO_ARITY_ARG_CONSTANT); - } else { - arg.nargs(option.getArity()); - } + .addParser(commandGroup.getName()) + .description(commandGroup.getDescription()); + for (CommandMetadata subCommand : commandGroup.getCommands()) { + addCommandToParser(parser, subCommand, ImmutableList.of(commandGroup.getName())); + } + } + } + + private void addCommandToParser(Subparser subparser, CommandMetadata subCommand, List commandRoot) { + Subparser parser = subparser.addSubparsers() + .addParser(subCommand.getName()) + .description(subCommand.getDescription()) + .setDefault(COMMAND_NAME_ATTR, ImmutableList.builder().addAll(commandRoot).add(subCommand.getName()).build()); + + List commandOptions = ImmutableList.builder() + .addAll(subCommand.getCommandOptions()) + .addAll(subCommand.getGroupOptions()) + .build(); + + for (OptionMetadata option : commandOptions) { + if (option.isHidden()) { + continue; } - super.configure(parser); + List sortedOptions = option.getOptions().stream() + .sorted((a, b) -> Integer.compareUnsigned(a.length(), b.length())) + .collect(Collectors.toList()); + + String longOption = Iterables.getLast(sortedOptions); + + Argument arg = parser.addArgument(sortedOptions.toArray(new String[] {})) + .required(option.isRequired()) + .help(option.getDescription()) + .dest(longOption); + + if(option.getArity() == 0) { + arg.action(Arguments.storeConst()); + arg.setConst(ZERO_ARITY_ARG_CONSTANT); + } else { + arg.nargs(option.getArity()); + } } + + super.configure(parser); } @Override @@ -117,10 +123,10 @@ protected void run(Bootstrap bootstrap, Namespace namespace, T configuration) List allArgs = ImmutableList.builder() .add("--inline-config") .add(OBJECT_MAPPER.writeValueAsString(configurationWithoutLeader)) - .add(namespace.getString(COMMAND_NAME_ATTR)) + .addAll(namespace.getList(COMMAND_NAME_ATTR)) .addAll(passedInArgs) .build(); - TIMESTAMP_CLI.parse(allArgs).call(); + CLI.parse(allArgs).call(); } }