@@ -17,7 +17,9 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
22
+ "encoding/json"
21
23
"os"
22
24
"time"
23
25
@@ -31,11 +33,13 @@ import (
31
33
"k8s.io/apimachinery/pkg/labels"
32
34
"k8s.io/apimachinery/pkg/util/duration"
33
35
"sigs.k8s.io/controller-runtime/pkg/client"
36
+ "sigs.k8s.io/yaml"
34
37
)
35
38
36
39
type GetRecycleItemFlags struct {
37
40
ObjectResource string
38
41
ObjectNamespace string
42
+ OutputFormat string
39
43
}
40
44
41
45
var getRecycleItemFlags GetRecycleItemFlags
@@ -70,9 +74,13 @@ func init() {
70
74
71
75
getRecycleItemCmd .Flags ().StringVarP (& getRecycleItemFlags .ObjectResource , "object-resource" , "" , "" , "List recycled resource objects filtered by the specified object resource" )
72
76
getRecycleItemCmd .Flags ().StringVarP (& getRecycleItemFlags .ObjectNamespace , "object-namespace" , "" , "" , "List recycled resource objects filtered by the specified object namespace" )
77
+ getRecycleItemCmd .Flags ().StringVarP (& getRecycleItemFlags .OutputFormat , "output" , "o" , "" , "Output format. One of: json|yaml" )
73
78
74
79
getRecycleItemCmd .RegisterFlagCompletionFunc ("object-resource" , completion .RecycleItemGroupResource )
75
80
getRecycleItemCmd .RegisterFlagCompletionFunc ("object-namespace" , completion .RecycleItemNamespace )
81
+ getRecycleItemCmd .RegisterFlagCompletionFunc ("output" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
82
+ return []string {"json" , "yaml" }, cobra .ShellCompDirectiveNoFileComp
83
+ })
76
84
}
77
85
78
86
func runGetRecycleItems (args []string ) {
@@ -111,15 +119,42 @@ func runGetRecycleItems(args []string) {
111
119
result = * list
112
120
}
113
121
114
- t := table .NewWriter ()
115
- t .SetOutputMirror (os .Stdout )
116
- t .AppendHeader (table.Row {"name" , "group" , "version" , "kind" , "namespace" , "name" , "age" })
122
+ if len (result .Items ) == 0 {
123
+ tlog .Println ("No recycle items found." )
124
+ return
125
+ }
117
126
118
- for _ , obj := range result .Items {
119
- t .AppendRow (table.Row {obj .Name , obj .Object .Group , obj .Object .Version , obj .Object .Resource , obj .Object .Namespace , obj .Object .Name , duration .HumanDuration (time .Since (obj .CreationTimestamp .Time ))}, table.RowConfig {
120
- AutoMerge : true ,
121
- })
127
+ switch getRecycleItemFlags .OutputFormat {
128
+ case "yaml" :
129
+ output , err := yaml .Marshal (result )
130
+ if err != nil {
131
+ tlog .Panicf ("failed to marshal recycle items to yaml: %v" , err )
132
+ }
133
+ tlog .Print (string (output ))
134
+ case "json" :
135
+ y , err := yaml .Marshal (result )
136
+ if err != nil {
137
+ tlog .Panicf ("failed to marshal recycle items to json: %v" , err )
138
+ }
139
+ j , err := yaml .YAMLToJSON (y )
140
+ if err != nil {
141
+ tlog .Panicf ("failed to convert recycle items to json: %v" , err )
142
+ }
143
+ var output bytes.Buffer
144
+ if err := json .Indent (& output , j , "" , " " ); err != nil {
145
+ tlog .Panicf ("failed to indent recycle items json: %v" , err )
146
+ }
147
+ tlog .Println (output .String ())
148
+ default :
149
+ t := table .NewWriter ()
150
+ t .SetOutputMirror (os .Stdout )
151
+ t .AppendHeader (table.Row {"Name" , "Object Key" , "Object APIVersion" , "Object Kind" , "Age" })
152
+ for _ , obj := range result .Items {
153
+ t .AppendRow (table.Row {obj .Name , obj .Object .Key ().String (), obj .Object .GroupVersion ().String (), obj .Object .Kind , duration .HumanDuration (time .Since (obj .CreationTimestamp .Time ))}, table.RowConfig {
154
+ AutoMerge : true ,
155
+ })
156
+ }
157
+ t .SetStyle (KrbTableStyle )
158
+ t .Render ()
122
159
}
123
- t .SetStyle (KrbTableStyle )
124
- t .Render ()
125
160
}
0 commit comments