-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresult.go
160 lines (139 loc) · 3.65 KB
/
result.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package action
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/file/diff"
"projectforge.dev/projectforge/app/module"
"projectforge.dev/projectforge/app/project"
"projectforge.dev/projectforge/app/util"
)
type Result struct {
Project *project.Project `json:"project"`
Action Type `json:"action"`
Status string `json:"status"`
Args util.ValueMap `json:"args,omitempty"`
Data any `json:"data,omitempty"`
Modules module.Results `json:"modules,omitempty"`
Logs []string `json:"logs,omitempty"`
Errors []string `json:"errors,omitempty"`
Duration int `json:"duration,omitempty"`
logger util.Logger
}
func newResult(act Type, prj *project.Project, cfg util.ValueMap, logger util.Logger) *Result {
return &Result{Project: prj, Action: act, Args: cfg, Status: "OK", logger: logger}
}
func (r *Result) WithError(err error) *Result {
msg := "error encountered"
if err != nil {
msg = err.Error()
}
if r.logger != nil {
r.logger.Warnf("action error: %+v", err.Error())
}
r.Status = util.KeyError
r.Errors = append(r.Errors, msg)
return r
}
func (r *Result) AddDebug(msg string, args ...any) {
ret := fmt.Sprintf(msg, args...)
if r.logger != nil {
r.logger.Debug(ret)
}
r.Logs = append(r.Logs, ret)
}
func (r *Result) AddLog(msg string, args ...any) {
ret := fmt.Sprintf(msg, args...)
if r.logger != nil {
r.logger.Info(ret)
}
r.Logs = append(r.Logs, ret)
}
func (r *Result) AddWarn(msg string, args ...any) {
ret := fmt.Sprintf(msg, args...)
if r.logger != nil {
r.logger.Warn(ret)
}
r.Logs = append(r.Logs, ret)
}
func (r *Result) Merge(tgt *Result) *Result {
status := r.Status
if status == "" {
status = tgt.Status
}
logger := r.logger
if logger == nil {
logger = tgt.logger
}
return &Result{
Status: status,
Args: r.Args.Merge(tgt.Args),
Modules: append(append(module.Results{}, r.Modules...), tgt.Modules...),
Logs: append(append([]string{}, r.Logs...), tgt.Logs...),
Errors: append(append([]string{}, r.Errors...), tgt.Errors...),
Duration: r.Duration + tgt.Duration,
logger: logger,
}
}
func (r *Result) Title() string {
return r.Action.Title
}
func (r *Result) HasErrors() bool {
return len(r.Errors) > 0
}
func (r *Result) AsError() error {
if r.HasErrors() {
return errors.New(strings.Join(r.Errors, "; "))
}
return nil
}
func (r *Result) StatusLog() string {
fileCount := 0
lo.ForEach(r.Modules, func(m *module.Result, _ int) {
lo.ForEach(m.Diffs, func(d *diff.Diff, _ int) {
if d.Status != diff.StatusSkipped {
fileCount++
}
})
})
if fileCount == 0 {
return "<em>no changes</em>"
}
return util.StringPlural(fileCount, "change")
}
type ResultContext struct {
Prj *project.Project `json:"prj,omitempty"`
Cfg util.ValueMap `json:"cfg,omitempty"`
Res *Result `json:"res,omitempty"`
}
func (c *ResultContext) Status() string {
if c.Res == nil {
return "<strong>missing</strong>"
}
return c.Res.StatusLog()
}
func (c *ResultContext) Title() string {
if c.Res == nil {
return "Unknown"
}
return c.Res.Action.Title
}
type ResultContexts []*ResultContext
func (x ResultContexts) Errors() []string {
return lo.FlatMap(x, func(c *ResultContext, _ int) []string {
if c.Res == nil {
return nil
}
return c.Res.Errors
})
}
func (x ResultContexts) Title() string {
if len(x) == 0 || x[0].Res == nil {
return fmt.Sprintf("Unknown (%d results)", len(x))
}
return x[0].Res.Action.Title
}
func errorResult(err error, t Type, cfg util.ValueMap, logger util.Logger) *Result {
return newResult(t, nil, cfg, logger).WithError(err)
}