forked from libopenstorage/stork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
109 lines (99 loc) · 3.23 KB
/
get.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
package storkctl
import (
"io"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
kprinters "k8s.io/kubernetes/pkg/printers"
)
func newGetCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
getCommands := &cobra.Command{
Use: "get",
Short: "Get stork resources",
}
getCommands.AddCommand(
newGetSnapshotCommand(cmdFactory, ioStreams),
newGetMigrationCommand(cmdFactory, ioStreams),
newGetClusterPairCommand(cmdFactory, ioStreams),
newGetSchedulePolicyCommand(cmdFactory, ioStreams),
newGetMigrationScheduleCommand(cmdFactory, ioStreams),
newGetSnapshotScheduleCommand(cmdFactory, ioStreams),
newGetGroupVolumeSnapshotCommand(cmdFactory, ioStreams),
newGetClusterDomainsStatusCommand(cmdFactory, ioStreams),
newGetClusterDomainUpdateCommand(cmdFactory, ioStreams),
newGetVolumeSnapshotRestoreCommand(cmdFactory, ioStreams),
newGetApplicationBackupCommand(cmdFactory, ioStreams),
newGetApplicationBackupScheduleCommand(cmdFactory, ioStreams),
newGetApplicationRestoreCommand(cmdFactory, ioStreams),
newGetApplicationCloneCommand(cmdFactory, ioStreams),
newGetBackupLocationCommand(cmdFactory, ioStreams),
newGetapplicationRegistrationCommand(cmdFactory, ioStreams),
)
return getCommands
}
func getRow(object runtime.Object, cells []interface{}) metav1beta1.TableRow {
return metav1beta1.TableRow{
Cells: cells,
Object: runtime.RawExtension{
Object: object.DeepCopyObject(),
},
}
}
func printTable(
cmd *cobra.Command,
object runtime.Object,
columns []string,
withNamespace bool,
printerFunc interface{},
out io.Writer,
) error {
columnDefinitions := make([]metav1beta1.TableColumnDefinition, 0)
for _, c := range columns {
columnDefinitions = append(columnDefinitions, metav1beta1.TableColumnDefinition{
Name: c,
})
}
generator := kprinters.NewTableGenerator()
err := generator.TableHandler(columnDefinitions, printerFunc)
if err != nil {
return err
}
table, err := generator.GenerateTable(object, kprinters.GenerateOptions{
NoHeaders: true,
})
if err != nil {
return err
}
printer := printers.NewTablePrinter(printers.PrintOptions{
ColumnLabels: columns,
WithNamespace: withNamespace,
})
return printer.PrintObj(table, out)
}
func printEncoded(cmd *cobra.Command, object runtime.Object, outputFormat string, out io.Writer) error {
if meta.IsListType(object) {
object.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Version: "v1",
Kind: "List",
})
}
printer, err := (&genericclioptions.JSONYamlPrintFlags{}).ToPrinter(outputFormat)
if err != nil {
return err
}
return printer.PrintObj(object, out)
}
func printObjects(cmd *cobra.Command, object runtime.Object, cmdFactory Factory, columns []string, printerFunc interface{}, out io.Writer) error {
outputFormat, err := cmdFactory.GetOutputFormat()
if err != nil {
return err
}
if outputFormat == outputFormatTable {
return printTable(cmd, object, columns, cmdFactory.AllNamespaces(), printerFunc, out)
}
return printEncoded(cmd, object, outputFormat, out)
}