-
Notifications
You must be signed in to change notification settings - Fork 10
/
init_bashcomp.go
110 lines (85 loc) · 2.55 KB
/
init_bashcomp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package command
import (
"fmt"
"os"
"path"
"strings"
"github.com/spf13/cobra"
"github.com/simplesurance/baur/fs"
"github.com/simplesurance/baur/log"
)
const bashCompLongHelp = `
Installs a bash completion file for baur.
The bash_completion file is written into the user's bash completion directory.
Environment Variables:
BASH_COMPLETION_USER_DIR Destination directory
`
// bashCompCmd represents the completion command
var bashCompCmd = &cobra.Command{
Use: "bashcomp",
Short: "generate and install a bash completion file for baur",
Long: strings.TrimSpace(bashCompLongHelp),
Run: bashComp,
}
func init() {
initCmd.AddCommand(bashCompCmd)
}
func getBashCompletionDir() string {
var exist bool
/*
https://github.com/scop/bash-completion/blob/master/README.md
Q. Where should I install my own local completions?
A. Put them in the completions subdir of $BASH_COMPLETION_USER_DIR (defaults to
$XDG_DATA_HOME/bash-completion or ~/.local/share/bash-completion if
$XDG_DATA_HOME is not set) to have them loaded on demand. See also the next
question's answer for considerations for these files' names, they apply here as
well. Alternatively, you can write them directly in ~/.bash_completion which is
loaded eagerly by our main script.
*/
if path, exist := os.LookupEnv("BASH_COMPLETION_USER_DIR"); exist {
return path
}
var xdgHome string
if xdgHome, exist = os.LookupEnv("XDG_DATA_HOME"); exist {
return path.Join(xdgHome, "bash-completion/completions")
}
if home, exist := os.LookupEnv("HOME"); exist {
return path.Join(home, ".local/share/bash-completion/completions")
}
return "~/.local/share/bash-completion/completions"
}
func mustCreatebashComplDir(path string) {
isDir, err := fs.IsDir(path)
if err == nil {
if isDir {
return
}
if !isDir {
log.Fatalf("'%s' must be a directory", path)
}
}
if !os.IsNotExist(err) {
log.Fatalln(err)
}
err = fs.Mkdir(path)
if err != nil {
log.Fatalf("could not create bash completion dir '%s': %s", path, err)
}
}
func bashComp(cmd *cobra.Command, args []string) {
complDir := getBashCompletionDir()
mustCreatebashComplDir(complDir)
complFile := path.Join(complDir, "baur")
f, err := os.Create(complFile)
if err != nil {
log.Fatalf("Creating '%s' failed: %s", complFile, err)
}
err = rootCmd.GenBashCompletion(f)
if err != nil {
log.Fatalln("generating bash completion failed:", err.Error())
}
if err := f.Close(); err != nil {
log.Fatalln("closing file failed:", err.Error())
}
fmt.Printf("bash completion file written to %s\n", highlight(complFile))
}