8000 Add dynamic completion for --node flag (#244) · stern/stern@59d4453 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 59d4453

Browse files
author
Takashi Kusumi
authored
Add dynamic completion for --node flag (#244)
1 parent f90f70f commit 59d4453

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ stern . --only-log-lines
290290
Stern supports command-line auto completion for bash, zsh or fish. `stern
291291
--completion=(bash|zsh|fish)` outputs the shell completion code which work by being
292292
evaluated in `.bashrc`, etc for the specified shell. In addition, Stern
293-
supports dynamic completion for `--namespace`, `--context`, a resource query
293+
supports dynamic completion for `--namespace`, `--context`, `--node`, a resource query
294294
in the form `<resource>/<name>`, and flags with pre-defined choices.
295295

296296
If you use bash, stern bash completion code depends on the

cmd/flag_completion.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func registerCompletionFuncForFlags(cmd *cobra.Command, o *options) error {
7575
return err
7676
}
7777

78+
if err := cmd.RegisterFlagCompletionFunc("node", nodeCompletionFunc(o)); err != nil {
79+
return err
80+
}
81+
7882
if err := cmd.RegisterFlagCompletionFunc("context", contextCompletionFunc(o)); err != nil {
7983
return err
8084
}
@@ -116,6 +120,32 @@ func namespaceCompletionFunc(o *options) func(cmd *cobra.Command, args []string,
116120
}
117121
}
118122

123+
// nodeCompletionFunc is a completion function that completes node names
124+
// that match the toComplete prefix.
125+
func nodeCompletionFunc(o *options) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
126+
return func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
127+
clientConfig := kubernetes.NewClientConfig(o.kubeConfig, o.context)
128+
clientset, err := kubernetes.NewClientSet(clientConfig)
129+
if err != nil {
130+
return compError(err)
131+
}
132+
133+
nodeList, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
134+
if err != nil {
135+
return compError(err)
136+
}
137+
138+
var comps []string
139+
for _, node := range nodeList.Items {
140+
if strings.HasPrefix(node.GetName(), toComplete) {
141+
comps = append(comps, node.GetName())
142+
}
143+
}
144+
145+
return comps, cobra.ShellCompDirectiveNoFileComp
146+
}
147+
}
148+
119149
// contextCompletionFunc is a completion function that completes contexts
120150
// that match the toComplete prefix.
121151
func contextCompletionFunc(o *options) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

0 commit comments

Comments
 (0)
0