Skip to content

Commit

Permalink
Merge pull request #73 from nao1215/change-bash-completion-file-path
Browse files Browse the repository at this point in the history
Issue #72: Change bash completion file path
  • Loading branch information
nao1215 authored Feb 23, 2023
2 parents 360e9b8 + 0a081f4 commit 60484c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func TestExecute_Completion(t *testing.T) {
os.Args = []string{"gup", "completion"}
Execute()

bash := filepath.Join(os.Getenv("HOME"), ".bash_completion")
bash := filepath.Join(os.Getenv("HOME"), ".bash_completions.d", cmdinfo.Name)
if runtime.GOOS == "windows" {
if file.IsFile(bash) {
t.Errorf("generate %s, however shell completion file is not generated on Windows", bash)
Expand Down
29 changes: 8 additions & 21 deletions internal/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,27 @@ func makeBashCompletionFileIfNeeded(cmd *cobra.Command) {
return
}

if !file.IsFile(path) {
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
print.Err(fmt.Errorf("can not open .bash_completion: %w", err))
return
}

if _, err := fp.WriteString(bashCompletion.String()); err != nil {
print.Err(fmt.Errorf("can not write .bash_completion %w", err))
return
}

if err := fp.Close(); err != nil {
print.Err(fmt.Errorf("can not close .bash_completion %w", err))
if !file.IsDir(path) {
if err := os.MkdirAll(filepath.Dir(path), 0775); err != nil {
print.Err(fmt.Errorf("can not create bash-completion file: %w", err))
return
}
return
}

fp, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0664)
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
print.Err(fmt.Errorf("can not open .bash_completion: %w", err))
return
}

if _, err := fp.WriteString(bashCompletion.String()); err != nil {
print.Err(fmt.Errorf("can not append .bash_completion: %w", err))
print.Err(fmt.Errorf("can not write .bash_completion %w", err))
return
}

if err := fp.Close(); err != nil {
print.Err(fmt.Errorf("can not close .bash_completion: %w", err))
return
print.Err(fmt.Errorf("can not close .bash_completion %w", err))
}
return
}

func makeFishCompletionFileIfNeeded(cmd *cobra.Command) {
Expand Down Expand Up @@ -235,7 +222,7 @@ func isSameZshCompletionFile(cmd *cobra.Command) bool {

// bashCompletionFilePath return bash-completion file path.
func bashCompletionFilePath() string {
return filepath.Join(os.Getenv("HOME"), ".bash_completion")
return filepath.Join(os.Getenv("HOME"), ".bash_completions.d", cmdinfo.Name)
}

// fishCompletionFilePath return fish-completion file path.
Expand Down

0 comments on commit 60484c4

Please sign in to comment.