GenerateKeyCommand extends Command { @Override public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception { String algorithmType = namespace.getString(ALGORITHM); String file = namespace.getString(FILE); Path path = Paths.get(file); Algorithm algorithm = Algorithm.valueOf(algorithmType); KeyPair keyPair = algorithm.newKeyPair(); KeyPairFiles keyPairFiles = KeyFileUtils.keyPairToFile(keyPair, path); System.out.println("Wrote key to " + path); if (!keyPairFiles.pathsEqual()) { System.out.println("Wrote private key to " + keyPairFiles.decryptionKeyFile()); } } protected  GenerateKeyCommand(); @Override void configure(Subparser subparser); @Override void run(Bootstrap<?> bootstrap, Namespace namespace); static final String FILE; static final String ALGORITHM; }
@Test(expected = FileAlreadyExistsException.class) public void weDoNotOverwriteAnExistingKeyfile() throws Exception { Path tempFilePath = Files.createTempDirectory("temp-key-directory").resolve("test.key"); String algorithm = "AES"; Files.createFile(tempFilePath); Namespace namespace = new Namespace(ImmutableMap.of( GenerateKeyCommand.ALGORITHM, algorithm, GenerateKeyCommand.FILE, tempFilePath.toString())); command.run(null, namespace); }
@Test(expected = FileAlreadyExistsException.class) public void weDoNotOverwriteAnExistingKeyfile() throws Exception { Path tempFilePath = Files.createTempDirectory("temp-key-directory").resolve("test.key"); String algorithm = "AES"; Files.createFile(tempFilePath); Namespace namespace = new Namespace(ImmutableMap.<String, Object>of( GenerateKeyCommand.ALGORITHM, algorithm, GenerateKeyCommand.FILE, tempFilePath.toString())); command.run(null, namespace); }