-
Notifications
You must be signed in to change notification settings - Fork 23
/
cmd_apply.go
87 lines (72 loc) · 1.8 KB
/
cmd_apply.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
type CmdApply struct{}
func init() {
_, err := parser.AddCommand("apply",
"apply a rule",
"The apply command configures the outputs as described in the given",
&CmdApply{})
if err != nil {
panic(err)
}
}
func (cmd CmdApply) Usage() string {
return "RULE"
}
func ApplyRule(outputs Outputs, rule Rule) error {
var cmds []*exec.Cmd
var err error
switch {
case rule.ConfigureSingle != "" || len(rule.ConfigureRow) > 0 || len(rule.ConfigureColumn) > 0:
cmds, err = BuildCommandOutputRow(rule, outputs)
case rule.ConfigureCommand != "":
cmds = []*exec.Cmd{exec.Command("sh", "-c", rule.ConfigureCommand)}
default:
return fmt.Errorf("no output configuration for rule %v", rule.Name)
}
after := append(globalOpts.cfg.ExecuteAfter, rule.ExecuteAfter...)
if len(after) > 0 {
for _, cmd := range after {
cmds = append(cmds, exec.Command("sh", "-c", cmd))
}
}
if err != nil {
return err
}
for _, cmd := range cmds {
err = RunCommand(cmd)
if err != nil {
fmt.Fprintf(os.Stderr, "executing command for rule %v failed: %v\n", rule.Name, err)
}
}
return nil
}
func (cmd CmdApply) Execute(args []string) (err error) {
err = globalOpts.ReadConfigfile()
if err != nil {
return err
}
// install panic handler if commands are given
defer RunCommandsOnFailure(&err, globalOpts.cfg.OnFailure)()
if len(args) != 1 {
return errors.New("need exactly one rule name as the parameter")
}
outputs, err := DetectOutputs()
if err != nil {
return err
}
ruleName := strings.ToLower(args[0])
for _, rule := range globalOpts.cfg.Rules {
if strings.ToLower(rule.Name) == ruleName {
V("found matching rule (name %v)\n", rule.Name)
return ApplyRule(outputs, rule)
}
}
return fmt.Errorf("rule %q not found", ruleName)
}