forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): app init template for golang (aws#13840)
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
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/aws-cdk/lib/init-templates/v1/app/go/%name%.template.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")), | ||
// } | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/aws-cdk/lib/init-templates/v1/app/go/%name%_test.template.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
packages/aws-cdk/lib/init-templates/v1/app/go/.template.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
packages/aws-cdk/lib/init-templates/v1/app/go/cdk.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
packages/aws-cdk/lib/init-templates/v1/app/go/go.template.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |