Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): app init template for golang #13840

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 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,12 @@
# Welcome to your CDK Go project!
eladb marked this conversation as resolved.
Show resolved Hide resolved

This is a blank project for Go development with CDK.

The `cdk.json` file tells the CDK CLI how to execute your app.

## 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