-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.go
60 lines (48 loc) · 1.11 KB
/
config.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
package main
import (
"os"
"path/filepath"
"strings"
homedir "github.com/mitchellh/go-homedir"
"github.com/posener/complete"
)
var (
dabConfPath string
home string
)
func init() {
var err error
home, err = homedir.Dir()
if err != nil {
panic(err)
}
dabConfPath = filepath.Join(home, ".config/dab")
if val := os.Getenv("DAB_CONF_PATH"); val != "" {
dabConfPath = val
}
}
func predictConfigKeys(args complete.Args) []string {
return getCompletedFragmentChildren(getCompletedFragment(args.Last))
}
func getDabConfigPath(key string) string {
return filepath.Join(dabConfPath, key)
}
func getCompletedFragmentChildren(input string) (out []string) {
dir := getDabConfigPath(input)
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil || path == dir || f.IsDir() {
return err
}
out = append(out, strings.TrimPrefix(path, dir+"/"))
return nil
})
if err != nil {
panic(err)
}
return out
}
func getCompletedFragment(input string) string {
parts := strings.Split(input, "/")
completed := parts[:len(parts)-1]
return strings.Join(completed, "/")
}