Skip to content

Commit 3bfa054

Browse files
committed
New command shell completion
1 parent dabb1df commit 3bfa054

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,54 @@ Import profile from JSON format
6767
cat home.json | xargs -0 git profile import home
6868
```
6969

70+
## Shell Completion
71+
72+
Shell completion is available for `git-profile` command.
73+
74+
**Note:** Use `git-profile TAB` instead of `git profile TAB` for completion.
75+
76+
### Bash
77+
78+
Requires [bash-completion](https://github.com/scop/bash-completion).
79+
80+
```bash
81+
# Temporary (current session only)
82+
source <(git-profile completion bash)
83+
84+
# Permanent (Linux)
85+
git-profile completion bash | sudo tee /etc/bash_completion.d/git-profile
86+
87+
# Permanent (macOS)
88+
git-profile completion bash > $(brew --prefix)/etc/bash_completion.d/git-profile
89+
```
90+
91+
### Zsh
92+
93+
```bash
94+
# Create completion directory if it doesn't exist
95+
mkdir -p ~/.zsh/completions
96+
97+
# Generate completion file
98+
git-profile completion zsh > ~/.zsh/completions/_git-profile
99+
100+
# Add to ~/.zshrc if not already present
101+
echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
102+
echo "autoload -U compinit && compinit" >> ~/.zshrc
103+
104+
# Reload shell
105+
source ~/.zshrc
106+
```
107+
108+
### Fish
109+
110+
```bash
111+
# Temporary (current session only)
112+
git-profile completion fish | source
113+
114+
# Permanent
115+
git-profile completion fish > ~/.config/fish/completions/git-profile.fish
116+
```
117+
70118
## License
71119

72120
http://www.opensource.org/licenses/mit-license.php

cmd/completion.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Completion returns `completion` command
10+
func Completion(rootCmd *cobra.Command) *cobra.Command {
11+
return &cobra.Command{
12+
Use: "completion [bash|zsh|fish|powershell]",
13+
Short: "Generate completion script",
14+
Long: `Generate shell completion script for Git Profile.
15+
16+
To load completions:
17+
18+
Bash:
19+
20+
$ source <(git-profile completion bash)
21+
22+
# To load completions for each session, execute once:
23+
# Linux:
24+
$ git-profile completion bash > /etc/bash_completion.d/git-profile
25+
# macOS:
26+
$ git-profile completion bash > $(brew --prefix)/etc/bash_completion.d/git-profile
27+
28+
Zsh:
29+
30+
# Create completion directory and generate file:
31+
$ mkdir -p ~/.zsh/completions
32+
$ git-profile completion zsh > ~/.zsh/completions/_git-profile
33+
34+
# Add to ~/.zshrc if not already present:
35+
$ echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
36+
$ echo 'autoload -U compinit && compinit' >> ~/.zshrc
37+
38+
# Reload shell or run: source ~/.zshrc
39+
40+
Fish:
41+
42+
$ git-profile completion fish | source
43+
44+
# To load completions for each session, execute once:
45+
$ git-profile completion fish > ~/.config/fish/completions/git-profile.fish
46+
`,
47+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
48+
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
49+
DisableFlagsInUseLine: true,
50+
Run: func(cmd *cobra.Command, args []string) {
51+
switch args[0] {
52+
case "bash":
53+
_ = rootCmd.GenBashCompletionV2(os.Stdout, true)
54+
case "zsh":
55+
_ = rootCmd.GenZshCompletion(os.Stdout)
56+
case "fish":
57+
_ = rootCmd.GenFishCompletion(os.Stdout, true)
58+
case "powershell":
59+
_ = rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
60+
}
61+
},
62+
}
63+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (c *Cmd) init() {
8080

8181
c.AddCommand(
8282
Add(c.config),
83+
Completion(&c.Command),
8384
Current(c.config, c.git),
8485
Del(c.config),
8586
List(c.config),

0 commit comments

Comments
 (0)