-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
diff.go
231 lines (208 loc) · 6.35 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package lambroll
import (
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"strings"
"github.com/aereal/jsondiff"
"github.com/fatih/color"
"github.com/itchyny/gojq"
"github.com/kylelemons/godebug/diff"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
)
// DiffOption represents options for Diff()
type DiffOption struct {
Src string `help:"function zip archive or src dir" default:"."`
CodeSha256 bool `name:"code" help:"diff of code sha256" default:"false"`
Qualifier *string `help:"the qualifier to compare"`
FunctionURL string `help:"path to function-url definition" default:"" env:"LAMBROLL_FUNCTION_URL"`
Ignore string `help:"ignore diff by jq query" default:""`
ZipOption
}
// Diff prints diff of function.json compared with latest function
func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
if err := opt.Expand(); err != nil {
return err
}
newFunc, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
}
fillDefaultValues(newFunc)
name := *newFunc.FunctionName
var remote *types.FunctionConfiguration
var code *types.FunctionCodeLocation
var tags Tags
var currentCodeSha256 string
var packageType types.PackageType
if res, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: &name,
Qualifier: opt.Qualifier,
}); err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
log.Printf("[info] function %s is not found. lambroll deploy will create a new function.", name)
} else {
return fmt.Errorf("failed to GetFunction %s: %w", name, err)
}
} else {
remote = res.Configuration
code = res.Code
{
log.Println("[debug] list tags Resource", app.functionArn(ctx, name))
res, err := app.lambda.ListTags(ctx, &lambda.ListTagsInput{
// Tagging operations are permitted on Lambda functions only.
// Tags on aliases and versions are not supported.
Resource: aws.String(app.functionArn(ctx, name)),
})
if err != nil {
return fmt.Errorf("failed to list tags: %w", err)
}
tags = res.Tags
}
currentCodeSha256 = *res.Configuration.CodeSha256
packageType = res.Configuration.PackageType
}
remoteFunc := newFunctionFrom(remote, code, tags)
fillDefaultValues(remoteFunc)
opts := []jsondiff.Option{}
if ignore := opt.Ignore; ignore != "" {
if p, err := gojq.Parse(ignore); err != nil {
return fmt.Errorf("failed to parse ignore query: %s %w", ignore, err)
} else {
opts = append(opts, jsondiff.Ignore(p))
}
}
remoteJSON, _ := marshalAny(remoteFunc)
newJSON, _ := marshalAny(newFunc)
remoteArn := fullQualifiedFunctionName(app.functionArn(ctx, name), opt.Qualifier)
if diff, err := jsondiff.Diff(
&jsondiff.Input{Name: remoteArn, X: remoteJSON},
&jsondiff.Input{Name: app.functionFilePath, X: newJSON},
opts...,
); err != nil {
return fmt.Errorf("failed to diff: %w", err)
} else if diff != "" {
fmt.Print(coloredDiff(diff))
}
if err := validateUpdateFunction(remote, code, newFunc); err != nil {
return err
}
if opt.CodeSha256 {
if packageType != types.PackageTypeZip {
return fmt.Errorf("code-sha256 is only supported for Zip package type")
}
zipfile, _, err := prepareZipfile(opt.Src, opt.excludes, opt.KeepSymlink)
if err != nil {
return err
}
h := sha256.New()
if _, err := io.Copy(h, zipfile); err != nil {
return err
}
newCodeSha256 := base64.StdEncoding.EncodeToString(h.Sum(nil))
prefix := "CodeSha256: "
if ds := diff.Diff(prefix+currentCodeSha256, prefix+newCodeSha256); ds != "" {
fmt.Println(color.RedString("---" + app.functionArn(ctx, name)))
fmt.Println(color.GreenString("+++" + "--src=" + opt.Src))
fmt.Println(coloredDiff(ds))
}
}
if opt.FunctionURL == "" {
return nil
}
if err := app.diffFunctionURL(ctx, name, opt); err != nil {
return err
}
return nil
}
func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOption) error {
var remote, local *types.FunctionUrlConfig
fu, err := app.loadFunctionUrl(opt.FunctionURL, name)
if err != nil {
return fmt.Errorf("failed to load function-url: %w", err)
} else {
fillDefaultValuesFunctionUrlConfig(fu.Config)
local = &types.FunctionUrlConfig{
AuthType: fu.Config.AuthType,
Cors: fu.Config.Cors,
InvokeMode: fu.Config.InvokeMode,
}
}
var qualifier *string
if opt.Qualifier != nil {
qualifier = opt.Qualifier
} else if fu.Config != nil && fu.Config.Qualifier != nil {
qualifier = fu.Config.Qualifier
}
fqName := fullQualifiedFunctionName(name, qualifier)
if res, err := app.lambda.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{
FunctionName: &name,
Qualifier: qualifier,
}); err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
// empty
remote = &types.FunctionUrlConfig{}
} else {
return fmt.Errorf("failed to get function url config: %w", err)
}
} else {
log.Println("[debug] FunctionUrlConfig found")
remote = &types.FunctionUrlConfig{
AuthType: res.AuthType,
Cors: res.Cors,
InvokeMode: res.InvokeMode,
}
}
r, _ := toGeneralMap(remote, true)
l, _ := toGeneralMap(local, true)
if diff, err := jsondiff.Diff(
&jsondiff.Input{Name: fqName, X: r},
&jsondiff.Input{Name: opt.FunctionURL, X: l},
); err != nil {
return fmt.Errorf("failed to diff: %w", err)
} else if diff != "" {
fmt.Print(coloredDiff(diff))
}
// permissions
adds, removes, err := app.calcFunctionURLPermissionsDiff(ctx, fu)
if err != nil {
return err
}
var addsB []byte
for _, in := range adds {
b, _ := marshalJSON(in)
addsB = append(addsB, b...)
}
var removesB []byte
for _, in := range removes {
b, _ := marshalJSON(in)
removesB = append(removesB, b...)
}
if ds := diff.Diff(string(removesB), string(addsB)); ds != "" {
fmt.Println(color.RedString("--- permissions"))
fmt.Println(color.GreenString("+++ permissions"))
fmt.Print(coloredDiff(ds))
}
return nil
}
func coloredDiff(src string) string {
var b strings.Builder
for _, line := range strings.Split(src, "\n") {
if strings.HasPrefix(line, "-") {
b.WriteString(color.RedString(line) + "\n")
} else if strings.HasPrefix(line, "+") {
b.WriteString(color.GreenString(line) + "\n")
} else {
b.WriteString(line + "\n")
}
}
return b.String()
}