use of com.quorum.tessera.cli.CliResult in project tessera by ConsenSys.
the class PicoCliDelegateTest method withValidConfigAndPidfileAlreadyExists.
@Test
public void withValidConfigAndPidfileAlreadyExists() throws Exception {
Path configFile = Paths.get(getClass().getResource("/sample-config.json").toURI());
Path pidFilePath = Files.createTempFile(UUID.randomUUID().toString(), "");
pidFilePath.toFile().deleteOnExit();
assertThat(pidFilePath).exists();
CliResult result = cliDelegate.execute("-configfile", configFile.toString(), "-pidfile", pidFilePath.toString());
assertThat(pidFilePath).exists();
assertThat(result).isNotNull();
assertThat(result.getConfig()).isPresent();
assertThat(result.getStatus()).isEqualTo(0);
assertThat(result.isSuppressStartup()).isFalse();
}
use of com.quorum.tessera.cli.CliResult in project tessera by ConsenSys.
the class PicoCliDelegateTest method suppressStartupForKeygenOptionWithFileOutputOptions.
@Test
public void suppressStartupForKeygenOptionWithFileOutputOptions() throws Exception {
Path publicKeyPath = Files.createTempFile(UUID.randomUUID().toString(), "");
Path privateKeyPath = Files.createTempFile(UUID.randomUUID().toString(), "");
Files.write(privateKeyPath, Arrays.asList("SOMEDATA"));
Files.write(publicKeyPath, Arrays.asList("SOMEDATA"));
FilesystemKeyPair keypair = new FilesystemKeyPair(publicKeyPath, privateKeyPath, null);
when(keyGenerator.generate(anyString(), eq(null), eq(null))).thenReturn(keypair);
final Path configFile = Paths.get(getClass().getResource("/sample-config.json").toURI());
final Path configOutputPath = configFile.resolveSibling(UUID.randomUUID().toString() + ".json");
final CliResult cliResult = cliDelegate.execute("-keygen", "-configfile", configFile.toString(), "-output", configOutputPath.toString());
assertThat(cliResult.isSuppressStartup()).isTrue();
}
use of com.quorum.tessera.cli.CliResult in project tessera by ConsenSys.
the class KeyGenCommandTest method onlySingleOutputFileProvided.
@Test
public void onlySingleOutputFileProvided() throws Exception {
List<String> optionVariations = List.of("--keyout", "-filename");
ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
when(keyGenerator.generate("myfile", null, null)).thenReturn(configKeyPair);
when(keyGeneratorFactory.create(refEq(null), any(EncryptorConfig.class))).thenReturn(keyGenerator);
for (String option : optionVariations) {
String arg = option.concat("=myfile");
int exitCode = commandLine.execute(arg);
assertThat(exitCode).isZero();
CommandLine.ParseResult parseResult = commandLine.getParseResult();
assertThat(parseResult).isNotNull();
assertThat(parseResult.matchedArgs()).hasSize(1);
assertThat(parseResult.hasMatchedOption("--keyout"));
assertThat(parseResult.unmatched()).isEmpty();
CliResult result = commandLine.getExecutionResult();
assertThat(result).isNotNull();
assertThat(result.isSuppressStartup()).isTrue();
assertThat(result.getConfig()).isNotPresent();
assertThat(result.getStatus()).isEqualTo(0);
}
verify(keyDataMarshaller, times(optionVariations.size())).marshal(configKeyPair);
verify(keyGeneratorFactory, times(optionVariations.size())).create(refEq(null), any(EncryptorConfig.class));
verify(keyGenerator, times(optionVariations.size())).generate("myfile", null, null);
}
use of com.quorum.tessera.cli.CliResult in project tessera by ConsenSys.
the class PicoCliDelegate method execute.
public CliResult execute(String... args) throws Exception {
LOGGER.debug("Execute with args [{}]", String.join(",", args));
final CommandLine commandLine = new CommandLine(TesseraCommand.class);
final CLIExceptionCapturer exceptionCapturer = new CLIExceptionCapturer();
commandLine.addSubcommand(new CommandLine(CommandLine.HelpCommand.class));
commandLine.addSubcommand(new CommandLine(VersionCommand.class));
commandLine.addSubcommand(new CommandLine(KeyGenCommand.class, new KeyGenCommandFactory()));
commandLine.addSubcommand(new CommandLine(KeyUpdateCommand.class, new KeyUpdateCommandFactory()));
commandLine.registerConverter(Config.class, new ConfigConverter()).registerConverter(ArgonOptions.class, new ArgonOptionsConverter()).setSeparator(" ").setCaseInsensitiveEnumValuesAllowed(true).setExecutionExceptionHandler(exceptionCapturer).setParameterExceptionHandler(exceptionCapturer).setStopAtUnmatched(false);
try {
// parse the args so that we can print usage help if no cmd args were provided
final CommandLine.ParseResult parseResult = commandLine.parseArgs(args);
final List<CommandLine> l = parseResult.asCommandLineList();
final CommandLine lastCmd = l.get(l.size() - 1);
// print help if no args were provided
if (lastCmd.getParseResult().matchedArgs().size() == 0 && !"help".equals(lastCmd.getCommandName()) && !"version".equals(lastCmd.getCommandName())) {
lastCmd.usage(lastCmd.getOut());
} else {
commandLine.execute(args);
}
} catch (CommandLine.ParameterException ex) {
exceptionCapturer.handleParseException(ex, args);
}
// if an exception occurred, throw it to to the upper levels where it gets handled
if (exceptionCapturer.getThrown() != null) {
throw exceptionCapturer.getThrown();
}
return (CliResult) Optional.ofNullable(commandLine.getExecutionResult()).orElse(new CliResult(0, true, null));
}
use of com.quorum.tessera.cli.CliResult in project tessera by ConsenSys.
the class TesseraCommand method call.
@Override
public CliResult call() throws Exception {
// all subcmds
if (Objects.isNull(config)) {
throw new NoTesseraConfigfileOptionException();
}
// overrides using '-o <KEY>=<VALUE>'
overrides.forEach(this::overrideConfigValue);
// legacy overrides using unmatched options
if (Objects.nonNull(unmatchedEntries)) {
LOGGER.warn("Using unmatched CLI options for config overrides is deprecated. Use the --override option instead.");
List<String> unmatched = new ArrayList<>(unmatchedEntries);
for (int i = 0; i < unmatched.size(); i += 2) {
String line = unmatched.get(i);
if (!line.startsWith("-")) {
throw new CliException(LEGACY_OVERRIDE_EXCEPTION_MSG);
}
final String target = line.replaceFirst("-{1,2}", "");
final int nextIndex = i + 1;
if (nextIndex > (unmatched.size() - 1)) {
throw new CliException(LEGACY_OVERRIDE_EXCEPTION_MSG);
}
final String value = unmatched.get(nextIndex);
try {
overrideConfigValue(target, value);
} catch (CliException ex) {
throw new CliException(String.join("\n", LEGACY_OVERRIDE_EXCEPTION_MSG, ex.getMessage()));
}
}
}
if (recover) {
config.setRecoveryMode(true);
}
final Set<ConstraintViolation<Config>> violations = validator.validate(config);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
keyPasswordResolver.resolveKeyPasswords(config);
pidFileMixin.createPidFile();
serverURIOutputPath.updateConfig(config);
return new CliResult(0, false, config);
}
Aggregations