forked from vmware-tanzu/velero
/
output.go
248 lines (215 loc) · 7.68 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
/*
Copyright 2017, 2020 the Velero contributors.
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 output
import (
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/printers"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
"github.com/vmware-tanzu/velero/pkg/util/encode"
)
const downloadRequestTimeout = 30 * time.Second
// BindFlags defines a set of output-specific flags within the provided
// FlagSet.
func BindFlags(flags *pflag.FlagSet) {
flags.StringP("output", "o", "table", "Output display format. For create commands, display the object but do not send it to the server. Valid formats are 'table', 'json', and 'yaml'. 'table' is not valid for the install command.")
labelColumns := flag.NewStringArray()
flags.Var(&labelColumns, "label-columns", "a comma-separated list of labels to be displayed as columns")
flags.Bool("show-labels", false, "show labels in the last column")
}
// BindFlagsSimple defines the output format flag only.
func BindFlagsSimple(flags *pflag.FlagSet) {
flags.StringP("output", "o", "table", "Output display format. For create commands, display the object but do not send it to the server. Valid formats are 'table', 'json', and 'yaml'. 'table' is not valid for the install command.")
}
// ClearOutputFlagDefault sets the current and default value
// of the "output" flag to the empty string.
func ClearOutputFlagDefault(cmd *cobra.Command) {
f := cmd.Flag("output")
if f == nil {
return
}
f.DefValue = ""
f.Value.Set("")
}
// GetOutputFlagValue returns the value of the "output" flag
// in the provided command, or the zero value if not present.
func GetOutputFlagValue(cmd *cobra.Command) string {
return flag.GetOptionalStringFlag(cmd, "output")
}
// GetLabelColumnsValues returns the value of the "label-columns" flag
// in the provided command, or the zero value if not present.
func GetLabelColumnsValues(cmd *cobra.Command) []string {
return flag.GetOptionalStringArrayFlag(cmd, "label-columns")
}
// GetShowLabelsValue returns the value of the "show-labels" flag
// in the provided command, or the zero value if not present.
func GetShowLabelsValue(cmd *cobra.Command) bool {
return flag.GetOptionalBoolFlag(cmd, "show-labels")
}
// ValidateFlags returns an error if any of the output-related flags
// were specified with invalid values, or nil otherwise.
func ValidateFlags(cmd *cobra.Command) error {
if err := validateOutputFlag(cmd); err != nil {
return err
}
return nil
}
func validateOutputFlag(cmd *cobra.Command) error {
output := GetOutputFlagValue(cmd)
switch output {
case "", "json", "yaml":
case "table":
if cmd.Name() == "install" {
return errors.New("'table' format is not supported with 'install' command")
}
default:
return errors.Errorf("invalid output format %q - valid values are 'table', 'json', and 'yaml'", output)
}
return nil
}
// PrintWithFormat prints the provided object in the format specified by
// the command's flags.
func PrintWithFormat(c *cobra.Command, obj runtime.Object) (bool, error) {
format := GetOutputFlagValue(c)
if format == "" {
return false, nil
}
switch format {
case "table":
return printTable(c, obj)
case "json", "yaml":
return printEncoded(obj, format)
}
return false, errors.Errorf("unsupported output format %q; valid values are 'table', 'json', and 'yaml'", format)
}
func printEncoded(obj runtime.Object, format string) (bool, error) {
// assume we're printing obj
toPrint := obj
if meta.IsListType(obj) {
list, _ := meta.ExtractList(obj)
if len(list) == 1 {
// if obj was a list and there was only 1 item, just print that 1 instead of a list
toPrint = list[0]
}
}
encoded, err := encode.Encode(toPrint, format)
if err != nil {
return false, err
}
fmt.Println(string(encoded))
return true, nil
}
func printTable(cmd *cobra.Command, obj runtime.Object) (bool, error) {
// 1. generate table
var table *metav1.Table
switch obj.(type) {
case *velerov1api.Backup:
table = &metav1.Table{
ColumnDefinitions: backupColumns,
Rows: printBackup(obj.(*velerov1api.Backup)),
}
case *velerov1api.BackupList:
table = &metav1.Table{
ColumnDefinitions: backupColumns,
Rows: printBackupList(obj.(*velerov1api.BackupList)),
}
case *velerov1api.Restore:
table = &metav1.Table{
ColumnDefinitions: restoreColumns,
Rows: printRestore(obj.(*velerov1api.Restore)),
}
case *velerov1api.RestoreList:
table = &metav1.Table{
ColumnDefinitions: restoreColumns,
Rows: printRestoreList(obj.(*velerov1api.RestoreList)),
}
case *velerov1api.Schedule:
table = &metav1.Table{
ColumnDefinitions: scheduleColumns,
Rows: printSchedule(obj.(*velerov1api.Schedule)),
}
case *velerov1api.ScheduleList:
table = &metav1.Table{
ColumnDefinitions: scheduleColumns,
Rows: printScheduleList(obj.(*velerov1api.ScheduleList)),
}
case *velerov1api.ResticRepository:
table = &metav1.Table{
ColumnDefinitions: resticRepoColumns,
Rows: printResticRepo(obj.(*velerov1api.ResticRepository)),
}
case *velerov1api.ResticRepositoryList:
table = &metav1.Table{
ColumnDefinitions: resticRepoColumns,
Rows: printResticRepoList(obj.(*velerov1api.ResticRepositoryList)),
}
case *velerov1api.BackupStorageLocation:
table = &metav1.Table{
ColumnDefinitions: backupStorageLocationColumns,
Rows: printBackupStorageLocation(obj.(*velerov1api.BackupStorageLocation)),
}
case *velerov1api.BackupStorageLocationList:
table = &metav1.Table{
ColumnDefinitions: backupStorageLocationColumns,
Rows: printBackupStorageLocationList(obj.(*velerov1api.BackupStorageLocationList)),
}
case *velerov1api.VolumeSnapshotLocation:
table = &metav1.Table{
ColumnDefinitions: volumeSnapshotLocationColumns,
Rows: printVolumeSnapshotLocation(obj.(*velerov1api.VolumeSnapshotLocation)),
}
case *velerov1api.VolumeSnapshotLocationList:
table = &metav1.Table{
ColumnDefinitions: volumeSnapshotLocationColumns,
Rows: printVolumeSnapshotLocationList(obj.(*velerov1api.VolumeSnapshotLocationList)),
}
case *velerov1api.ServerStatusRequest:
table = &metav1.Table{
ColumnDefinitions: pluginColumns,
Rows: printPluginList(obj.(*velerov1api.ServerStatusRequest)),
}
default:
return false, errors.Errorf("type %T is not supported", obj)
}
if table == nil {
return false, errors.Errorf("error generating table for type %T", obj)
}
// 2. print table
tablePrinter, err := NewPrinter(cmd)
if err != nil {
return false, err
}
err = tablePrinter.PrintObj(table, os.Stdout)
if err != nil {
return false, err
}
return true, nil
}
// NewPrinter returns a printer for doing human-readable table printing of
// Velero objects.
func NewPrinter(cmd *cobra.Command) (printers.ResourcePrinter, error) {
options := printers.PrintOptions{
ShowLabels: GetShowLabelsValue(cmd),
ColumnLabels: GetLabelColumnsValues(cmd),
}
printer := printers.NewTablePrinter(options)
return printer, nil
}