Skip to content

Commit 86c0007

Browse files
committed
Use a pair of generic functions instead of aws.{To,}Type helpers
1 parent a977713 commit 86c0007

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module github.com/artyom/update-cloudformation-stack
33
go 1.23.3
44

55
require (
6-
github.com/aws/aws-sdk-go-v2 v1.32.5
76
github.com/aws/aws-sdk-go-v2/config v1.28.5
87
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.56.0
98
github.com/aws/smithy-go v1.22.1
109
)
1110

1211
require (
12+
github.com/aws/aws-sdk-go-v2 v1.32.5 // indirect
1313
github.com/aws/aws-sdk-go-v2/credentials v1.17.46 // indirect
1414
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 // indirect
1515
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24 // indirect

main.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/aws/aws-sdk-go-v2/aws"
1817
"github.com/aws/aws-sdk-go-v2/config"
1918
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
2019
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
@@ -68,13 +67,13 @@ func run(ctx context.Context, stackName string, args []string) error {
6867
stack := desc.Stacks[0]
6968
var params []types.Parameter
7069
for _, p := range stack.Parameters {
71-
k := aws.ToString(p.ParameterKey)
70+
k := unptr(p.ParameterKey)
7271
if v, ok := toReplace[k]; ok {
7372
params = append(params, types.Parameter{ParameterKey: &k, ParameterValue: &v})
7473
delete(toReplace, k)
7574
continue
7675
}
77-
params = append(params, types.Parameter{ParameterKey: &k, UsePreviousValue: aws.Bool(true)})
76+
params = append(params, types.Parameter{ParameterKey: &k, UsePreviousValue: ptr(true)})
7877
}
7978
if len(toReplace) != 0 {
8079
return fmt.Errorf("stack has no parameters with these names: %s", strings.Join(slices.Sorted(maps.Keys(toReplace)), ", "))
@@ -83,18 +82,18 @@ func run(ctx context.Context, stackName string, args []string) error {
8382
debugf("parameters to call UpdateStack with:")
8483
for _, p := range params {
8584
switch {
86-
case aws.ToBool(p.UsePreviousValue):
87-
debugf("%s (use the previous value)", aws.ToString(p.ParameterKey))
85+
case unptr(p.UsePreviousValue):
86+
debugf("%s (use the previous value)", unptr(p.ParameterKey))
8887
default:
89-
debugf("%s: %s", aws.ToString(p.ParameterKey), aws.ToString(p.ParameterValue))
88+
debugf("%s: %s", unptr(p.ParameterKey), unptr(p.ParameterValue))
9089
}
9190
}
9291

9392
token := newToken()
9493
_, err = svc.UpdateStack(ctx, &cloudformation.UpdateStackInput{
9594
StackName: &stackName,
9695
ClientRequestToken: &token,
97-
UsePreviousTemplate: aws.Bool(true),
96+
UsePreviousTemplate: ptr(true),
9897
Parameters: params,
9998
Capabilities: stack.Capabilities,
10099
NotificationARNs: stack.NotificationARNs,
@@ -120,17 +119,17 @@ func run(ctx context.Context, stackName string, args []string) error {
120119
return err
121120
}
122121
for _, evt := range page.StackEvents {
123-
if evt.Timestamp != nil && aws.ToTime(evt.Timestamp).Before(oldEventsCutoff) {
122+
if evt.Timestamp != nil && unptr(evt.Timestamp).Before(oldEventsCutoff) {
124123
break scanEvents
125124
}
126125
if evt.ClientRequestToken == nil || *evt.ClientRequestToken != token {
127126
continue
128127
}
129-
if evt.ResourceStatus == types.ResourceStatusUpdateFailed && aws.ToString(evt.ResourceStatusReason) != "Resource update cancelled" {
130-
return fmt.Errorf("%v: %s", evt.ResourceStatus, aws.ToString(evt.ResourceStatusReason))
128+
if evt.ResourceStatus == types.ResourceStatusUpdateFailed && unptr(evt.ResourceStatusReason) != "Resource update cancelled" {
129+
return fmt.Errorf("%v: %s", evt.ResourceStatus, unptr(evt.ResourceStatusReason))
131130
}
132-
debugf("%s\t%s\t%v", aws.ToString(evt.ResourceType), aws.ToString(evt.LogicalResourceId), evt.ResourceStatus)
133-
if aws.ToString(evt.LogicalResourceId) == stackName && aws.ToString(evt.ResourceType) == "AWS::CloudFormation::Stack" {
131+
debugf("%s\t%s\t%v", unptr(evt.ResourceType), unptr(evt.LogicalResourceId), evt.ResourceStatus)
132+
if unptr(evt.LogicalResourceId) == stackName && unptr(evt.ResourceType) == "AWS::CloudFormation::Stack" {
134133
switch evt.ResourceStatus {
135134
case types.ResourceStatusUpdateRollbackComplete,
136135
types.ResourceStatusRollbackFailed:
@@ -204,3 +203,12 @@ func parseKvs(list []string) (map[string]string, error) {
204203
}
205204
return out, nil
206205
}
206+
207+
func ptr[T any](v T) *T { return &v }
208+
func unptr[T any](v *T) T {
209+
var zero T
210+
if v != nil {
211+
return *v
212+
}
213+
return zero
214+
}

0 commit comments

Comments
 (0)