-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinters.go
163 lines (140 loc) · 2.99 KB
/
printers.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
package patman
import (
"encoding/csv"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/tidwall/sjson"
)
type printer func(results [][]string)
var printers = map[string]printer{
"stdout": handleStdoutPrint,
"csv": handleCsvPrint,
"json": handleJsonPrint,
}
func RegisterPrinter(name string, p printer) {
printers[name] = p
}
var csvWriter *csv.Writer
func handleCsvPrint(results [][]string) {
if len(pipelineNames) != len(pipelines) {
// TODO: better error
fmt.Println("all pipelines must be named")
os.Exit(1)
}
if csvWriter == nil {
csvWriter = csv.NewWriter(os.Stdout)
csvWriter.Write(pipelineNames)
}
empty := true
var record []string
for _, result := range results {
record = append(record, strings.TrimSpace(result[0]))
// print csv line if there's at least one non-empty value
if result[0] != "" {
empty = false
}
}
if !empty {
csvWriter.Write(record)
csvWriter.Flush()
}
}
var matchDigits = regexp.MustCompile(`^\d+(\.\d+)?$`)
func handleJsonPrint(results [][]string) {
json := "{}"
for _, result := range results {
match, name := result[0], result[1]
match = strings.TrimSpace(result[0])
if name == "" {
// TODO: This error should happen before parsing?
fmt.Println("cannot set json without named pipeline")
os.Exit(1)
}
// interpret all digit strings as numbers
// for friendlier json serialization
if matchDigits.MatchString(match) {
num, _ := strconv.ParseFloat(match, 64)
json, _ = sjson.Set(json, name, num)
continue
}
if match != "" {
json, _ = sjson.Set(json, name, match)
}
}
if json != "{}" {
fmt.Println(json)
}
}
func handleStdoutPrint(results [][]string) {
for i, result := range results {
match := strings.TrimSpace(result[0])
if match == "" {
continue
}
fmt.Print(match)
if i != len(results)-1 {
fmt.Print(" ")
}
}
if len(results) > 0 {
fmt.Print("\n")
}
}
var stdoutBuffer string
var stdoutBufferCount int
func flushBufferedStdout() {
fmt.Print(stdoutBuffer)
stdoutBuffer = ""
stdoutBufferCount = 0
}
func handleBufferedStdoutPrint(results [][]string) {
var r string
for i, result := range results {
match := strings.TrimSpace(result[0])
if match == "" {
continue
}
r += match
if i != len(results)-1 {
r += " "
}
}
if len(results) > 0 {
r += "\n"
}
stdoutBuffer += r
stdoutBufferCount++
if stdoutBufferCount >= stdoutBufferSize {
flushBufferedStdout()
}
}
var lastWrittenToken bool
func handleJoinPrint(results [][]string) {
for _, result := range results {
match := strings.TrimSpace(result[0])
if match == "" {
continue
}
if lastWrittenToken {
fmt.Print(joinDelimiter)
lastWrittenToken = false
}
fmt.Print(match)
lastWrittenToken = true
}
}
func handleCustomFormatPrint(results [][]string) {
// copy into msg
msg := format
for _, r := range results {
match, name := r[0], r[1]
// Using `%<name>` to void replacements with `$`
msg = strings.ReplaceAll(msg, "%"+name, match)
}
if msg != format {
fmt.Println(msg)
}
}