forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_run.go
63 lines (61 loc) · 1.34 KB
/
cmd_run.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
package ecschedule
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
)
var cmdRun = &runnerImpl{
name: "run",
description: "run the rule",
run: func(ctx context.Context, argv []string, outStream, errStream io.Writer) (err error) {
fs := flag.NewFlagSet("ecschedule run", flag.ContinueOnError)
fs.SetOutput(errStream)
var (
conf = fs.String("conf", "", "configuration")
rule = fs.String("rule", "", "rule")
dryRun = fs.Bool("dry-run", false, "dry run")
// noWait = fs.Bool("no-wait", false, "exit immediately after starting the rule")
)
if err := fs.Parse(argv); err != nil {
return err
}
if *rule == "" {
return errors.New("-rule 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
}
}
ru := c.GetRuleByName(*rule)
if ru == nil {
return fmt.Errorf("no rules found for %s", *rule)
}
var dryRunSuffix string
if *dryRun {
dryRunSuffix = " (dry-run)"
}
log.Printf("running the rule %q%s", *rule, dryRunSuffix)
defer func() {
if err == nil {
log.Printf("✅ ran the rule %q%s", ru.Name, dryRunSuffix)
}
}()
if *dryRun {
return nil
}
return ru.Run(ctx, a.Session, true)
},
}