-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcmd_diff.go
More file actions
178 lines (152 loc) · 4.66 KB
/
Copy pathcmd_diff.go
File metadata and controls
178 lines (152 loc) · 4.66 KB
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
package ecschedule
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"sync/atomic"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchevents"
"github.com/goccy/go-yaml"
)
type diffResult struct {
ruleName string
diffOutput string
validationErrors []string
}
var cmdDiff = &runnerImpl{
name: "diff",
description: "diff of the rule with remote",
run: func(ctx context.Context, argv []string, outStream, errStream io.Writer) (err error) {
fs := flag.NewFlagSet("ecschedule diff", flag.ContinueOnError)
fs.SetOutput(errStream)
var (
conf = fs.String("conf", "", "configuration")
rule = fs.String("rule", "", "rule")
all = fs.Bool("all", false, "diff all rules")
unified = fs.Bool("u", false, "output in unified diff format (colored, similar to git diff)")
noColor = fs.Bool("no-color", false, "disable colored output (Unified diff format only)")
prune = fs.Bool("prune", false, "detect orphaned rules for deletion")
validate = fs.Bool("validate", false, "perform validation (env, tfstate, ssm, task definition)")
parallel = fs.Int("parallel", 1, "number of parallel workers (default: 1, recommended: 1-10 due to AWS API rate limits. Note: output order is not guaranteed when parallel > 1)")
)
if err := fs.Parse(argv); err != nil {
return err
}
setupColor(*noColor)
if !*all && *rule == "" {
return errors.New("-rule or -all option required")
}
if *prune && !*all {
return errors.New("-prune can only be used with -all flag")
}
if *parallel < 1 {
return errors.New("-parallel must be at least 1")
}
a := getApp(ctx)
c := a.Config
if *conf != "" {
f, err := os.Open(*conf)
if err != nil {
return err
}
defer f.Close()
c, err = LoadConfig(ctx, f, a.AccountID, *conf)
if err != nil {
return err
}
}
if c == nil {
return errors.New("-conf option required")
}
var ruleNames []string
if !*all {
ruleNames = append(ruleNames, *rule)
} else {
for _, r := range c.Rules {
ruleNames = append(ruleNames, r.Name)
}
}
format := selectDiffFormat(*unified)
// Create AWS client once before starting workers
svc := cloudwatchevents.NewFromConfig(a.AwsConf, func(o *cloudwatchevents.Options) {
o.Region = c.Region
})
var hasValidationError atomic.Bool
processDiffJob := func(ctx context.Context, ruleName string) (diffResult, error) {
result := diffResult{ruleName: ruleName}
ru := c.GetRuleByName(ruleName)
if ru == nil {
return result, fmt.Errorf("no rules found for %s", ruleName)
}
if *validate {
if err := ru.validateEnv(); err != nil {
result.validationErrors = append(result.validationErrors, fmt.Sprintf(" env: %s", err))
}
if err := ru.validateTFstate(); err != nil {
result.validationErrors = append(result.validationErrors, fmt.Sprintf(" tfstate: %s", err))
}
if err := ru.validateSSM(); err != nil {
result.validationErrors = append(result.validationErrors, fmt.Sprintf(" ssm: %s", err))
}
if err := ru.validateTaskDefinition(ctx, a.AwsConf); err != nil {
result.validationErrors = append(result.validationErrors, fmt.Sprintf(" task definition: %s", err))
}
if len(result.validationErrors) > 0 {
hasValidationError.Store(true)
}
}
from, to, err := ru.diff(ctx, svc)
if err != nil {
return result, err
}
result.diffOutput = formatDiff(ruleName, from, to, format)
return result, nil
}
results, errChan := executeJobsInParallel[diffResult](ctx, ruleNames, *parallel, processDiffJob)
for result := range results {
if len(result.validationErrors) > 0 {
log.Printf("❌ %q: validation failed", result.ruleName)
for _, verr := range result.validationErrors {
log.Println(verr)
}
}
if result.diffOutput != "" {
if *unified {
fmt.Fprintln(errStream, result.diffOutput)
} else {
log.Printf("💡 diff of the rule %q\n%s", result.ruleName, result.diffOutput)
}
}
}
err = <-errChan
if err != nil {
return err
}
// Display orphaned rules if -prune is specified
if *prune {
orphanedRules, err := extractOrphanedRules(ctx, a.AwsConf, c.BaseConfig, ruleNames)
if err != nil {
return err
}
for _, rule := range orphanedRules {
remoteRuleYaml, err := yaml.Marshal(rule)
if err != nil {
return err
}
diffOutput := formatDiff(rule.Name, string(remoteRuleYaml), "", format)
if *unified {
fmt.Fprintln(errStream, diffOutput)
} else {
log.Printf("🪓 orphaned rule will be deleted\n%s", diffOutput)
}
}
}
if hasValidationError.Load() {
return errors.New("validation failed for one or more rules")
}
return nil
},
}