Skip to content

Commit

Permalink
feat(cli): app init template for golang (#13840)
Browse files Browse the repository at this point in the history
Introduce an initial `cdk init` project template for Go.

The template includes a single stack with an SNS topic, `cdk.json` and a simple unit test.

Output example: https://github.com/eladb/hello-go-cdk/tree/go-init-template

Resolves aws/jsii#2678


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Elad Ben-Israel committed Mar 29, 2021
1 parent aa9fd9c commit 41fd42b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/aws-cdk/lib/init-templates/v1/app/go/%name%.template.go
Original file line number Diff line number Diff line change
@@ -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")),
// }
}
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions packages/aws-cdk/lib/init-templates/v1/app/go/.template.gitignore
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions packages/aws-cdk/lib/init-templates/v1/app/go/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "go mod download && go run %name%.go"
}
13 changes: 13 additions & 0 deletions packages/aws-cdk/lib/init-templates/v1/app/go/go.template.mod
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions packages/aws-cdk/test/integ/init/test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 26 additions & 0 deletions packages/aws-cdk/test/integ/init/test-go.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 41fd42b

Please sign in to comment.