diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/%name%.template.go b/packages/aws-cdk/lib/init-templates/v1/app/go/%name%.template.go new file mode 100644 index 0000000000000..24a2b0e90b616 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/%name%.template.go @@ -0,0 +1,68 @@ +package main + +import ( + "github.com/aws/aws-cdk-go/awscdk" + "github.com/aws/aws-cdk-go/awscdk/awssns" + "github.com/aws/constructs-go/constructs/v3" + "github.com/aws/jsii-runtime-go" +) + +type %name.PascalCased%StackProps struct { + awscdk.StackProps +} + +func New%name.PascalCased%Stack(scope constructs.Construct, id string, props *%name.PascalCased%StackProps) awscdk.Stack { + var sprops awscdk.StackProps + if props != nil { + sprops = props.StackProps + } + stack := awscdk.NewStack(scope, &id, &sprops) + + // The code that defines your stack goes here + + // as an example, here's how you would define an AWS SNS topic: + awssns.NewTopic(stack, jsii.String("MyTopic"), &awssns.TopicProps{ + DisplayName: jsii.String("MyCoolTopic"), + }) + + return stack +} + +func main() { + app := awscdk.NewApp(nil) + + New%name.PascalCased%Stack(app, "%name.PascalCased%Stack", &%name.PascalCased%StackProps{ + awscdk.StackProps{ + Env: env(), + }, + }) + + app.Synth(nil) +} + +// env determines the AWS environment (account+region) in which our stack is to +// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html +func env() *awscdk.Environment { + // If unspecified, this stack will be "environment-agnostic". + // Account/Region-dependent features and context lookups will not work, but a + // single synthesized template can be deployed anywhere. + //--------------------------------------------------------------------------- + return nil + + // Uncomment if you know exactly what account and region you want to deploy + // the stack to. This is the recommendation for production stacks. + //--------------------------------------------------------------------------- + // return &awscdk.Environment{ + // Account: jsii.String("123456789012"), + // Region: jsii.String("us-east-1"), + // } + + // Uncomment to specialize this stack for the AWS Account and Region that are + // implied by the current CLI configuration. This is recommended for dev + // stacks. + //--------------------------------------------------------------------------- + // return &awscdk.Environment{ + // Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")), + // Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")), + // } +} diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/%name%_test.template.go b/packages/aws-cdk/lib/init-templates/v1/app/go/%name%_test.template.go new file mode 100644 index 0000000000000..c0534249b6282 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/%name%_test.template.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "testing" + + "github.com/aws/aws-cdk-go/awscdk" + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" +) + +func Test%name.PascalCased%Stack(t *testing.T) { + // GIVEN + app := awscdk.NewApp(nil) + + // WHEN + stack := New%name.PascalCased%Stack(app, "MyStack", nil) + + // THEN + bytes, err := json.Marshal(app.Synth(nil).GetStackArtifact(stack.ArtifactId()).Template()) + if err != nil { + t.Error(err) + } + + template := gjson.ParseBytes(bytes) + displayName := template.Get("Resources.MyTopic86869434.Properties.DisplayName").String() + assert.Equal(t, "MyCoolTopic", displayName) +} diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/.template.gitignore b/packages/aws-cdk/lib/init-templates/v1/app/go/.template.gitignore new file mode 100644 index 0000000000000..92fe1ec34b4b6 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/.template.gitignore @@ -0,0 +1,19 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# go.sum should be committed +!go.sum + +# CDK asset staging directory +.cdk.staging +cdk.out diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/README.md b/packages/aws-cdk/lib/init-templates/v1/app/go/README.md new file mode 100644 index 0000000000000..9b8d4b29f26e3 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/README.md @@ -0,0 +1,14 @@ +# Welcome to your CDK Go project! + +This is a blank project for Go development with CDK. + +**NOTICE**: Go support is still in Developer Preview. This implies that APIs may +change while we address early feedback from the community. We would love to hear +about your experience through GitHub issues. + +## Useful commands + + * `cdk deploy` deploy this stack to your default AWS account/region + * `cdk diff` compare deployed stack with current state + * `cdk synth` emits the synthesized CloudFormation template + * `go test` run unit tests diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/cdk.template.json b/packages/aws-cdk/lib/init-templates/v1/app/go/cdk.template.json new file mode 100644 index 0000000000000..ad88cd7ef75f3 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/cdk.template.json @@ -0,0 +1,3 @@ +{ + "app": "go mod download && go run %name%.go" +} \ No newline at end of file diff --git a/packages/aws-cdk/lib/init-templates/v1/app/go/go.template.mod b/packages/aws-cdk/lib/init-templates/v1/app/go/go.template.mod new file mode 100644 index 0000000000000..a1dcb391c1614 --- /dev/null +++ b/packages/aws-cdk/lib/init-templates/v1/app/go/go.template.mod @@ -0,0 +1,13 @@ +module %name% + +go 1.16 + +require ( + github.com/aws/aws-cdk-go/awscdk v%cdk-version%-devpreview + github.com/aws/constructs-go/constructs/v3 v3.3.71 + github.com/aws/jsii-runtime-go v1.26.0 + + // for testing + github.com/tidwall/gjson v1.7.4 + github.com/stretchr/testify v1.7.0 +) diff --git a/packages/aws-cdk/test/integ/init/test-all.sh b/packages/aws-cdk/test/integ/init/test-all.sh index cbca0f859feb2..f191d37bf4296 100755 --- a/packages/aws-cdk/test/integ/init/test-all.sh +++ b/packages/aws-cdk/test/integ/init/test-all.sh @@ -7,5 +7,6 @@ $scriptdir/test-java.sh $scriptdir/test-javascript.sh $scriptdir/test-python.sh $scriptdir/test-typescript.sh +$scriptdir/test-go.sh echo "SUCCESS" diff --git a/packages/aws-cdk/test/integ/init/test-go.sh b/packages/aws-cdk/test/integ/init/test-go.sh new file mode 100755 index 0000000000000..6b461f3e25ef5 --- /dev/null +++ b/packages/aws-cdk/test/integ/init/test-go.sh @@ -0,0 +1,26 @@ +#!/bin/bash +#------------------------------------------------------------------ +# setup +#------------------------------------------------------------------ +set -eu +scriptdir=$(cd $(dirname $0) && pwd) +source ${scriptdir}/common.bash + +header Go + +#------------------------------------------------------------------ + +if [[ "${1:-}" == "" ]]; then + templates="app" +else + templates="$@" +fi + +for template in $templates; do + echo "Trying Go template $template" + + setup + + cdk init -l go $template + cdk synth +done