forked from yext/edward
/
autocomplete.go
53 lines (46 loc) · 1.13 KB
/
autocomplete.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
package cmd
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/yext/edward/common"
"github.com/yext/edward/config"
)
func autocompleteServicesAndGroups(homeDir string, arg string) {
if arg == "--generate-bash-completion" {
printCommandChildren(RootCmd)
return
}
configPath, err := config.GetConfigPathFromWorkingDirectory(homeDir)
if err != nil {
log.Println("Autocomplete> Error getting config path:", err)
return
}
if configPath == "" {
log.Println("Autocomplete> Config file not found")
return
}
cfg, err := config.LoadConfig(configPath, common.EdwardVersion)
if err != nil {
log.Println("Autocomplete> Error loading config:", err)
return
}
var names []string
for _, service := range cfg.ServiceMap {
names = append(names, service.Name)
names = append(names, service.Aliases...)
}
for _, group := range cfg.GroupMap {
names = append(names, group.Name)
names = append(names, group.Aliases...)
}
for _, name := range names {
fmt.Println(name)
}
}
func printCommandChildren(cmd *cobra.Command) {
for _, cmd := range cmd.Commands() {
fmt.Println(cmd.Use)
printCommandChildren(cmd)
}
}