-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnose.go
209 lines (178 loc) · 4.13 KB
/
diagnose.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package diagnostics
import (
"fmt"
"strings"
"github.com/acarl005/stripansi"
"github.com/nestoca/joy/internal/config"
"github.com/nestoca/joy/internal/dependencies"
"github.com/nestoca/joy/internal/style"
)
func Evaluate(cliVersion string, cfg *config.Config) Groups {
return Groups{
diagnoseExecutable(cfg, cliVersion, ExecutableOptions{}),
diagnoseDependencies(dependencies.AllRequired, dependencies.AllOptional),
diagnoseConfig(cfg, ConfigOpts{}),
diagnoseCatalog(cfg, CatalogOpts{}),
}
}
const (
success = "success"
warning = "warning"
failed = "failed"
hint = "hint"
info = "info"
)
type Message struct {
Type string
Value string
Details Messages
}
func (msg Message) StripAnsi() Message {
msg.Value = stripansi.Strip(msg.Value)
for i, detail := range msg.Details {
msg.Details[i] = detail.StripAnsi()
}
return msg
}
func (msg Message) String() string {
emoji := func() string {
switch msg.Type {
case success:
return "✅"
case warning:
return "⚠️"
case failed:
return "💔"
case hint:
return "👉"
case info:
fallthrough
default:
return "➡️"
}
}()
return emoji + " " + msg.Value + "\n" + indent(msg.Details.String())
}
type Messages []Message
func (set Messages) String() string {
var builder strings.Builder
for _, msg := range set {
builder.WriteString(msg.String())
}
return builder.String()
}
type Group struct {
Title string
Messages Messages
SubGroups Groups
topLevel bool
}
func (group *Group) AddMsg(typ string, value string, details ...Message) *Group {
group.Messages = append(group.Messages, msg(typ, value, details...))
return group
}
func (group *Group) AddSubGroup(sub ...Group) *Group {
group.SubGroups = append(group.SubGroups, sub...)
return group
}
type Stats struct {
Failed int
Warnings int
}
func (group Group) Stats() (result Stats) {
for _, msg := range group.Messages {
switch msg.Type {
case failed:
result.Failed++
case warning:
result.Warnings++
}
}
for _, sub := range group.SubGroups {
stats := sub.Stats()
result.Failed += stats.Failed
result.Warnings += stats.Warnings
}
return
}
func (group Group) StripAnsi() Group {
group.Title = stripansi.Strip(group.Title)
for i, msg := range group.Messages {
group.Messages[i] = msg.StripAnsi()
}
for i, sub := range group.SubGroups {
group.SubGroups[i] = sub.StripAnsi()
}
return group
}
func (group Group) String() string {
title := func() string {
if !group.topLevel {
return style.DiagnosticGroup(group.Title)
}
emoji := func() string {
stats := group.Stats()
switch {
case stats.Failed > 0:
return "💔"
case stats.Warnings > 0:
return "⚠️"
default:
return "✅"
}
}()
return emoji + " " + style.DiagnosticHeader(group.Title)
}()
return title + "\n" + indent(group.Messages.String()+group.SubGroups.String())
}
type Groups []Group
func (groups Groups) String() string {
if len(groups) == 0 {
return ""
}
var builder strings.Builder
for i, group := range groups {
builder.WriteString(group.String())
if i != len(groups)-1 {
builder.WriteByte('\n')
}
}
return builder.String()
}
func (groups Groups) Stats() (stats Stats) {
for _, group := range groups {
groupStats := group.Stats()
stats.Failed += groupStats.Failed
stats.Warnings += groupStats.Warnings
}
return
}
func indent(value string) string {
if value == "" {
return ""
}
return " " + strings.ReplaceAll(value, "\n", "\n ")
}
func msg(typ string, value string, details ...Message) Message {
return Message{
Type: typ,
Value: value,
Details: details,
}
}
func label(label string, value any) string {
return fmt.Sprintf("%s %v", style.DiagnosticLabel(label+":"), value)
}
type StatGroup interface {
fmt.Stringer
Stats() Stats
}
func OutputWithGlobalStats(group StatGroup) string {
statMsg := func() string {
if stats := group.Stats(); stats.Failed+stats.Warnings > 0 {
return fmt.Sprintf("🚨 Diagnostics completed with %d error(s) and %d warning(s)", stats.Failed, stats.Warnings)
}
return "🚀 All systems nominal. Houston, we're cleared for launch!"
}()
return group.String() + "\n" + statMsg
}