Skip to content

Commit 00fb459

Browse files
authored
feat: accept KIND/NAME form (#57)
1 parent 1dbda51 commit 00fb459

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cmd/kubectl-tree/apis.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,20 @@ func apiNames(a metav1.APIResource, gv schema.GroupVersion) []string {
113113
}
114114
return out
115115
}
116+
117+
func figureOutKindName(args []string) (string, string, error) {
118+
if l := len(args); l == 0 || l > 2 {
119+
return "", "", fmt.Errorf("accepts between 1 and 2 arg(s), received %d", l)
120+
}
121+
if len(args) == 2 {
122+
return args[0], args[1], nil
123+
}
124+
seg := strings.Split(args[0], "/")
125+
if len(seg) < 2 {
126+
return "", "", fmt.Errorf("specify the kubernetes object in KIND NAME or KIND/NAME form")
127+
}
128+
if len(seg) > 2 {
129+
return "", "", fmt.Errorf("arguments in KIND/NAME form may not have more than one slash")
130+
}
131+
return seg[0], seg[1], nil
132+
}

cmd/kubectl-tree/rootcmd.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var rootCmd = &cobra.Command{
4747
Short: "Show sub-resources of the Kubernetes object",
4848
Example: " kubectl tree deployment my-app\n" +
4949
" kubectl tree kservice.v1.serving.knative.dev my-app", // TODO add more examples about disambiguation etc
50-
Args: cobra.MinimumNArgs(2),
50+
Args: cobra.RangeArgs(1, 2),
5151
RunE: run,
5252
Version: versionString(),
5353
}
@@ -90,7 +90,10 @@ func run(command *cobra.Command, args []string) error {
9090
}
9191
klog.V(3).Info("completed querying APIs list")
9292

93-
kind, name := args[0], args[1]
93+
kind, name, err := figureOutKindName(args)
94+
if err != nil {
95+
return err
96+
}
9497
klog.V(3).Infof("parsed kind=%v name=%v", kind, name)
9598

9699
var api apiResource

0 commit comments

Comments
 (0)