Skip to content

Commit 60484c4

Browse files
authored
Merge pull request #73 from nao1215/change-bash-completion-file-path
Issue #72: Change bash completion file path
2 parents 360e9b8 + 0a081f4 commit 60484c4

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

cmd/root_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ func TestExecute_Completion(t *testing.T) {
770770
os.Args = []string{"gup", "completion"}
771771
Execute()
772772

773-
bash := filepath.Join(os.Getenv("HOME"), ".bash_completion")
773+
bash := filepath.Join(os.Getenv("HOME"), ".bash_completions.d", cmdinfo.Name)
774774
if runtime.GOOS == "windows" {
775775
if file.IsFile(bash) {
776776
t.Errorf("generate %s, however shell completion file is not generated on Windows", bash)

internal/completion/completion.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,27 @@ func makeBashCompletionFileIfNeeded(cmd *cobra.Command) {
4141
return
4242
}
4343

44-
if !file.IsFile(path) {
45-
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0664)
46-
if err != nil {
47-
print.Err(fmt.Errorf("can not open .bash_completion: %w", err))
48-
return
49-
}
50-
51-
if _, err := fp.WriteString(bashCompletion.String()); err != nil {
52-
print.Err(fmt.Errorf("can not write .bash_completion %w", err))
53-
return
54-
}
55-
56-
if err := fp.Close(); err != nil {
57-
print.Err(fmt.Errorf("can not close .bash_completion %w", err))
44+
if !file.IsDir(path) {
45+
if err := os.MkdirAll(filepath.Dir(path), 0775); err != nil {
46+
print.Err(fmt.Errorf("can not create bash-completion file: %w", err))
5847
return
5948
}
60-
return
6149
}
62-
63-
fp, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0664)
50+
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0664)
6451
if err != nil {
6552
print.Err(fmt.Errorf("can not open .bash_completion: %w", err))
6653
return
6754
}
6855

6956
if _, err := fp.WriteString(bashCompletion.String()); err != nil {
70-
print.Err(fmt.Errorf("can not append .bash_completion: %w", err))
57+
print.Err(fmt.Errorf("can not write .bash_completion %w", err))
7158
return
7259
}
7360

7461
if err := fp.Close(); err != nil {
75-
print.Err(fmt.Errorf("can not close .bash_completion: %w", err))
76-
return
62+
print.Err(fmt.Errorf("can not close .bash_completion %w", err))
7763
}
64+
return
7865
}
7966

8067
func makeFishCompletionFileIfNeeded(cmd *cobra.Command) {
@@ -235,7 +222,7 @@ func isSameZshCompletionFile(cmd *cobra.Command) bool {
235222

236223
// bashCompletionFilePath return bash-completion file path.
237224
func bashCompletionFilePath() string {
238-
return filepath.Join(os.Getenv("HOME"), ".bash_completion")
225+
return filepath.Join(os.Getenv("HOME"), ".bash_completions.d", cmdinfo.Name)
239226
}
240227

241228
// fishCompletionFilePath return fish-completion file path.

0 commit comments

Comments
 (0)