/
trigger.go
103 lines (96 loc) · 2.73 KB
/
trigger.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright © Solus Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package triggers
import (
"log/slog"
"github.com/getsolus/usysconf/state"
)
// Trigger contains all the information for a configuration to be executed and output to the user.
type Trigger struct {
Name string
Path string
Output []Output
Description string `toml:"description"`
Check *Check `toml:"check,omitempty"`
Skip *Skip `toml:"skip,omitempty"`
Deps *Deps `toml:"deps,omitempty"`
Env map[string]string `toml:"env,omitempty"`
Bins []Bin `toml:"bins,omitempty"`
Removals []Remove `toml:"remove,omitempty"`
}
// Run will process a single configuration and scope.
func (t *Trigger) Run(s Scope, prev, next state.Map) (ok bool) {
var check, diff state.Map
// Get the new check result
if check, ok = t.CheckMatch(); !ok {
goto FINISH
}
// Calculate Diff
diff = prev.Diff(check)
// Merge it into the new State
next.Merge(diff)
// Check for Skip
if t.ShouldSkip(s, check, diff) {
goto FINISH
}
// Do the removals
if ok = t.Remove(s); !ok {
goto FINISH
}
// Run the bins
t.ExecuteBins(s)
FINISH:
t.Finish(s)
return
}
// Finish is the last function to be executed by any trigger to output details to the user.
func (t *Trigger) Finish(s Scope) {
// Check for the worst status
status := Skipped
for _, out := range t.Output {
if out.Status > status {
status = out.Status
}
}
// Indicate the worst status for the whole group
switch status {
case Skipped:
slog.Debug(t.Name)
case Failure:
slog.Error(t.Name)
case Success:
slog.Info(t.Name)
}
// Indicate status for sub-tasks
for _, out := range t.Output {
switch out.Status {
case Skipped:
if len(out.SubTask) > 0 {
slog.Debug("Skipped", "subtask", out.SubTask, "reason", out.Message)
} else if len(out.Message) > 0 {
slog.Debug("Skipped", "reason", out.Message)
}
case Failure:
if len(out.SubTask) > 0 {
slog.Error("Failed", "subtask", out.SubTask, "reason", out.Message)
} else if len(out.Message) > 0 {
slog.Error("Failed", "reason", out.Message)
}
case Success:
if s.DryRun && len(out.SubTask) > 0 {
slog.Info(out.SubTask)
}
}
}
}