/
completion_util.go
69 lines (65 loc) · 1.79 KB
/
completion_util.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"encoding/json"
"os"
"os/user"
"path"
"github.com/boson-project/faas/buildpacks"
"github.com/boson-project/faas/knative"
"github.com/spf13/cobra"
)
func CompleteFunctionList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
lister, err := knative.NewLister("")
if err != nil {
directive = cobra.ShellCompDirectiveError
return
}
s, err := lister.List()
if err != nil {
directive = cobra.ShellCompDirectiveError
return
}
strings = s
directive = cobra.ShellCompDirectiveDefault
return
}
func CompleteRuntimeList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
strings = []string{}
for lang := range buildpacks.RuntimeToBuildpack {
strings = append(strings, lang)
}
directive = cobra.ShellCompDirectiveDefault
return
}
func CompleteOutputFormatList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
directive = cobra.ShellCompDirectiveDefault
strings = []string{"plain", "yaml", "xml", "json"}
return
}
func CompleteRegistryList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
directive = cobra.ShellCompDirectiveError
u, err := user.Current()
if err != nil {
return
}
file, err := os.Open(path.Join(u.HomeDir, ".docker", "config.json"))
if err != nil {
return
}
decoder := json.NewDecoder(file)
var data map[string]interface{}
err = decoder.Decode(&data)
if err != nil {
return
}
auth, ok := data["auths"].(map[string]interface{})
if !ok {
return
}
strings = make([]string, len(auth))
for reg := range auth {
strings = append(strings, reg)
}
directive = cobra.ShellCompDirectiveDefault
return
}