EncryptConfigValueCommand extends Command { @Override public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception { String keyfile = namespace.getString(KEYFILE); String value = namespace.getString(VALUE); KeyWithType keyWithType = KeyFileUtils.keyWithTypeFromPath(Paths.get(keyfile)); Algorithm algorithm = keyWithType.getType().getAlgorithm(); EncryptedValue encryptedValue = algorithm.newEncrypter().encrypt(keyWithType, value); System.out.println(encryptedValue); } protected  EncryptConfigValueCommand(); @Override void configure(Subparser subparser); @Override void run(Bootstrap<?> bootstrap, Namespace namespace); static final String KEYFILE; static final String VALUE; }
@Test(expected = NoSuchFileException.class) public void weFailIfTheKeyfileDoesNotExist() throws Exception { Path tempFilePath = Files.createTempDirectory("temp-key-directory").resolve("test.key"); Namespace namespace = new Namespace(ImmutableMap.of( EncryptConfigValueCommand.KEYFILE, tempFilePath.toString(), EncryptConfigValueCommand.VALUE, plaintext)); command.run(null, namespace); }