Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Convert timestamp CLI dropwizard class into a generic one for all CLIs
Browse files Browse the repository at this point in the history
  • Loading branch information
SerialVelocity committed Aug 16, 2016
1 parent ddf2207 commit 9a27fe9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,16 +33,16 @@ public class AtlasDbConfiguredCommand<T extends Configuration & AtlasDbConfigura
private static final String COMMAND_NAME_ATTR = "subCommand";

private final Class<T> configurationClass;
private final Map<String, AtlasDbCommand<T>> subCommands;

private final AtlasDbCommand<T> consoleCommand;
private final AtlasDbCommand<T> cliCommand;

protected AtlasDbConfiguredCommand(Class<T> configurationClass) {
super("atlasdb", "Run AtlasDB tasks");

this.configurationClass = configurationClass;
this.subCommands = ImmutableMap.<String, AtlasDbCommand<T>>builder()
.put("console", new AtlasDbConsoleCommand<>(configurationClass))
.put("timestamp", new AtlasDbTimestampCommand<>(configurationClass))
.build();
this.consoleCommand = new AtlasDbConsoleCommand<>(configurationClass);
this.cliCommand = new AtlasDbCliCommand<>(configurationClass);
}

@Override
Expand All @@ -52,17 +52,21 @@ protected Class<T> getConfigurationClass() {

@Override
public void configure(Subparser subparser) {
for (AtlasDbCommand<T> 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<T> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,16 +28,10 @@
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

public class AtlasDbTimestampCommand<T extends Configuration & AtlasDbConfigurationProvider> extends AtlasDbCommand<T> {
private static final String COMMAND_NAME_ATTR = "timestampSubCommand";
public class AtlasDbCliCommand<T extends Configuration & AtlasDbConfigurationProvider> extends AtlasDbCommand<T> {
private static final String COMMAND_NAME_ATTR = "airlineSubCommand";
private static final Object ZERO_ARITY_ARG_CONSTANT = "<ZERO ARITY ARG CONSTANT>";
private static final Cli<Callable> TIMESTAMP_CLI = Cli.<Callable>builder("timestamp")
.withDescription("Timestamp-centric commands")
.withCommands(
FetchTimestamp.class,
CleanTransactionRange.class,
FastForwardTimestamp.class)
.build();
private static final Cli<Callable> CLI = AtlasCli.buildCli();
private static final ObjectMapper OBJECT_MAPPER;

static {
Expand All @@ -47,50 +41,62 @@ public class AtlasDbTimestampCommand<T extends Configuration & AtlasDbConfigurat
OBJECT_MAPPER.registerModule(new GuavaModule());
}

public AtlasDbTimestampCommand(Class<T> configurationClass) {
super("timestamp", "Get timestamp information", configurationClass);
public AtlasDbCliCommand(Class<T> 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<OptionMetadata> commandOptions = ImmutableList.<OptionMetadata>builder()
.addAll(subCommand.getCommandOptions())
.addAll(subCommand.getGroupOptions())
.build();

for (OptionMetadata option : commandOptions) {
if (option.isHidden()) {
continue;
}

List<String> 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<String> commandRoot) {
Subparser parser = subparser.addSubparsers()
.addParser(subCommand.getName())
.description(subCommand.getDescription())
.setDefault(COMMAND_NAME_ATTR, ImmutableList.builder().addAll(commandRoot).add(subCommand.getName()).build());

List<OptionMetadata> commandOptions = ImmutableList.<OptionMetadata>builder()
.addAll(subCommand.getCommandOptions())
.addAll(subCommand.getGroupOptions())
.build();

for (OptionMetadata option : commandOptions) {
if (option.isHidden()) {
continue;
}

super.configure(parser);
List<String> 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
Expand All @@ -117,10 +123,10 @@ protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration)
List<String> allArgs = ImmutableList.<String>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();
}
}

0 comments on commit 9a27fe9

Please sign in to comment.