From d8150542e57496d0a29304e7bf442cda53dc6702 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Mon, 3 Jul 2023 09:33:25 -0600 Subject: [PATCH 1/2] Add default template --- .../software/amazon/smithy/cli/commands/InitCommand.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) 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 ac7b67fe005..d719b1ea543 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 @@ -44,7 +44,7 @@ final class InitCommand implements Command { private static final String SMITHY_TEMPLATE_JSON = "smithy-templates.json"; private static final String DEFAULT_REPOSITORY_URL = "https://github.com/smithy-lang/smithy-examples.git"; - + private static final String DEFAULT_TEMPLATE_NAME = "quickstart-cli"; private static final String DOCUMENTATION = "documentation"; private static final String NAME = "name"; @@ -156,10 +156,6 @@ private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) { private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String template, String directory, Env env) throws IOException, InterruptedException, URISyntaxException { - if (template == null || template.isEmpty()) { - throw new IllegalArgumentException("Please specify a template using `--template` or `-t`"); - } - ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode); if (!templatesNode.containsMember(template)) { throw new IllegalArgumentException(String.format( @@ -275,7 +271,7 @@ private static String pad(String s, int n) { } private static final class Options implements ArgumentReceiver { - private String template; + private String template = DEFAULT_TEMPLATE_NAME; private String directory; private Boolean listTemplates = false; private String repositoryUrl = DEFAULT_REPOSITORY_URL; From 829766b0e829a0df1778945a798d9671fa4e1762 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Mon, 3 Jul 2023 09:59:26 -0600 Subject: [PATCH 2/2] Update smithy-init integration tests --- .../amazon/smithy/cli/InitCommandTest.java | 19 +++++++++++++------ .../smithy/cli/commands/InitCommand.java | 4 ++++ 2 files changed, 17 insertions(+), 6 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 81fd5dfab09..46252aca092 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 @@ -34,23 +34,30 @@ public void init() { } @Test - public void missingTemplate() { + public void usesDefaultTemplate() { IntegUtils.withProject(PROJECT_NAME, templatesDir -> { setupTemplatesDirectory(templatesDir); - IntegUtils.withTempDir("missingTemplate", dir -> { + IntegUtils.withTempDir("defaultTemplate", dir -> { RunResult result = IntegUtils.run( - dir, ListUtils.of("init", "-u", templatesDir.toString())); + dir, ListUtils.of("init", "-u", templatesDir.toString())); assertThat(result.getOutput(), - containsString("Please specify a template using `--template` or `-t`")); - assertThat(result.getExitCode(), is(1)); + containsString("Smithy project created in directory: quickstart-cli")); + assertThat(result.getExitCode(), is(0)); }); + }); + } + + @Test + public void missingTemplate() { + IntegUtils.withProject(PROJECT_NAME, templatesDir -> { + setupTemplatesDirectory(templatesDir); IntegUtils.withTempDir("emptyTemplateName", dir -> { RunResult result = IntegUtils.run( dir, ListUtils.of("init", "-t", "", "-u", templatesDir.toString())); assertThat(result.getOutput(), - containsString("Please specify a template using `--template` or `-t`")); + containsString("Please specify a template name using `--template` or `-t`")); assertThat(result.getExitCode(), is(1)); }); }); 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 d719b1ea543..2a78b21bbde 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 @@ -156,6 +156,10 @@ private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) { private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, String template, String directory, Env env) throws IOException, InterruptedException, URISyntaxException { + if (template == null || template.isEmpty()) { + throw new IllegalArgumentException("Please specify a template name using `--template` or `-t`"); + } + ObjectNode templatesNode = getTemplatesNode(smithyTemplatesNode); if (!templatesNode.containsMember(template)) { throw new IllegalArgumentException(String.format(