-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.go
More file actions
133 lines (122 loc) · 3.09 KB
/
logging.go
File metadata and controls
133 lines (122 loc) · 3.09 KB
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
package mg
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
const (
severityDefault = "DEFAULT"
severityDebug = "DEBUG"
severityInfo = "INFO"
severityNotice = "NOTICE"
severityWarning = "WARNING"
severityError = "ERROR"
severityCritical = "CRITICAL"
severityAlert = "ALERT"
severityEmergency = "EMERGENCY"
)
type jsonErr struct {
output string
}
func (e *jsonErr) Error() string {
return e.output
}
func jsonEncodeErr(severity string, message interface{}) error {
return &jsonErr{
output: jsonEncode(severity, message),
}
}
func jsonEncode(severity string, message interface{}) string {
b, err := json.Marshal(
map[string]interface{}{
"time": time.Now().Format(time.RFC3339Nano),
"severity": severity,
"message": message,
},
)
if err != nil {
panic(err)
}
return string(b)
}
func (m *Migration) stringApplied(do string) (string, error) {
if m.JsonFormat {
j := map[string]interface{}{
"section": m.Section,
"current": m.status.CurrentVersion,
}
var ss []*Source
for _, v := range m.status.fetchApplySources(do) {
ss = append(ss, v)
if !v.Apply {
break
}
}
j[do] = ss
if m.status.Error != nil {
j["error"] = m.status.Error.Error()
return "", jsonEncodeErr(severityCritical, j)
}
return jsonEncode(severityNotice, j), nil
}
var out []string
for _, v := range m.status.fetchApplySources(do) {
out = append(out, fmt.Sprintf("%s %d to %s is %s", state(v.Apply), v.Version, m.Section, v.File))
if !v.Apply {
break
}
}
return strings.Join(out, "\n"), m.status.Error
}
func (m *Migration) stringStatus() (string, error) {
var err error
if len(m.status.UnappliedSources) > 0 {
err = errors.New("There are versions that do not apply.")
}
if m.JsonFormat {
j := map[string]interface{}{
"section": m.Section,
"current": m.status.CurrentVersion,
}
if len(m.status.UnappliedSources) > 0 {
j["unapplied"] = m.status.UnappliedSources
}
if len(m.status.ApplySources) > 0 {
j["apply"] = m.status.ApplySources
}
if err != nil {
j["error"] = err.Error()
return "", jsonEncodeErr(severityError, j)
}
return jsonEncode(severityInfo, j), nil
}
out := fmt.Sprintf(" current:\n %d\n", m.status.CurrentVersion)
if len(m.status.UnappliedSources) > 0 {
var befores []string
for _, v := range m.status.UnappliedSources {
s := []string{fmt.Sprintf("%d", v.Version)}
if v.Duplicate {
s = append(s, "duplicate")
}
s = append(s, v.File)
befores = append(befores, strings.Join(s, " "))
}
out = fmt.Sprintf(" \x1b[31munapplied:\n%s\x1b[0m%s", fmt.Sprintf(" %s\n", strings.Join(befores, "\n ")), out)
}
if len(m.status.ApplySources) > 0 {
var afters []string
for _, v := range m.status.ApplySources {
afters = append(afters, fmt.Sprintf("%d %s", v.Version, v.File))
}
out = fmt.Sprintf("%s \x1b[33mapply:\n%s\x1b[0m", out, fmt.Sprintf(" %s\n", strings.Join(afters, "\n ")))
}
return fmt.Sprintf("Version of %s:\n%s", m.Section, out), err
}
func state(apply bool) string {
if apply {
return "OK"
}
return "NG"
}