Skip to content

Commit

Permalink
chore: add go v2 init template (#14785)
Browse files Browse the repository at this point in the history
Adds go `app` init template for cdk v2. References aws-cdk-go v2
and constructs v10.

Testing: Checked out the v2-main branch and added this init template to
the CLI. Changed the version of `packages/aws-cdk/package.json` from
`0.0.0` to `2.0.0-rc.4` and ran `buildup`. Then ran `cdk init
--language=go` against local build of CLI and verified that `cdk ls` and
`cdk synth` both output correctly using both local cli and `npx
cdk@next`.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
MrArnoldPalmer authored May 20, 2021
1 parent 6179ac1 commit e478bf5
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/aws-cdk/lib/init-templates/v2/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/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awssns"
"github.com/aws/constructs-go/constructs/v10"
"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/v2"
"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/v2/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/v2/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/v2/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/v2 v%cdk-version%
github.com/aws/constructs-go/constructs/v10 v10.0.5
github.com/aws/jsii-runtime-go v1.29.0

// for testing
github.com/tidwall/gjson v1.7.4
github.com/stretchr/testify v1.7.0
)

0 comments on commit e478bf5

Please sign in to comment.