Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema committed Jun 20, 2023
1 parent 0a0f3c9 commit 3753ac2
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public List<ValidationEvent> validate(Model model) {
// check that the resource name is consistent between the two traits
if (resource.hasTrait(ArnTrait.class)) {
String resourceName = resource.expectTrait(IamResourceTrait.class).getName()
.orElse(StringUtils.lowerCase(resource.getId().getName()));
.orElseGet(() -> StringUtils.lowerCase(resource.getId().getName()));
ArnTrait arnTrait = resource.expectTrait(ArnTrait.class);
List<String> arnComponents = parseArnComponents(arnTrait.getTemplate());
if (!arnComponents.contains(resourceName)) {
results.add(danger(resource, String.format(
"The `@aws.iam#iamResource` trait applied to this resource "
+ "defines a resource name, `%s`, that does not match the `@arn` template, "
+ "`%s`, of the resource.",
"The `@aws.iam#iamResource trait applied to the resource "
+ "defines an IAM resource name, `%s`, that does not match the `@arn` template, "
+ "`%s`, for that same resource.",
resourceName, arnTrait.getTemplate())));
}
}
Expand All @@ -57,6 +57,6 @@ public List<ValidationEvent> validate(Model model) {
}

private List<String> parseArnComponents(String arnTemplate) {
return new ArrayList<>(Arrays.asList(arnTemplate.split("/")));
return Arrays.asList(arnTemplate.split("/"));
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
software.amazon.smithy.aws.iam.traits.ConditionKeysValidator
software.amazon.smithy.aws.iam.traits.IamResourceTraitValidator
software.amazon.smithy.aws.iam.traits.IamResourceTraitValidator
35 changes: 35 additions & 0 deletions smithy-cli/clone-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

#REPO=ssh://git.amazon.com/pkg/Smithy-Examples
#TEMPLATE_PATH=templates/common-shapes

REPO=$1
TEMPLATE=$2
TEMPLATE_PATH="templates/$TEMPLATE"
RELATIVE_PREFIX=../

# Check whether current directory is a Git repo
if git rev-parse --is-inside-work-tree >& /dev/null; then
printf '%s\n' "Cannot clone template into an existing Git repository" >&2
exit 1
fi

rm -rf Smithy-Examples
git clone --filter=blob:none --no-checkout --depth 1 --sparse $REPO
cd Smithy-Examples
git sparse-checkout set --no-cone $TEMPLATE_PATH
git checkout

SYMLINK_PATH=$(readlink $TEMPLATE_PATH)
if [ -z ${SYMLINK_PATH+x} ]; then
echo "no symlink found. continuing";
else
echo "symlink found: '$SYMLINK_PATH'";
TEMPLATE_PATH=${SYMLINK_PATH#$RELATIVE_PREFIX}
git sparse-checkout set --no-cone $TEMPLATE_PATH
git checkout
fi

rm -rf "../$TEMPLATE"
mv $TEMPLATE_PATH "../$TEMPLATE"
rm -rf ../Smithy-Examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.cli.commands;

import software.amazon.smithy.cli.ArgumentReceiver;
import software.amazon.smithy.cli.Arguments;
import software.amazon.smithy.cli.Command;
import software.amazon.smithy.cli.HelpPrinter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class InitCommand implements Command {

private static final Logger LOGGER = Logger.getLogger(InitCommand.class.getName());

private final String parentCommandName;

public InitCommand(String parentCommandName) {
this.parentCommandName = parentCommandName;
}

@Override
public String getName() {
return "init";
}

@Override
public String getSummary() {
return "Init a smithy workspace by template";
}

@Override
public int execute(Arguments arguments, Env env) {
arguments.addReceiver(new Options());
CommandAction action = HelpActionWrapper.fromCommand(this, parentCommandName, this::run);
return action.apply(arguments, env);
}

public void executeScript(String repoUrl, String template) throws IOException, InterruptedException {
String command = String.format("sh /Volumes/workplace/smithy/smithy-cli/clone-template.sh %s %s", repoUrl, template);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();

BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));

String line = "";
while ((line = errorReader.readLine()) != null) {
LOGGER.log(Level.INFO, line);
}
}

private int run(Arguments arguments, Env env) {
Options options = arguments.getReceiver(Options.class);
try {
this.executeScript(options.repoUrl, options.template);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}

return 0;
}

private static final class Options implements ArgumentReceiver {
private String template;

private String repoUrl = "ssh://git.amazon.com/pkg/Smithy-Examples";

@Override
public boolean testOption(String name) {
return false;
}

@Override
public Consumer<String> testParameter(String name) {
switch (name) {
case "--template":
return value -> template = value;
case "--url":
return value -> repoUrl = value;
default:
return value -> {
};
}
}

@Override
public void registerHelp(HelpPrinter printer) {
printer.param("--template", null, "",
"template name");
printer.param("--url", null, "",
"repo url");
}
}
}

0 comments on commit 3753ac2

Please sign in to comment.