-
Notifications
You must be signed in to change notification settings - Fork 118
/
output.go
335 lines (298 loc) · 9.4 KB
/
output.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
// Copyright 2022 FairwindsOps Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"sort"
"text/tabwriter"
"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v3"
)
var padChar = byte(' ')
// Output is a thing that has an apiVersion in it
type Output struct {
// Name is the name of the object in question.
// This might be an object name, or a release
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// FilePath is the full path of the file if the output came from a file
FilePath string `json:"filePath,omitempty" yaml:"filePath,omitempty"`
// Namespace is the namespace that the object is in
// The output may resolve this to UNKNOWN if there is no way of determining it
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
// APIVersion is the version object corresponding to this output
APIVersion *Version `json:"api,omitempty" yaml:"api,omitempty"`
// Deprecated is a boolean indicating whether or not the version is deprecated
Deprecated bool `json:"deprecated" yaml:"deprecated"`
// Removed is a boolean indicating whether or not the version has been removed
Removed bool `json:"removed" yaml:"removed"`
// ReplacementAvailable is a boolean indicating whether or not the replacement is available
ReplacementAvailable bool `json:"replacementAvailable" yaml:"replacementAvailable"`
// CustomColumns is a list of column headers to be displayed with -ocustom or -omarkdown
CustomColumns []string `json:"-" yaml:"-"`
}
// Instance is an instance of the API. This holds configuration for a "run" of Pluto
type Instance struct {
Outputs []*Output `json:"items,omitempty" yaml:"items,omitempty"`
IgnoreDeprecations bool `json:"-" yaml:"-"`
IgnoreRemovals bool `json:"-" yaml:"-"`
IgnoreUnavailableReplacements bool `json:"-" yaml:"-"`
OnlyShowRemoved bool `json:"-" yaml:"-"`
NoHeaders bool `json:"-" yaml:"-"`
OutputFormat string `json:"-" yaml:"-"`
TargetVersions map[string]string `json:"target-versions,omitempty" yaml:"target-versions,omitempty"`
DeprecatedVersions []Version `json:"-" yaml:"-"`
CustomColumns []string `json:"-" yaml:"-"`
Components []string `json:"-" yaml:"-"`
}
// DisplayOutput prints the output based on desired variables
func (instance *Instance) DisplayOutput() error {
if len(instance.Outputs) == 0 && (instance.OutputFormat == "normal" || instance.OutputFormat == "wide") {
fmt.Println("There were no resources found with known deprecated apiVersions.")
return nil
}
instance.FilterOutput()
var err error
var outData []byte
switch instance.OutputFormat {
case "normal":
c := instance.normalColumns()
t := instance.tabOut(c)
err = t.Flush()
if err != nil {
return err
}
return nil
case "wide":
c := instance.wideColumns()
t := instance.tabOut(c)
err = t.Flush()
if err != nil {
return err
}
return nil
case "custom":
c := instance.customColumns()
t := instance.tabOut(c)
err = t.Flush()
if err != nil {
return err
}
case "json":
outData, err = json.Marshal(instance)
if err != nil {
return err
}
fmt.Println(string(outData))
case "yaml":
outData, err = yaml.Marshal(instance)
if err != nil {
return err
}
fmt.Println(string(outData))
case "markdown":
var c columnList
if len(instance.CustomColumns) >= 1 {
c = instance.customColumns()
} else {
c = instance.wideColumns()
}
t := instance.markdownOut(c)
if t != nil {
t.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
t.SetCenterSeparator("|")
t.Render()
}
case "csv":
var c columnList
if len(instance.CustomColumns) >= 1 {
c = instance.customColumns()
} else {
c = instance.wideColumns()
}
csvWriter, err := instance.csvOut(c)
if err != nil {
return err
}
csvWriter.Flush()
err = csvWriter.Error()
if err != nil {
return err
}
}
return nil
}
// FilterOutput filters the outputs that get printed
// first it fills out the Deprecated and Removed booleans
// then it returns the outputs that are either deprecated or removed
// and in the component list
// additionally, if instance.OnlyShowDeprecated is true, it will remove the
// apiVersions that are deprecated but not removed
func (instance *Instance) FilterOutput() {
var usableOutputs []*Output
for _, output := range instance.Outputs {
output.Deprecated = output.APIVersion.isDeprecatedIn(instance.TargetVersions)
output.Removed = output.APIVersion.isRemovedIn(instance.TargetVersions)
output.ReplacementAvailable = output.APIVersion.isReplacementAvailableIn(instance.TargetVersions)
switch instance.OnlyShowRemoved {
case false:
if output.Deprecated || output.Removed {
if StringInSlice(output.APIVersion.Component, instance.Components) {
usableOutputs = append(usableOutputs, output)
}
}
case true:
if output.Removed {
if StringInSlice(output.APIVersion.Component, instance.Components) {
usableOutputs = append(usableOutputs, output)
}
}
}
}
instance.Outputs = usableOutputs
}
// removeDeprecatedOnly is a list replacement operation
func (instance *Instance) tabOut(columns columnList) *tabwriter.Writer {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 15, 2, padChar, 0)
if len(instance.Outputs) == 0 {
_, _ = fmt.Fprintln(w, "No output to display")
return w
}
columnIndexes := make([]int, 0, len(columns))
for k := range columns {
columnIndexes = append(columnIndexes, k)
}
sort.Ints(columnIndexes)
if !instance.NoHeaders {
var headers string
for _, k := range columnIndexes {
if k == 0 {
headers = fmt.Sprintf("%s\t", columns[k].header())
} else {
headers = fmt.Sprintf("%s %s\t", headers, columns[k].header())
}
}
_, _ = fmt.Fprintln(w, headers)
}
var rows string
for _, o := range instance.Outputs {
var row string
for _, k := range columnIndexes {
if k == 0 {
row = fmt.Sprintf("%s\t", columns[k].value(o))
} else {
row = fmt.Sprintf("%s %s\t", row, columns[k].value(o))
}
}
rows = rows + row + "\n"
}
_, _ = fmt.Fprintln(w, rows)
return w
}
func (instance *Instance) markdownOut(columns columnList) *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
if len(instance.Outputs) == 0 {
_, _ = fmt.Println("No output to display")
return nil
}
columnIndexes := make([]int, 0, len(columns))
for k := range columns {
columnIndexes = append(columnIndexes, k)
}
sort.Ints(columnIndexes)
if !instance.NoHeaders {
var headers []string
for _, k := range columnIndexes {
headers = append(headers, columns[k].header())
}
table.SetHeader(headers)
}
for _, o := range instance.Outputs {
var row []string
for _, k := range columnIndexes {
row = append(row, columns[k].value(o))
}
table.Append(row)
}
return table
}
func (instance *Instance) csvOut(columns columnList) (*csv.Writer, error) {
csvWriter := csv.NewWriter(os.Stdout)
if len(instance.Outputs) == 0 {
_, _ = fmt.Println("No output to display")
}
columnIndexes := make([]int, 0, len(columns))
for k := range columns {
columnIndexes = append(columnIndexes, k)
}
sort.Ints(columnIndexes)
var csvData [][]string
if !instance.NoHeaders {
var headers []string
for _, k := range columnIndexes {
headers = append(headers, columns[k].header())
}
csvData = append(csvData, headers)
}
for _, o := range instance.Outputs {
var row []string
for _, k := range columnIndexes {
row = append(row, columns[k].value(o))
}
csvData = append(csvData, row)
}
for i := range csvData {
if err := csvWriter.Write(csvData[i]); err != nil {
return nil, err
}
}
return csvWriter, nil
}
// GetReturnCode checks for deprecated versions and returns a code.
// takes a boolean to ignore any errors.
// exit 2 - version deprecated
// exit 3 - version removed
// exit 4 - replacement is unavailable in target version
func (instance *Instance) GetReturnCode() int {
returnCode := 0
var deprecations int
var removals int
var unavailableReplacements int
for _, output := range instance.Outputs {
if output.APIVersion.isRemovedIn(instance.TargetVersions) {
removals = removals + 1
}
if output.APIVersion.isDeprecatedIn(instance.TargetVersions) {
if output.APIVersion.isReplacementAvailableIn(instance.TargetVersions) || !instance.IgnoreUnavailableReplacements {
deprecations = deprecations + 1
}
}
if !output.APIVersion.isReplacementAvailableIn(instance.TargetVersions) {
unavailableReplacements = unavailableReplacements + 1
}
}
if deprecations > 0 && !instance.IgnoreDeprecations {
returnCode = 2
}
if removals > 0 && !instance.IgnoreRemovals {
returnCode = 3
}
if unavailableReplacements > 0 && !instance.IgnoreUnavailableReplacements {
returnCode = 4
}
return returnCode
}