-
Notifications
You must be signed in to change notification settings - Fork 78
/
completion.go
31 lines (26 loc) · 1004 Bytes
/
completion.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
package completion
import (
"os/exec"
"strings"
"github.com/spf13/cobra"
)
// GetObjectsWithKubectl return completions with kubectl, we are doing this with
// kubectl since we have caching and without it completion is way too slow
func GetObjectsWithKubectl(obj string) []string {
out, err := exec.Command("kubectl", "get", obj, "-o=jsonpath={range .items[*]}{.metadata.name} {end}").Output()
if err != nil {
return nil
}
return strings.Fields(string(out))
}
// BaseCompletion return a completion for a kubernetes object using Kubectl
func BaseCompletion(target string, args []string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return GetObjectsWithKubectl(target), cobra.ShellCompDirectiveNoFileComp
}
// ParentCompletion do completion of command to the Parent
func ParentCompletion(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
return BaseCompletion(cmd.Parent().Name(), args)
}