From 5f5a95c82e137a8ef31578a1a600b2b2597671ff Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Thu, 27 Jul 2023 11:03:08 -0600 Subject: [PATCH 1/5] Fail fast when template directory already exists --- .../amazon/smithy/cli/InitCommandTest.java | 25 +++++++++++++++++++ .../smithy/cli/commands/InitCommand.java | 15 +++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) 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..a868604a43e 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 @@ -13,6 +13,7 @@ import java.nio.file.Paths; import java.util.Collections; import java.util.List; +import java.util.logging.Level; public class InitCommandTest { private static final String PROJECT_NAME = "smithy-templates"; @@ -208,6 +209,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..f9fe315472e 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 RuntimeException("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); From 079916da550107b1493a6c92b410e0430d39d842 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Thu, 27 Jul 2023 11:05:56 -0600 Subject: [PATCH 2/5] Remove unused import --- .../src/it/java/software/amazon/smithy/cli/InitCommandTest.java | 1 - 1 file changed, 1 deletion(-) 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 a868604a43e..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 @@ -13,7 +13,6 @@ import java.nio.file.Paths; import java.util.Collections; import java.util.List; -import java.util.logging.Level; public class InitCommandTest { private static final String PROJECT_NAME = "smithy-templates"; From d5b7269592f741fe5444fb72fdb87d415c95dc94 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Thu, 27 Jul 2023 11:27:05 -0600 Subject: [PATCH 3/5] Correct checkstyle error --- .../java/software/amazon/smithy/cli/commands/InitCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f9fe315472e..47ae3d90555 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 @@ -166,7 +166,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem } final Path dest = Paths.get(directory); if (Files.exists(dest)) { - throw new RuntimeException("Output directory `" + directory + "` already exists." ); + throw new RuntimeException("Output directory `" + directory + "` already exists."); } ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode); From 9ba38e5d01bd8293a8643aff38bf33dd52620b4e Mon Sep 17 00:00:00 2001 From: Hunter Mellema <124718352+hpmellema@users.noreply.github.com> Date: Thu, 27 Jul 2023 15:27:37 -0600 Subject: [PATCH 4/5] Update smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java Co-authored-by: Michael Dowling --- .../java/software/amazon/smithy/cli/commands/InitCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 47ae3d90555..82e139bcdcb 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 @@ -166,7 +166,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem } final Path dest = Paths.get(directory); if (Files.exists(dest)) { - throw new RuntimeException("Output directory `" + directory + "` already exists."); + throw new CliException("Output directory `" + directory + "` already exists."); } ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode); From c36fc8dce78ebce7f86546d84b6383c78769cf83 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Thu, 27 Jul 2023 15:59:35 -0600 Subject: [PATCH 5/5] Update error type --- .../java/software/amazon/smithy/cli/commands/InitCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 82e139bcdcb..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 @@ -166,7 +166,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem } final Path dest = Paths.get(directory); if (Files.exists(dest)) { - throw new CliException("Output directory `" + directory + "` already exists."); + throw new CliError("Output directory `" + directory + "` already exists."); } ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode);