blob: 91a223a3c3abffa71834af07d8f77c357f12d0a2 (
plain) (
blame)
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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Print functions for color-coded text.
// Author: bratseth
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
)
// Set this to have output written somewhere else than os.Stdout
var Out io.Writer
func init() {
Out = os.Stdout
}
// Prints in default color
func Print(messages ...string) {
print("", messages)
}
// Prints in a color appropriate for errors
func Error(messages ...string) {
print("\033[31m", messages)
}
// Prints in a color appropriate for success messages
func Success(messages ...string) {
print("\033[32m", messages)
}
// Prints in a color appropriate for detail messages
func Detail(messages ...string) {
print("\033[33m", messages)
}
// Prints all the text of the given reader
func PrintReader(reader io.Reader) {
bodyBytes := ReaderToBytes(reader)
var prettyJSON bytes.Buffer
parseError := json.Indent(&prettyJSON, bodyBytes, "", " ")
if parseError != nil { // Not JSON: Print plainly
Print(string(bodyBytes))
} else {
Print(string(prettyJSON.Bytes()))
}
}
func print(prefix string, messages []string) {
fmt.Fprint(Out, prefix)
for i := 0; i < len(messages); i++ {
fmt.Fprint(Out, messages[i])
if (i < len(messages) - 1) {
fmt.Fprint(Out, " ")
}
}
fmt.Fprintln(Out, "")
}
|