-
Notifications
You must be signed in to change notification settings - Fork 157
/
message.go
361 lines (300 loc) · 9.25 KB
/
message.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package message provides a rich set of functions for displaying messages to the user.
package message
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime/debug"
"strings"
"time"
"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/config"
"github.com/fatih/color"
"github.com/pterm/pterm"
"github.com/sergi/go-diff/diffmatchpatch"
)
// LogLevel is the level of logging to display.
type LogLevel int
const (
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel LogLevel = iota
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel
// TraceLevel level. Designates finer-grained informational events than the Debug.
TraceLevel
// TermWidth sets the width of full width elements like progressbars and headers
TermWidth = 100
)
// NoProgress tracks whether spinner/progress bars show updates.
var NoProgress bool
// RuleLine creates a line of ━ as wide as the terminal
var RuleLine = strings.Repeat("━", TermWidth)
// logLevel holds the pterm compatible log level integer
var logLevel = InfoLevel
// logFile acts as a buffer for logFile generation
var logFile *pausableLogFile
// DebugWriter represents a writer interface that writes to message.Debug
type DebugWriter struct{}
func (d *DebugWriter) Write(raw []byte) (int, error) {
debugPrinter(2, string(raw))
return len(raw), nil
}
func init() {
pterm.ThemeDefault.SuccessMessageStyle = *pterm.NewStyle(pterm.FgLightGreen)
// Customize default error.
pterm.Success.Prefix = pterm.Prefix{
Text: " ✔",
Style: pterm.NewStyle(pterm.FgLightGreen),
}
pterm.Error.Prefix = pterm.Prefix{
Text: " ERROR:",
Style: pterm.NewStyle(pterm.BgLightRed, pterm.FgBlack),
}
pterm.Info.Prefix = pterm.Prefix{
Text: " •",
}
pterm.SetDefaultOutput(os.Stderr)
}
// UseLogFile writes output to stderr and a logFile.
func UseLogFile(dir string) (io.Writer, error) {
// Prepend the log filename with a timestamp.
ts := time.Now().Format("2006-01-02-15-04-05")
f, err := os.CreateTemp(dir, fmt.Sprintf("zarf-%s-*.log", ts))
if err != nil {
return nil, err
}
logFile = &pausableLogFile{
wr: f,
f: f,
}
return logFile, nil
}
// LogFileLocation returns the location of the log file.
func LogFileLocation() string {
if logFile == nil {
return ""
}
return logFile.f.Name()
}
// SetLogLevel sets the log level.
func SetLogLevel(lvl LogLevel) {
logLevel = lvl
if logLevel >= DebugLevel {
pterm.EnableDebugMessages()
}
}
// GetLogLevel returns the current log level.
func GetLogLevel() LogLevel {
return logLevel
}
// DisableColor disables color in output
func DisableColor() {
pterm.DisableColor()
}
// ZarfCommand prints a zarf terminal command.
func ZarfCommand(format string, a ...any) {
Command("zarf "+format, a...)
}
// Command prints a zarf terminal command.
func Command(format string, a ...any) {
style := pterm.NewStyle(pterm.FgWhite, pterm.BgBlack)
style.Printfln("$ "+format, a...)
}
// Debug prints a debug message.
func Debug(payload ...any) {
debugPrinter(2, payload...)
}
// Debugf prints a debug message with a given format.
func Debugf(format string, a ...any) {
message := fmt.Sprintf(format, a...)
debugPrinter(2, message)
}
// ErrorWebf prints an error message and returns a web response.
func ErrorWebf(err any, w http.ResponseWriter, format string, a ...any) {
debugPrinter(2, err)
message := fmt.Sprintf(format, a...)
Warn(message)
http.Error(w, message, http.StatusInternalServerError)
}
// Warn prints a warning message.
func Warn(message string) {
Warnf("%s", message)
}
// Warnf prints a warning message with a given format.
func Warnf(format string, a ...any) {
message := Paragraphn(TermWidth-10, format, a...)
pterm.Println()
pterm.Warning.Println(message)
}
// WarnErr prints an error message as a warning.
func WarnErr(err any, message string) {
debugPrinter(2, err)
Warnf(message)
}
// WarnErrf prints an error message as a warning with a given format.
func WarnErrf(err any, format string, a ...any) {
debugPrinter(2, err)
Warnf(format, a...)
}
// Fatal prints a fatal error message and exits with a 1.
func Fatal(err any, message string) {
debugPrinter(2, err)
errorPrinter(2).Println(message)
debugPrinter(2, string(debug.Stack()))
os.Exit(1)
}
// Fatalf prints a fatal error message and exits with a 1 with a given format.
func Fatalf(err any, format string, a ...any) {
message := Paragraph(format, a...)
Fatal(err, message)
}
// Info prints an info message.
func Info(message string) {
Infof("%s", message)
}
// Infof prints an info message with a given format.
func Infof(format string, a ...any) {
if logLevel > 0 {
message := Paragraph(format, a...)
pterm.Info.Println(message)
}
}
// Success prints a success message.
func Success(message string) {
Successf("%s", message)
}
// Successf prints a success message with a given format.
func Successf(format string, a ...any) {
message := Paragraph(format, a...)
pterm.Success.Println(message)
}
// Question prints a user prompt description message.
func Question(text string) {
Questionf("%s", text)
}
// Questionf prints a user prompt description message with a given format.
func Questionf(format string, a ...any) {
message := Paragraph(format, a...)
pterm.Println()
pterm.FgLightGreen.Println(message)
}
// Note prints a note message.
func Note(text string) {
Notef("%s", text)
}
// Notef prints a note message with a given format.
func Notef(format string, a ...any) {
message := Paragraphn(TermWidth-7, format, a...)
notePrefix := pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.InfoPrefixStyle,
Text: "NOTE",
},
}
pterm.Println()
notePrefix.Println(message)
}
// Title prints a title and an optional help description for that section
func Title(title string, help string) {
titleFormatted := pterm.FgBlack.Sprint(pterm.BgWhite.Sprintf(" %s ", title))
helpFormatted := pterm.FgGray.Sprint(help)
pterm.Printfln("%s %s", titleFormatted, helpFormatted)
}
// HeaderInfof prints a large header with a formatted message.
func HeaderInfof(format string, a ...any) {
pterm.Println()
message := helpers.Truncate(fmt.Sprintf(format, a...), TermWidth, false)
// Ensure the text is consistent for the header width
padding := TermWidth - len(message)
pterm.DefaultHeader.
WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)).
WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)).
WithMargin(2).
Printfln(message + strings.Repeat(" ", padding))
}
// HorizontalRule prints a white horizontal rule to separate the terminal
func HorizontalRule() {
pterm.Println()
pterm.Println(RuleLine)
}
// JSONValue prints any value as JSON.
func JSONValue(value any) string {
bytes, err := json.MarshalIndent(value, "", " ")
if err != nil {
debugPrinter(2, fmt.Sprintf("ERROR marshalling json: %s", err.Error()))
}
return string(bytes)
}
// Paragraph formats text into a paragraph matching the TermWidth
func Paragraph(format string, a ...any) string {
return Paragraphn(TermWidth, format, a...)
}
// Paragraphn formats text into an n column paragraph
func Paragraphn(n int, format string, a ...any) string {
return pterm.DefaultParagraph.WithMaxWidth(n).Sprintf(format, a...)
}
// PrintDiff prints the differences between a and b with a as original and b as new
func PrintDiff(textA, textB string) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(textA, textB, true)
diffs = dmp.DiffCleanupSemantic(diffs)
pterm.Println(dmp.DiffPrettyText(diffs))
}
// Table prints a padded table containing the specified header and data
func Table(header []string, data [][]string) {
pterm.Println()
// To avoid side effects make copies of the header and data before adding padding
headerCopy := make([]string, len(header))
copy(headerCopy, header)
dataCopy := make([][]string, len(data))
copy(dataCopy, data)
if len(headerCopy) > 0 {
headerCopy[0] = fmt.Sprintf(" %s", headerCopy[0])
}
table := pterm.TableData{
headerCopy,
}
for _, row := range dataCopy {
if len(row) > 0 {
row[0] = fmt.Sprintf(" %s", row[0])
}
table = append(table, pterm.TableData{row}...)
}
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
}
// ColorWrap changes a string to an ansi color code and appends the default color to the end
// preventing future characters from taking on the given color
// returns string as normal if color is disabled
func ColorWrap(str string, attr color.Attribute) string {
if config.NoColor {
return str
}
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", attr, str)
}
func debugPrinter(offset int, a ...any) {
printer := pterm.Debug.WithShowLineNumber(logLevel > 2).WithLineNumberOffset(offset)
now := time.Now().Format(time.RFC3339)
// prepend to a
a = append([]any{now, " - "}, a...)
printer.Println(a...)
// Always write to the log file
if logFile != nil {
pterm.Debug.
WithShowLineNumber(true).
WithLineNumberOffset(offset).
WithDebugger(false).
WithWriter(logFile).
Println(a...)
}
}
func errorPrinter(offset int) *pterm.PrefixPrinter {
return pterm.Error.WithShowLineNumber(logLevel > 2).WithLineNumberOffset(offset)
}