diff --git a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java index 46252aca092..34961006874 100644 --- a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java +++ b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java @@ -208,6 +208,30 @@ public void withListArg() { }); } + @Test + public void outputDirectoryAlreadyExists() { + IntegUtils.withProject(PROJECT_NAME, templatesDir -> { + setupTemplatesDirectory(templatesDir); + + IntegUtils.withTempDir("existingOutputDir", dir -> { + Path existingPath = null; + RunResult result; + try { + existingPath = Files.createDirectory(dir.resolve("quickstart-cli")); + result = IntegUtils.run( + dir, ListUtils.of("init", "-t", "quickstart-cli", "-u", templatesDir.toString())); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + IoUtils.rmdir(existingPath); + } + + assertThat(result.getOutput(), containsString("Output directory `quickstart-cli` already exists.")); + assertThat(result.getExitCode(), is(1)); + }); + }); + } + private static void run(List args, Path root) { StringBuilder output = new StringBuilder(); int result = IoUtils.runCommand(args, root, output, Collections.emptyMap()); diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java index 2a78b21bbde..5adac8b1b03 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java @@ -160,6 +160,15 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem throw new IllegalArgumentException("Please specify a template name using `--template` or `-t`"); } + // Use templateName if directory is not specified + if (directory == null) { + directory = template; + } + final Path dest = Paths.get(directory); + if (Files.exists(dest)) { + throw new CliError("Output directory `" + directory + "` already exists."); + } + ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode); if (!templatesNode.containsMember(template)) { throw new IllegalArgumentException(String.format( @@ -180,12 +189,8 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem } exec(ListUtils.of("git", "checkout"), temp); - // Use templateName if directory is not specified - if (directory == null) { - directory = template; - } - final Path dest = Paths.get(directory); + IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest); copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env);