Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for existing directory when creating template with init #1885

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args, Path root) {
StringBuilder output = new StringBuilder();
int result = IoUtils.runCommand(args, root, output, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);

Expand Down