Skip to content

Commit

Permalink
Update init command to honor quiet setting (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema authored Jul 28, 2023
1 parent 1f80a10 commit 8db9d56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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 @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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`");
}
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 8db9d56

Please sign in to comment.