Skip to content

Commit 061f337

Browse files
authored
Merge pull request #53 from nao1215/add-completion-cmd
Add completion subcommand
2 parents 5e7edb9 + 2a6e6a4 commit 061f337

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ $ gup export --output > gup.conf
129129
$ gup import --input=gup.conf
130130
```
131131

132-
### Auto-generate shell completion file (for bash, zsh, fish)
133-
gup command automatically generates shell completion files for bash, zsh, and fish. After the user executes gup, if the shell completion file does not exist in the system, the auto-generation process will begin. To activate the completion feature, restart the shell.
132+
### Generate shell completion file (for bash, zsh, fish)
133+
completion subcommand generates shell completion files for bash, zsh, and fish. If the shell completion file does not exist in the system, the generation process will begin. To activate the completion feature, restart the shell.
134134

135135
```
136-
$ gup
136+
$ gup completion
137137
gup:INFO : create bash-completion file: /home/nao/.bash_completion
138138
gup:INFO : create fish-completion file: /home/nao/.config/fish/completions/gup.fish
139139
gup:INFO : create zsh-completion file: /home/nao/.zsh/completion/_gup

cmd/completion.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"github.com/nao1215/gup/internal/completion"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var completionCmd = &cobra.Command{
9+
Use: "completion",
10+
Short: "Create shell completion files (bash, fish, zsh) for the gup",
11+
Long: `Create shell completion files (bash, fish, zsh) for the gup command
12+
if it is not already on the system`,
13+
Run: func(cmd *cobra.Command, args []string) {
14+
completion.DeployShellCompletionFileIfNeeded(rootCmd)
15+
},
16+
}
17+
18+
func init() {
19+
rootCmd.AddCommand(completionCmd)
20+
}

cmd/root.go

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66

77
"github.com/nao1215/gup/internal/assets"
8-
"github.com/nao1215/gup/internal/completion"
98
"github.com/nao1215/gup/internal/print"
109
"github.com/spf13/cobra"
1110
)
@@ -22,8 +21,6 @@ var OsExit = os.Exit
2221
// Execute run gup process.
2322
func Execute() {
2423
assets.DeployIconIfNeeded()
25-
completion.DeployShellCompletionFileIfNeeded(rootCmd)
26-
2724
rootCmd.CompletionOptions.DisableDefaultCmd = true
2825
if err := rootCmd.Execute(); err != nil {
2926
print.Err(err)

cmd/root_test.go

+41-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/google/go-cmp/cmp"
14+
"github.com/nao1215/gup/internal/cmdinfo"
1415
"github.com/nao1215/gup/internal/config"
1516
"github.com/nao1215/gup/internal/file"
1617
"github.com/nao1215/gup/internal/goutil"
@@ -517,16 +518,6 @@ func TestExecute_Import_WithInputOption(t *testing.T) {
517518
}
518519
}
519520

520-
defer func() {
521-
os.RemoveAll(filepath.Join("testdata", ".config", "fish"))
522-
os.RemoveAll(filepath.Join("testdata", ".zsh"))
523-
os.RemoveAll(filepath.Join("testdata", ".zshrc"))
524-
os.RemoveAll(filepath.Join("testdata", ".bash_completion"))
525-
os.RemoveAll(filepath.Join("testdata", ".config", "gup", "assets"))
526-
os.RemoveAll(filepath.Join("testdata", "go"))
527-
os.RemoveAll(filepath.Join("testdata", ".cache"))
528-
}()
529-
530521
orgStdout := print.Stdout
531522
orgStderr := print.Stderr
532523
pr, pw, err := os.Pipe()
@@ -687,16 +678,6 @@ func TestExecute_Update(t *testing.T) {
687678
}
688679
}
689680

690-
defer func() {
691-
os.RemoveAll(filepath.Join("testdata", ".config", "fish"))
692-
os.RemoveAll(filepath.Join("testdata", ".zsh"))
693-
os.RemoveAll(filepath.Join("testdata", ".zshrc"))
694-
os.RemoveAll(filepath.Join("testdata", ".bash_completion"))
695-
os.RemoveAll(filepath.Join("testdata", ".config", "gup", "assets"))
696-
os.RemoveAll(filepath.Join("testdata", "go"))
697-
os.RemoveAll(filepath.Join("testdata", ".cache"))
698-
}()
699-
700681
orgStdout := print.Stdout
701682
orgStderr := print.Stderr
702683
pr, pw, err := os.Pipe()
@@ -791,16 +772,6 @@ func TestExecute_Update_DryRun(t *testing.T) {
791772
}
792773
}
793774

794-
defer func() {
795-
os.RemoveAll(filepath.Join("testdata", ".config", "fish"))
796-
os.RemoveAll(filepath.Join("testdata", ".zsh"))
797-
os.RemoveAll(filepath.Join("testdata", ".zshrc"))
798-
os.RemoveAll(filepath.Join("testdata", ".bash_completion"))
799-
os.RemoveAll(filepath.Join("testdata", ".config", "gup", "assets"))
800-
os.RemoveAll(filepath.Join("testdata", "go"))
801-
os.RemoveAll(filepath.Join("testdata", ".cache"))
802-
}()
803-
804775
orgStdout := print.Stdout
805776
orgStderr := print.Stderr
806777
pr, pw, err := os.Pipe()
@@ -834,3 +805,43 @@ func TestExecute_Update_DryRun(t *testing.T) {
834805
t.Errorf("failed to update posixer command")
835806
}
836807
}
808+
809+
func TestExecute_Completion(t *testing.T) {
810+
t.Run("generate completion file", func(t *testing.T) {
811+
os.Args = []string{"gup", "completion"}
812+
Execute()
813+
814+
bash := filepath.Join(os.Getenv("HOME"), ".bash_completion")
815+
if runtime.GOOS == "windows" {
816+
if file.IsFile(bash) {
817+
t.Errorf("generate %s, however shell completion file is not generated on Windows", bash)
818+
}
819+
} else {
820+
if !file.IsFile(bash) {
821+
t.Errorf("failed to generate %s", bash)
822+
}
823+
}
824+
825+
fish := filepath.Join(os.Getenv("HOME"), ".config", "fish", "completions", cmdinfo.Name+".fish")
826+
if runtime.GOOS == "windows" {
827+
if file.IsFile(fish) {
828+
t.Errorf("generate %s, however shell completion file is not generated on Windows", fish)
829+
}
830+
} else {
831+
if !file.IsFile(fish) {
832+
t.Errorf("failed to generate %s", fish)
833+
}
834+
}
835+
836+
zsh := filepath.Join(os.Getenv("HOME"), ".zsh", "completion", "_"+cmdinfo.Name)
837+
if runtime.GOOS == "windows" {
838+
if file.IsFile(zsh) {
839+
t.Errorf("generate %s, however shell completion file is not generated on Windows", zsh)
840+
}
841+
} else {
842+
if !file.IsFile(zsh) {
843+
t.Errorf("failed to generate %s", zsh)
844+
}
845+
}
846+
})
847+
}

0 commit comments

Comments
 (0)