Skip to content

Commit 0ea2b57

Browse files
committed
feat: add output format options for get cmd & enhance CRD printer columns
1 parent 662e9d3 commit 0ea2b57

File tree

5 files changed

+125
-42
lines changed

5 files changed

+125
-42
lines changed

cmd/krb-cli/cmd/get-recycle-items.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"encoding/json"
2123
"os"
2224
"time"
2325

@@ -31,11 +33,13 @@ import (
3133
"k8s.io/apimachinery/pkg/labels"
3234
"k8s.io/apimachinery/pkg/util/duration"
3335
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/yaml"
3437
)
3538

3639
type GetRecycleItemFlags struct {
3740
ObjectResource string
3841
ObjectNamespace string
42+
OutputFormat string
3943
}
4044

4145
var getRecycleItemFlags GetRecycleItemFlags
@@ -70,9 +74,13 @@ func init() {
7074

7175
getRecycleItemCmd.Flags().StringVarP(&getRecycleItemFlags.ObjectResource, "object-resource", "", "", "List recycled resource objects filtered by the specified object resource")
7276
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")
7378

7479
getRecycleItemCmd.RegisterFlagCompletionFunc("object-resource", completion.RecycleItemGroupResource)
7580
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+
})
7684
}
7785

7886
func runGetRecycleItems(args []string) {
@@ -111,15 +119,42 @@ func runGetRecycleItems(args []string) {
111119
result = *list
112120
}
113121

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+
}
117126

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()
122159
}
123-
t.SetStyle(KrbTableStyle)
124-
t.Render()
125160
}

cmd/krb-cli/cmd/get-recycle-policies.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"encoding/json"
2123
"os"
24+
"strings"
2225
"time"
2326

2427
"github.com/jedib0t/go-pretty/v6/table"
@@ -31,11 +34,13 @@ import (
3134
"k8s.io/apimachinery/pkg/labels"
3235
"k8s.io/apimachinery/pkg/util/duration"
3336
"sigs.k8s.io/controller-runtime/pkg/client"
37+
"sigs.k8s.io/yaml"
3438
)
3539

3640
type GetRecyclePoliciesFlags struct {
3741
TargetResource string
3842
TargetNamespace string
43+
OutputFormat string
3944
}
4045

4146
var getRecyclePoliciesFlags GetRecyclePoliciesFlags
@@ -70,9 +75,13 @@ func init() {
7075

7176
getRecyclePoliciesCmd.Flags().StringVarP(&getRecyclePoliciesFlags.TargetResource, "target-resource", "", "", "List recycle policies filtered by the specified target resource")
7277
getRecyclePoliciesCmd.Flags().StringVarP(&getRecyclePoliciesFlags.TargetNamespace, "target-namespace", "", "", "List recycle policies filtered by the specified target namespace")
78+
getRecyclePoliciesCmd.Flags().StringVarP(&getRecyclePoliciesFlags.OutputFormat, "output", "o", "", "Output format. One of: json|yaml")
7379

7480
getRecyclePoliciesCmd.RegisterFlagCompletionFunc("target-resource", completion.RecyclePolicyGroupResource)
7581
getRecyclePoliciesCmd.RegisterFlagCompletionFunc("target-namespace", completion.RecyclePolicyNamespace)
82+
getRecyclePoliciesCmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
83+
return []string{"json", "yaml"}, cobra.ShellCompDirectiveDefault
84+
})
7685
}
7786

7887
func runGetRecyclePolicies(args []string) {
@@ -106,15 +115,44 @@ func runGetRecyclePolicies(args []string) {
106115
result = *list
107116
}
108117

109-
t := table.NewWriter()
110-
t.SetOutputMirror(os.Stdout)
111-
t.AppendHeader(table.Row{"name", "group", "resource", "namespaces", "age"})
118+
if len(result.Items) == 0 {
119+
tlog.Println("No recycle items found.")
120+
return
121+
}
112122

113-
for _, obj := range result.Items {
114-
t.AppendRow(table.Row{obj.Name, obj.Target.Group, obj.Target.Resource, obj.Target.Namespaces, duration.HumanDuration(time.Since(obj.CreationTimestamp.Time))}, table.RowConfig{
115-
AutoMerge: true,
116-
})
123+
switch getRecyclePoliciesFlags.OutputFormat {
124+
case "yaml":
125+
output, err := yaml.Marshal(result)
126+
if err != nil {
127+
tlog.Panicf("failed to marshal recycle policies to yaml: %v", err)
128+
}
129+
tlog.Print(string(output))
130+
case "json":
131+
y, err := yaml.Marshal(result)
132+
if err != nil {
133+
tlog.Panicf("failed to marshal recycle policies to json: %v", err)
134+
}
135+
j, err := yaml.YAMLToJSON(y)
136+
if err != nil {
137+
tlog.Panicf("failed to convert recycle policies to json: %v", err)
138+
}
139+
var output bytes.Buffer
140+
if err := json.Indent(&output, j, "", " "); err != nil {
141+
tlog.Panicf("failed to indent recycle policies json: %v", err)
142+
}
143+
tlog.Println(output.String())
144+
default:
145+
t := table.NewWriter()
146+
t.SetOutputMirror(os.Stdout)
147+
t.AppendHeader(table.Row{"Name", "Target GR", "Target Namespaces", "Age"})
148+
149+
for _, obj := range result.Items {
150+
t.AppendRow(table.Row{obj.Name, obj.Target.GroupResource().String(), strings.Join(obj.Target.Namespaces, ","), duration.HumanDuration(time.Since(obj.CreationTimestamp.Time))}, table.RowConfig{
151+
AutoMerge: true,
152+
})
153+
}
154+
t.SetStyle(KrbTableStyle)
155+
t.Render()
117156
}
118-
t.SetStyle(KrbTableStyle)
119-
t.Render()
157+
120158
}

internal/api/recycle_item_types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ func (obj *RecycledObject) GroupVersionResource() schema.GroupVersionResource {
100100
Version: obj.Version,
101101
Resource: obj.Resource,
102102
}
103-
104103
}
105104

106105
func (obj *RecycledObject) GroupResource() schema.GroupResource {
@@ -110,6 +109,13 @@ func (obj *RecycledObject) GroupResource() schema.GroupResource {
110109
}
111110
}
112111

112+
func (obj *RecycledObject) GroupVersion() schema.GroupVersion {
113+
return schema.GroupVersion{
114+
Group: obj.Group,
115+
Version: obj.Version,
116+
}
117+
}
118+
113119
func (obj *RecycledObject) ObjectGroupKind() schema.GroupKind {
114120
return schema.GroupKind{
115121
Group: obj.Group,

internal/api/recycle_policy_types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package api
1818

1919
import (
20+
"slices"
21+
2022
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2123
"k8s.io/apimachinery/pkg/runtime/schema"
2224
"k8s.io/apimachinery/pkg/util/rand"
@@ -42,17 +44,15 @@ type RecyclePolicyList struct {
4244
}
4345

4446
func NewRecyclePolicy(gvr schema.GroupVersionResource, targetNamespaces []string) *RecyclePolicy {
45-
if len(targetNamespaces) == 0 {
46-
targetNamespaces = []string{metav1.NamespaceAll}
47-
}
47+
targetNamespaces = slices.DeleteFunc(targetNamespaces, func(ns string) bool {
48+
return ns == metav1.NamespaceAll
49+
})
4850

4951
labels := map[string]string{
5052
"krb.ketches.cn/target-gr": gvr.GroupResource().String(),
5153
}
5254
for _, ns := range targetNamespaces {
53-
if ns != metav1.NamespaceAll {
54-
labels["krb.ketches.cn/target-namespace-"+ns] = "true"
55-
}
55+
labels["krb.ketches.cn/target-namespace-"+ns] = "true"
5656
}
5757

5858
return &RecyclePolicy{

manifests/crds.yaml

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,27 @@ spec:
6767
- name
6868
- raw
6969
additionalPrinterColumns:
70-
- name: Group
71-
type: string
72-
jsonPath: .object.group
73-
- name: Version
70+
- name: Recycled Object
7471
type: string
75-
jsonPath: .object.version
76-
- name: Kind
72+
jsonPath: .object.name
73+
- name: Object Kind
7774
type: string
7875
jsonPath: .object.kind
79-
- name: Resource
80-
type: string
81-
jsonPath: .object.resource
82-
- name: Namespace
76+
- name: Object Namespace
8377
type: string
8478
jsonPath: .object.namespace
85-
- name: Name
79+
- name: Object Group
8680
type: string
87-
jsonPath: .object.name
81+
jsonPath: .object.group
82+
priority: 1
83+
- name: Object Version
84+
type: string
85+
jsonPath: .object.version
86+
priority: 1
87+
- name: Object Resource
88+
type: string
89+
jsonPath: .object.resource
90+
priority: 1
8891
- name: Age
8992
type: date
9093
jsonPath: .metadata.creationTimestamp
@@ -139,15 +142,16 @@ spec:
139142
required:
140143
- resource
141144
additionalPrinterColumns:
142-
- name: Group
143-
type: string
144-
jsonPath: .target.group
145-
- name: Resource
145+
- name: Target Resource
146146
type: string
147147
jsonPath: .target.resource
148-
- name: Namespaces
148+
- name: Target Namespaces
149149
type: string
150150
jsonPath: .target.namespaces
151+
- name: Group
152+
type: string
153+
jsonPath: .target.group
154+
priority: 1
151155
- name: Age
152156
type: date
153157
jsonPath: .metadata.creationTimestamp

0 commit comments

Comments
 (0)