forked from wallix/awless
/
marshal.go
182 lines (154 loc) · 4.11 KB
/
marshal.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package template
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/oklog/ulid"
"github.com/wallix/awless/template/internal/ast"
)
// Allow template executions serialization with context for JSON storage
// without altering the template.Template model
type TemplateExecution struct {
*Template
Author, Source, Locale string
Profile, Path, Message string
Fillers map[string]interface{}
}
// Date extract the date from the ulid template identifier
func (t *TemplateExecution) Date() time.Time {
parsed, err := ulid.Parse(t.ID)
if err != nil {
panic(err)
}
return time.Unix(int64(parsed.Time())/int64(1000), time.Nanosecond.Nanoseconds())
}
func (t *TemplateExecution) IsOneLiner() bool {
var count int
for range t.CommandNodesIterator() {
count++
}
return count == 1
}
const maxMsgLen = 140
// SetMessage set the value of Message, truncating it if exceeds max len
func (t *TemplateExecution) SetMessage(s string) {
out := strings.TrimSpace(s)
if len(out) > maxMsgLen {
out = out[:maxMsgLen-3] + "..."
}
t.Message = out
}
func (t *TemplateExecution) MarshalJSON() ([]byte, error) {
out := &toJSON{}
out.ID = t.ID
out.Author = t.Author
out.Source = t.Source
out.Locale = t.Locale
out.Profile = t.Profile
out.Message = t.Message
out.Path = t.Path
out.Fillers = t.Fillers
if out.Fillers == nil {
out.Fillers = make(map[string]interface{}, 0) // friendlier for json, avoiding "fillers": null,
}
out.Commands = []command{}
for _, cmd := range t.CommandNodesIterator() {
newCmd := command{}
newCmd.Line = cmd.String()
if cmd.CmdErr != nil {
newCmd.Errors = append(newCmd.Errors, cmd.CmdErr.Error())
}
if cmd.CmdResult != nil {
if s, ok := cmd.CmdResult.(string); ok {
newCmd.Results = append(newCmd.Results, s)
}
}
out.Commands = append(out.Commands, newCmd)
}
return json.MarshalIndent(out, "", " ")
}
func (t *TemplateExecution) UnmarshalJSON(b []byte) error {
if t == nil {
t = new(TemplateExecution)
}
if t.Template == nil {
t.Template = new(Template)
}
var v toJSON
if err := json.Unmarshal(b, &v); err != nil {
return err
}
t.Source = v.Source
t.Locale = v.Locale
t.Profile = v.Profile
t.Message = v.Message
t.Path = v.Path
t.Author = v.Author
t.Fillers = v.Fillers
tpl := &Template{ID: v.ID, AST: &ast.AST{
Statements: make([]*ast.Statement, 0),
}}
for _, c := range v.Commands {
node, err := parseStatement(c.Line)
if err != nil {
return err
}
switch node.(type) {
case *ast.CommandNode:
n := node.(*ast.CommandNode)
if len(c.Results) > 0 {
n.CmdResult = c.Results[0]
}
if len(c.Errors) > 0 {
n.CmdErr = errors.New(c.Errors[0])
}
tpl.Statements = append(tpl.Statements, &ast.Statement{Node: n})
}
}
*(t.Template) = *tpl
return nil
}
type TemplateExecutionStats struct {
KOCount, OKCount, CmdCount int
ActionEntityCount map[string]int
Oneliner string
}
func (te *TemplateExecutionStats) AllKO() bool {
return te.KOCount == te.CmdCount
}
func (t *TemplateExecution) Stats() *TemplateExecutionStats {
stats := &TemplateExecutionStats{ActionEntityCount: make(map[string]int)}
var actionentity string
for _, cmd := range t.CommandNodesIterator() {
actionentity = fmt.Sprintf("%s %s", cmd.Action, cmd.Entity)
if cmd.Err() != nil {
stats.KOCount++
} else {
stats.OKCount++
}
stats.CmdCount++
stats.ActionEntityCount[actionentity]++
}
if stats.CmdCount == 1 {
stats.Oneliner = actionentity
}
return stats
}
type toJSON struct {
ID string `json:"id"`
Author string `json:"author,omitempty"`
Source string `json:"source"`
Locale string `json:"locale"`
Profile string `json:"profile,omitempty"`
Message string `json:"message,omitempty"`
Path string `json:"path,omitempty"`
Fillers map[string]interface{} `json:"fillers"`
Commands []command `json:"commands"`
}
type command struct {
Line string `json:"line"`
Errors []string `json:"errors,omitempty"`
Results []string `json:"results,omitempty"`
}