From 8db9d562fe744f511a29984f8e869f9cb070faaf Mon Sep 17 00:00:00 2001 From: Hunter Mellema <124718352+hpmellema@users.noreply.github.com> Date: Fri, 28 Jul 2023 12:38:23 -0600 Subject: [PATCH] Update init command to honor quiet setting (#1889) --- .../amazon/smithy/cli/InitCommandTest.java | 17 +++++++++++++++++ .../amazon/smithy/cli/commands/InitCommand.java | 17 +++++++++++++---- 2 files changed, 30 insertions(+), 4 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 34961006874..2667a6606af 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 @@ -2,6 +2,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.emptyString; import static org.hamcrest.Matchers.is; import org.junit.jupiter.api.Test; import software.amazon.smithy.utils.IoUtils; @@ -232,6 +233,22 @@ public void outputDirectoryAlreadyExists() { }); } + @Test + public void executesInitSuccessQuiet() { + IntegUtils.withProject(PROJECT_NAME, templatesDir -> { + setupTemplatesDirectory(templatesDir); + + IntegUtils.withTempDir("exitZeroQuiet", dir -> { + RunResult result = IntegUtils.run( + dir, ListUtils.of("init", "--quiet", "-t", "quickstart-cli", "-u", templatesDir.toString())); + + assertThat(result.getOutput().trim(), emptyString()); + assertThat(result.getExitCode(), is(0)); + assertThat(Files.exists(Paths.get(dir.toString(), "quickstart-cli")), is(true)); + }); + }); + } + 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 5adac8b1b03..8deb92fd084 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 @@ -35,6 +35,7 @@ import software.amazon.smithy.cli.ColorTheme; import software.amazon.smithy.cli.Command; import software.amazon.smithy.cli.HelpPrinter; +import software.amazon.smithy.cli.StandardOptions; import software.amazon.smithy.model.node.Node; import software.amazon.smithy.model.node.ObjectNode; import software.amazon.smithy.model.node.StringNode; @@ -83,6 +84,8 @@ public int execute(Arguments arguments, Env env) { private int run(Arguments arguments, Env env) { Options options = arguments.getReceiver(Options.class); + StandardOptions standardOptions = arguments.getReceiver(StandardOptions.class); + try { final Path root = Paths.get("."); final Path temp = Files.createTempDirectory("temp"); @@ -94,7 +97,7 @@ private int run(Arguments arguments, Env env) { if (options.listTemplates) { this.listTemplates(smithyTemplatesNode, env); } else { - this.cloneTemplate(temp, smithyTemplatesNode, options.template, options.directory, env); + this.cloneTemplate(temp, smithyTemplatesNode, options, standardOptions, env); } } catch (IOException | InterruptedException | URISyntaxException e) { throw new RuntimeException(e); @@ -153,9 +156,13 @@ private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) { return builder.toString(); } - private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String template, String directory, Env env) + private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options options, + StandardOptions standardOptions, Env env) throws IOException, InterruptedException, URISyntaxException { + String template = options.template; + String directory = options.directory; + if (template == null || template.isEmpty()) { throw new IllegalArgumentException("Please specify a template name using `--template` or `-t`"); } @@ -194,8 +201,10 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String tem IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest); copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env); - try (ColorBuffer buffer = ColorBuffer.of(env.colors(), env.stderr())) { - buffer.println(String.format("Smithy project created in directory: %s", directory), ColorTheme.SUCCESS); + if (!standardOptions.quiet()) { + try (ColorBuffer buffer = ColorBuffer.of(env.colors(), env.stderr())) { + buffer.println(String.format("Smithy project created in directory: %s", directory), ColorTheme.SUCCESS); + } } }