-
Notifications
You must be signed in to change notification settings - Fork 472
/
printer.go
249 lines (210 loc) · 6.12 KB
/
printer.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/grafana/cortex-tools/blob/main/pkg/printer/printer.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package printer
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"github.com/alecthomas/chroma/v2/quick"
"github.com/mitchellh/colorstring"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"github.com/grafana/mimir/pkg/mimirtool/rules"
"github.com/grafana/mimir/pkg/mimirtool/rules/rwrulefmt"
)
// Printer is used for printing formatted output from the mimirtool
type Printer struct {
disableColor bool
colorizer colorstring.Colorize
}
// New returns a Printer struct
func New(color bool) *Printer {
return &Printer{
disableColor: color,
colorizer: colorstring.Colorize{
Colors: colorstring.DefaultColors,
Reset: true,
Disable: color,
},
}
}
// Println is a convenience wrapper for fmt.Println with support for color
// codes.
func (p *Printer) Println(a string) {
fmt.Println(p.colorizer.Color(a))
}
// Printf is a convenience wrapper for fmt.Printf with support for color
// codes.
func (p *Printer) Printf(format string, a ...interface{}) {
fmt.Printf(p.colorizer.Color(format), a...)
}
// PrintAlertmanagerConfig prints the current alertmanager config
func (p *Printer) PrintAlertmanagerConfig(config string, templates map[string]string) error {
// go-text-template
if !p.disableColor {
err := quick.Highlight(os.Stdout, config, "yaml", "terminal", "swapoff")
if err != nil {
return err
}
} else {
fmt.Println(string(config))
}
fmt.Printf("\nTemplates:\n")
for fn, template := range templates {
fmt.Println(fn + ":")
if !p.disableColor {
err := quick.Highlight(os.Stdout, template, "go-text-template", "terminal", "swapoff")
if err != nil {
return nil
}
} else {
fmt.Println(template)
}
}
return nil
}
// PrintRuleGroups prints the current alertmanager config
func (p *Printer) PrintRuleGroups(rules map[string][]rwrulefmt.RuleGroup) error {
encodedRules, err := yaml.Marshal(&rules)
if err != nil {
return err
}
// go-text-template
if !p.disableColor {
return quick.Highlight(os.Stdout, string(encodedRules), "yaml", "terminal", "swapoff")
}
fmt.Println(string(encodedRules))
return nil
}
// PrintRuleGroup prints the current alertmanager config
func (p *Printer) PrintRuleGroup(rule rwrulefmt.RuleGroup) error {
encodedRule, err := yaml.Marshal(&rule)
if err != nil {
return err
}
// go-text-template
if !p.disableColor {
return quick.Highlight(os.Stdout, string(encodedRule), "yaml", "terminal", "swapoff")
}
fmt.Println(string(encodedRule))
return nil
}
// PrintComparisonResult prints the differences between the staged rules namespace
// and active rules namespace
func (p *Printer) PrintComparisonResult(results []rules.NamespaceChange, verbose bool) error {
created, updated, deleted := rules.SummarizeChanges(results)
// If any changes are detected, print the symbol legend
if (created + updated + deleted) > 0 {
fmt.Println("Changes are indicated with the following symbols:")
if created > 0 {
p.Println("[green] +[reset] created")
}
if updated > 0 {
p.Println("[yellow] ~[reset] updated")
}
if deleted > 0 {
p.Println("[red] -[reset] deleted")
}
fmt.Println()
fmt.Println("The following changes will be made if the provided rule set is synced:")
} else {
fmt.Println("no changes detected")
return nil
}
for _, change := range results {
switch change.State {
case rules.Created:
p.Printf("[green]+ Namespace: %v\n", change.Namespace)
for _, c := range change.GroupsCreated {
p.Printf("[green] + Group: %v\n", c.Name)
}
case rules.Updated:
p.Printf("[yellow]~ Namespace: %v\n", change.Namespace)
for _, c := range change.GroupsCreated {
p.Printf("[green] + Group: %v\n", c.Name)
}
for _, c := range change.GroupsUpdated {
p.Printf("[yellow] ~ Group: %v\n", c.New.Name)
// Print the full diff of the rules if verbose is set
if verbose {
newYaml, _ := yaml.Marshal(c.New)
separated := strings.Split(string(newYaml), "\n")
for _, l := range separated {
p.Printf("[green]+ %v\n", l)
}
oldYaml, _ := yaml.Marshal(c.Original)
separated = strings.Split(string(oldYaml), "\n")
for _, l := range separated {
p.Printf("[red]- %v\n", l)
}
}
}
for _, c := range change.GroupsDeleted {
p.Printf("[red] - Group: %v\n", c.Name)
}
case rules.Deleted:
p.Printf("[red]- Namespace: %v\n", change.Namespace)
for _, c := range change.GroupsDeleted {
p.Printf("[red] - Group: %v\n", c.Name)
}
}
}
fmt.Println()
fmt.Printf("Diff Summary: %v Groups Created, %v Groups Updated, %v Groups Deleted\n", created, updated, deleted)
return nil
}
func (p *Printer) PrintRuleSet(rules map[string][]rwrulefmt.RuleGroup, format string, writer io.Writer) error {
nsKeys := make([]string, 0, len(rules))
for k := range rules {
nsKeys = append(nsKeys, k)
}
slices.Sort(nsKeys)
type namespaceAndRuleGroup struct {
Namespace string `json:"namespace" yaml:"namespace"`
RuleGroup string `json:"rulegroup" yaml:"rulegroup"`
}
var items []namespaceAndRuleGroup
for _, ns := range nsKeys {
for _, rg := range rules[ns] {
items = append(items, namespaceAndRuleGroup{
Namespace: ns,
RuleGroup: rg.Name,
})
}
}
switch format {
case "json":
output, err := json.Marshal(items)
if err != nil {
return err
}
// go-text-template
if !p.disableColor {
return quick.Highlight(writer, string(output), "json", "terminal", "swapoff")
}
fmt.Fprint(writer, string(output))
case "yaml":
output, err := yaml.Marshal(items)
if err != nil {
return err
}
// go-text-template
if !p.disableColor {
return quick.Highlight(writer, string(output), "yaml", "terminal", "swapoff")
}
fmt.Fprint(writer, string(output))
default:
w := tabwriter.NewWriter(writer, 0, 0, 1, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Namespace\t Rule Group")
for _, item := range items {
fmt.Fprintf(w, "%s\t %s\n", item.Namespace, item.RuleGroup)
}
w.Flush()
}
return nil
}