Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use picocli for main cli #946

Merged
merged 43 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
891cf46
Declare picocli dependency version in root pom's dep management section
chris-j-h Nov 29, 2019
1a26213
[WIP] Replace DefaultCli Apache CLI with PicoCLI
chris-j-h Nov 29, 2019
d4429d8
[KEYGEN-WIP] Replace DefaultCli Apache CLI with PicoCLI
chris-j-h Nov 29, 2019
5259cae
[KEYGEN-WIP] Replace DefaultCli Apache CLI with PicoCLI
chris-j-h Nov 30, 2019
237209b
[KEYUPDATE-WIP] Replace DefaultCli Apache CLI with PicoCLI
chris-j-h Nov 30, 2019
44e802b
[WIP] Use picoCLI for main tessera cmd - dynamically generate options…
chris-j-h Dec 3, 2019
dc11650
[WIP TESSERA-CMD] Prompt user for pwds if necessary, validate config …
chris-j-h Dec 3, 2019
41e2bed
Clean up
chris-j-h Dec 4, 2019
2023be5
Parse and execute subcommands
chris-j-h Dec 4, 2019
10a8558
Implement keygen behaviour. Create a mixin for encryptor options.
chris-j-h Dec 4, 2019
4473525
Execute subcmd without having to manually determine subcmd being called
chris-j-h Dec 4, 2019
8c69997
Change main cmd name to tessera
chris-j-h Dec 4, 2019
9ab8d7a
Show help if no options are provided for a subcommand
chris-j-h Dec 4, 2019
3b84ad5
Remove hardcoded default for encryptor type CLI option and clean up
chris-j-h Dec 4, 2019
6fffad3
Merge branch 'master' into cli
chris-j-h Dec 4, 2019
5468edc
Create picocli module for new files.
chris-j-h Dec 4, 2019
965a6b9
[WIP] Migrate existing CLI tests to new picocli implementation
chris-j-h Dec 5, 2019
99f399f
Add gradle buld files. just enough to build.
melowe Dec 10, 2019
06b69d0
Merge branch 'master' into cli
chris-j-h Dec 12, 2019
3145f49
Fix tessera-launcher dependencies after incorrect merge with master
chris-j-h Dec 12, 2019
2222212
Start to migrate DefaultCliAdapter tests for use with PicoCliDelegate
chris-j-h Dec 12, 2019
0f79b73
Update OverrideUtil to enable positional selection of override targets
chris-j-h Dec 13, 2019
fd8f39e
Update pico cli delegate tests with new override syntax
chris-j-h Dec 15, 2019
3812037
Migrate remainder of DefaultCliAdapter tests for pico CLI
chris-j-h Dec 15, 2019
bf55f36
Remove unused CliDelegate code and tests & execute enclave CLI directly
chris-j-h Dec 15, 2019
2625aa3
Migrate keygen command tests to picocli
chris-j-h Dec 15, 2019
9f94170
Migrate update password command tests to picocli
chris-j-h Dec 16, 2019
3c2a261
Fix enclave cmd tests
chris-j-h Dec 16, 2019
ddcec46
Config migration CLI test fixes
chris-j-h Dec 16, 2019
2edf395
Database migration CLI test fixes
chris-j-h Dec 16, 2019
204bab3
Pidfile CLI option and ArgonOptionsConverter tests
chris-j-h Dec 16, 2019
940365c
Enable admin CLI subcommand and fix tests
chris-j-h Dec 17, 2019
445af90
Update ConfigFileStore during startup for use in config update methods
chris-j-h Dec 17, 2019
e96de75
Add comma split on multi keygen option & subcommand exception mapper
chris-j-h Dec 17, 2019
ce56853
Test subcommand exception handling
chris-j-h Dec 18, 2019
3f5d446
Add validation on argon algorithm in keyupdate cmd
chris-j-h Dec 18, 2019
3615821
Update keygen argon file option name
chris-j-h Dec 18, 2019
8952c2c
Return correct status code for updatepassword cmd
chris-j-h Dec 18, 2019
5f8b584
Add admin cli as as dependency for pico cli module (reflect maven cha…
melowe Dec 18, 2019
8ed083f
Remove commons cli dependnecy and change admin package name.
melowe Dec 19, 2019
4c7ff1a
Colapse admin-cli and pico-cli modules into config-cli
melowe Dec 19, 2019
075505b
Populate unmatched cli options and search for config override matches.
melowe Dec 20, 2019
8bd8b94
move index forward when value has neen extracted from unmatched option.
melowe Dec 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions cli/admin-cli/build.gradle

This file was deleted.

34 changes: 0 additions & 34 deletions cli/admin-cli/pom.xml

This file was deleted.

This file was deleted.

This file was deleted.

77 changes: 0 additions & 77 deletions cli/admin-cli/src/test/resources/sample-config.json

This file was deleted.

3 changes: 1 addition & 2 deletions cli/cli-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

dependencies {
compile 'info.picocli:picocli:4.0.4'
compile 'info.picocli:picocli'
compile 'org.apache.commons:commons-lang3:3.7'
compile 'commons-cli:commons-cli:1.4'
compile project(':config')
compile project(':shared')
compile project(':encryption:encryption-api')
Expand Down
1 change: 0 additions & 1 deletion cli/cli-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.0.4</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package com.quorum.tessera.cli;

import com.quorum.tessera.ServiceLoaderUtil;
import com.quorum.tessera.cli.parsers.ConfigConverter;
import com.quorum.tessera.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static picocli.CommandLine.Model.CommandSpec.DEFAULT_COMMAND_NAME;

// TODO(cjh) still using CliDelegate as a config store so that config can be injected by spring
public enum CliDelegate {
INSTANCE;

private static final Logger LOGGER = LoggerFactory.getLogger(CliDelegate.class);

private static final CliResult HELP_RESULT = new CliResult(0, true, null);

private static final CliResult DEFAULT_RESULT = new CliResult(1, true, null);

private Config config;

public static CliDelegate instance() {
Expand All @@ -36,70 +20,7 @@ public Config getConfig() {
() -> new IllegalStateException("Execute must be invoked before attempting to fetch config"));
}

public CliResult execute(String... args) throws Exception {

final List<CliAdapter> adapters = ServiceLoaderUtil.loadAll(CliAdapter.class).collect(Collectors.toList());

LOGGER.debug("Loaded adapters {}", adapters);

CliType cliType = CliType.valueOf(System.getProperty(CliType.CLI_TYPE_KEY, CliType.CONFIG.name()));

LOGGER.debug("cliType {}", cliType);

Predicate<CliAdapter> isTopLevel =
a -> a.getClass().getAnnotation(CommandLine.Command.class).name().equals(DEFAULT_COMMAND_NAME);

// Finds the top level adapter that we want to start with. Exactly one is expected to be on the classpath.
final CliAdapter adapter =
adapters.stream()
.filter(a -> a.getClass().isAnnotationPresent(CommandLine.Command.class))
.filter(isTopLevel)
.filter(a -> a.getType() == cliType)
.findFirst()
.get();

LOGGER.debug("Loaded adapter {}", adapter);

// Then we find all the others and attach them as sub-commands. It is expected that they have defined their
// own hierarchy and command names.
final List<CliAdapter> subcommands =
adapters.stream()
// .filter(isTopLevel)
.filter(a -> a != adapter)
.filter(a -> a.getType() != CliType.ENCLAVE)
.collect(Collectors.toList());

// the mapper will give us access to the exception from the outside, if one occurred.
// mostly since we have an existing system, and this is a workaround
final CLIExceptionCapturer mapper = new CLIExceptionCapturer();
final CommandLine commandLine = new CommandLine(adapter);

subcommands.stream().peek(sc -> LOGGER.debug("Adding subcommand {}", sc)).forEach(commandLine::addSubcommand);

commandLine
.registerConverter(Config.class, new ConfigConverter())
.setSeparator(" ")
.setCaseInsensitiveEnumValuesAllowed(true)
.setUnmatchedArgumentsAllowed(true)
.setExecutionExceptionHandler(mapper)
.setParameterExceptionHandler(mapper);

commandLine.execute(args);

// if an exception occurred, throw it to to the upper levels where it gets handled
if (mapper.getThrown() != null) {
throw mapper.getThrown();
}

// otherwise, set the config object (if there is one) and return
final CliResult result =
commandLine.getParseResult().asCommandLineList().stream()
.map(cl -> cl.isUsageHelpRequested() ? HELP_RESULT : cl.getExecutionResult())
.filter(Objects::nonNull)
.findFirst()
.orElse(DEFAULT_RESULT);

this.config = result.getConfig().orElse(null);
return result;
public void setConfig(Config config) {
this.config = config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.quorum.tessera.config.Config;
import com.quorum.tessera.config.ConfigFactory;
import com.quorum.tessera.config.util.ConfigFileStore;
import picocli.CommandLine;

import java.io.FileNotFoundException;
Expand All @@ -22,6 +23,8 @@ public Config convert(final String value) throws Exception {
throw new FileNotFoundException(String.format("%s not found.", path));
}

ConfigFileStore.create(path);

try (InputStream in = Files.newInputStream(path)) {
return configFactory.create(in);
}
Expand Down
Loading