forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_diff.go
More file actions
76 lines (72 loc) · 1.71 KB
/
Copy pathcmd_diff.go
File metadata and controls
76 lines (72 loc) · 1.71 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
package ecschedule
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchevents"
"github.com/sergi/go-diff/diffmatchpatch"
)
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 apply", flag.ContinueOnError)
fs.SetOutput(errStream)
var (
conf = fs.String("conf", "", "configuration")
rule = fs.String("rule", "", "rule")
all = fs.Bool("all", false, "apply all rules")
)
if err := fs.Parse(argv); err != nil {
return err
}
if !*all && *rule == "" {
return errors.New("-rule or -all option required")
}
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)
}
}
for _, rule := range ruleNames {
ru := c.GetRuleByName(rule)
if ru == nil {
return fmt.Errorf("no rules found for %s", rule)
}
svc := cloudwatchevents.NewFromConfig(a.AwsConf, func(o *cloudwatchevents.Options) {
o.Region = c.Region
})
from, to, err := ru.diff(ctx, svc)
if err != nil {
return err
}
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(from, to, false)
log.Printf("💡 diff of the rule %q\n%s", rule, dmp.DiffPrettyText(diffs))
}
return nil
},
}