forked from rliebz/tusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_completion.go
282 lines (229 loc) · 5.86 KB
/
install_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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package appcli
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
// Embed completion scripts.
_ "embed"
"github.com/rliebz/tusk/runner"
"github.com/rliebz/tusk/ui"
)
//go:embed completion/tusk-completion.bash
var rawBashCompletion string
//go:embed completion/tusk.fish
var rawFishCompletion string
//go:embed completion/_tusk
var rawZshCompletion string
const (
bashCompletionFile = "tusk-completion.bash"
fishCompletionFile = "tusk.fish"
zshCompletionFile = "_tusk"
zshInstallDir = "/usr/local/share/zsh/site-functions"
)
var bashRCFiles = []string{".bashrc", ".bash_profile", ".profile"}
// InstallCompletion installs command line tab completion for a given shell.
func InstallCompletion(meta *runner.Metadata) error {
shell := meta.InstallCompletion
switch shell {
case "bash":
return installBashCompletion(meta.Logger)
case "fish":
return installFishCompletion(meta.Logger)
case "zsh":
return installZshCompletion(meta.Logger, zshInstallDir)
default:
return fmt.Errorf("tab completion for %q is not supported", shell)
}
}
// UninstallCompletion uninstalls command line tab completion for a given shell.
func UninstallCompletion(meta *runner.Metadata) error {
shell := meta.UninstallCompletion
switch shell {
case "bash":
return uninstallBashCompletion()
case "fish":
return uninstallFishCompletion()
case "zsh":
return uninstallZshCompletion(zshInstallDir)
default:
return fmt.Errorf("tab completion for %q is not supported", shell)
}
}
func installBashCompletion(logger *ui.Logger) error {
dir, err := getDataDir()
if err != nil {
return err
}
err = installFileInDir(logger, dir, bashCompletionFile, []byte(rawBashCompletion))
if err != nil {
return err
}
rcfile, err := getBashRCFile()
if err != nil {
return err
}
slashPath := filepath.ToSlash(filepath.Join(dir, bashCompletionFile))
command := fmt.Sprintf("source %q", slashPath)
return appendIfAbsent(rcfile, command)
}
func getBashRCFile() (string, error) {
homedir, err := os.UserHomeDir()
if err != nil {
return "", err
}
for _, rcfile := range bashRCFiles {
path := filepath.Join(homedir, rcfile)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
continue
}
return "", err
}
return path, nil
}
return filepath.Join(homedir, ".bashrc"), nil
}
func appendIfAbsent(path, text string) error {
//nolint: gosec
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return err
}
defer f.Close() //nolint: errcheck
scanner := bufio.NewScanner(f)
prependNewline := false
for scanner.Scan() {
switch scanner.Text() {
case text:
return nil
case "":
prependNewline = false
default:
prependNewline = true
}
}
if serr := scanner.Err(); serr != nil {
return serr
}
if prependNewline {
text = "\n" + text
}
_, err = fmt.Fprintln(f, text)
return err
}
func uninstallBashCompletion() error {
dir, err := getDataDir()
if err != nil {
return err
}
err = uninstallFileFromDir(dir, bashCompletionFile)
if err != nil {
return err
}
rcfile, err := getBashRCFile()
if err != nil {
return err
}
re := regexp.MustCompile(fmt.Sprintf(`source ".*/%s"`, bashCompletionFile))
return removeLineInFile(rcfile, re)
}
func removeLineInFile(path string, re *regexp.Regexp) error {
rf, err := os.OpenFile(path, os.O_RDONLY, 0o644) //nolint: gosec
if err != nil {
return err
}
defer rf.Close() //nolint: errcheck
wf, err := os.CreateTemp("", ".profile.tusk.bkp")
if err != nil {
return err
}
defer wf.Close() //nolint: errcheck
scanner := bufio.NewScanner(rf)
buf := ""
for scanner.Scan() {
line := scanner.Text()
switch {
case re.MatchString(line):
continue
case line == "":
buf += "\n"
continue
}
_, err := fmt.Fprintln(wf, buf+line)
if err != nil {
return err
}
buf = ""
}
if serr := scanner.Err(); serr != nil {
return serr
}
rf.Close() //nolint: errcheck
wf.Close() //nolint: errcheck
return os.Rename(wf.Name(), path)
}
func installFishCompletion(logger *ui.Logger) error {
dir, err := getFishCompletionsDir()
if err != nil {
return err
}
return installFileInDir(logger, dir, fishCompletionFile, []byte(rawFishCompletion))
}
func uninstallFishCompletion() error {
dir, err := getFishCompletionsDir()
if err != nil {
return err
}
return uninstallFileFromDir(dir, fishCompletionFile)
}
func getDataDir() (string, error) {
if xdgHome := os.Getenv("XDG_DATA_HOME"); xdgHome != "" {
return filepath.Join(xdgHome, "tusk"), nil
}
homedir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homedir, ".local", "share", "tusk"), nil
}
// getFishCompletionsDir gets the directory to place completions in, adhering
// to the XDG base directory.
func getFishCompletionsDir() (string, error) {
if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" {
return filepath.Join(xdgHome, "fish", "completions"), nil
}
homedir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homedir, ".config", "fish", "completions"), nil
}
func installZshCompletion(logger *ui.Logger, dir string) error {
return installFileInDir(logger, dir, zshCompletionFile, []byte(rawZshCompletion))
}
func installFileInDir(logger *ui.Logger, dir, file string, content []byte) error {
//nolint: gosec
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
target := filepath.Join(dir, file)
//nolint: gosec
if err := os.WriteFile(target, content, 0o644); err != nil {
return err
}
logger.Info("Tab completion successfully installed", target)
logger.Info("You may need to restart your shell for completion to take effect")
return nil
}
func uninstallZshCompletion(dir string) error {
return uninstallFileFromDir(dir, zshCompletionFile)
}
func uninstallFileFromDir(dir, file string) error {
err := os.Remove(filepath.Join(dir, file))
if !os.IsNotExist(err) {
return err
}
return nil
}