forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_diff.go
73 lines (69 loc) · 1.65 KB
/
cmd_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
package ecschedule
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/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
}
}
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)
}
sess := a.Session
svc := cloudwatchevents.New(sess, &aws.Config{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
},
}