-
Notifications
You must be signed in to change notification settings - Fork 34
/
json_parser.go
145 lines (125 loc) · 3.85 KB
/
json_parser.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
package terraform
import (
"bufio"
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
"strings"
)
type (
// Taken from https://github.com/hashicorp/terraform/blob/2622e89cfb7eb62045fbb35956345eb4ad169704/internal/command/views/json/diagnostic.go#L34
Diagnostic struct {
Severity string `json:"severity"`
Summary string `json:"summary"`
Detail string `json:"detail"`
Address string `json:"address,omitempty"`
}
// Taken from https://github.com/hashicorp/terraform/blob/2622e89cfb7eb62045fbb35956345eb4ad169704/internal/command/jsonformat/renderer.go#L25
JSONLogType string
JSONLog struct {
Level string `json:"@level"`
Message string `json:"@message"`
Type JSONLogType `json:"type"`
Diagnostic *Diagnostic `json:"diagnostic"`
}
JSONLogs []JSONLog
)
const (
LogApplyComplete JSONLogType = "apply_complete"
LogApplyErrored JSONLogType = "apply_errored"
LogApplyStart JSONLogType = "apply_start"
LogChangeSummary JSONLogType = "change_summary"
LogDiagnostic JSONLogType = "diagnostic"
LogPlannedChange JSONLogType = "planned_change"
LogProvisionComplete JSONLogType = "provision_complete"
LogProvisionErrored JSONLogType = "provision_errored"
LogProvisionProgress JSONLogType = "provision_progress"
LogProvisionStart JSONLogType = "provision_start"
LogOutputs JSONLogType = "outputs"
LogRefreshComplete JSONLogType = "refresh_complete"
LogRefreshStart JSONLogType = "refresh_start"
LogResourceDrift JSONLogType = "resource_drift"
LogVersion JSONLogType = "version"
)
const (
DiagnosticSeverityUnknown = "unknown"
DiagnosticSeverityError = "error"
DiagnosticSeverityWarning = "warning"
)
func (l JSONLogs) prettyPrint() string {
b := new(strings.Builder)
for _, v := range l {
b.WriteString("\n")
b.WriteString(v.prettyPrint())
}
return b.String()
}
func (l JSONLog) prettyPrint() string {
b := new(strings.Builder)
if l.Type == LogApplyErrored {
b.WriteString(l.Message)
}
// formated as:
// hcloud_server.test-jxt0chy-hetzner-asg2dpg-1: Creation errored after 0s
// hcloud_server.test-jxt0chy-hetzner-c05ep09-1: Creation errored after 1s
// diagnostic:
// severity: error
// summary: server type cpx89 not found
// address hcloud_server.test-jxt0chy-hetzner-asg2dpg-1
//
// diagnostic:
// severity: error
// summary: server type cpx89 not found
// address hcloud_server.test-jxt0chy-hetzner-c05ep09-1
if l.Type == LogDiagnostic {
if l.Diagnostic != nil {
b.WriteString("diagnostic:\n")
b.WriteString(fmt.Sprintf("\tseverity: %s\n", l.Diagnostic.Severity))
b.WriteString(fmt.Sprintf("\tsummary: %s\n", l.Diagnostic.Summary))
if l.Diagnostic.Detail != "" {
b.WriteString(fmt.Sprintf("\tdetail: %s\n", l.Diagnostic.Detail))
}
if l.Diagnostic.Address != "" {
b.WriteString(fmt.Sprintf("\taddress: %s\n", l.Diagnostic.Address))
}
} else {
b.WriteString(l.Message)
}
}
return b.String()
}
// parseJSONLog parses a single line into the JSONLog struct.
func parseJSONLog(line []byte) (JSONLog, error) {
s := JSONLog{}
if err := json.Unmarshal(line, &s); err != nil {
return JSONLog{}, err
}
return s, nil
}
// collectErrors parses specific errors from a stream of json data, delimited by a new line, that the terraform
// tool outputs with the `-json` flag.
func collectErrors(reader io.Reader) (JSONLogs, error) {
s := bufio.NewScanner(reader)
s.Split(bufio.ScanLines)
var logs []JSONLog
for s.Scan() {
txt := s.Text()
if !gjson.Valid(txt) {
continue
}
l, err := parseJSONLog([]byte(txt))
if err != nil {
return nil, err
}
isErrLog := l.Type == LogApplyErrored
isErrLog = isErrLog || ((l.Type == LogDiagnostic) && (l.Level == DiagnosticSeverityError))
if isErrLog {
logs = append(logs, l)
}
}
if err := s.Err(); err != nil {
return nil, err
}
return logs, nil
}