Skip to content

Commit 185ef3a

Browse files
hpmellemaSteven Yuan
authored and
Steven Yuan
committed
Check for existing directory when creating template with init (smithy-lang#1885)
This commit updates the Smithy Init CLI command to fail before attempting to clone the template folder when the target output directory already exists. --------- Co-authored-by: Michael Dowling <[email protected]>
1 parent c1697e4 commit 185ef3a

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ public void withListArg() {
208208
});
209209
}
210210

211+
@Test
212+
public void outputDirectoryAlreadyExists() {
213+
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
214+
setupTemplatesDirectory(templatesDir);
215+
216+
IntegUtils.withTempDir("existingOutputDir", dir -> {
217+
Path existingPath = null;
218+
RunResult result;
219+
try {
220+
existingPath = Files.createDirectory(dir.resolve("quickstart-cli"));
221+
result = IntegUtils.run(
222+
dir, ListUtils.of("init", "-t", "quickstart-cli", "-u", templatesDir.toString()));
223+
} catch (IOException e) {
224+
throw new RuntimeException(e);
225+
} finally {
226+
IoUtils.rmdir(existingPath);
227+
}
228+
229+
assertThat(result.getOutput(), containsString("Output directory `quickstart-cli` already exists."));
230+
assertThat(result.getExitCode(), is(1));
231+
});
232+
});
233+
}
234+
211235
private static void run(List<String> args, Path root) {
212236
StringBuilder output = new StringBuilder();
213237
int result = IoUtils.runCommand(args, root, output, Collections.emptyMap());

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem
160160
throw new IllegalArgumentException("Please specify a template name using `--template` or `-t`");
161161
}
162162

163+
// Use templateName if directory is not specified
164+
if (directory == null) {
165+
directory = template;
166+
}
167+
final Path dest = Paths.get(directory);
168+
if (Files.exists(dest)) {
169+
throw new CliError("Output directory `" + directory + "` already exists.");
170+
}
171+
163172
ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode);
164173
if (!templatesNode.containsMember(template)) {
165174
throw new IllegalArgumentException(String.format(
@@ -180,12 +189,8 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem
180189
}
181190
exec(ListUtils.of("git", "checkout"), temp);
182191

183-
// Use templateName if directory is not specified
184-
if (directory == null) {
185-
directory = template;
186-
}
187192

188-
final Path dest = Paths.get(directory);
193+
189194
IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest);
190195
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env);
191196

0 commit comments

Comments
 (0)