Skip to content

Commit 4f65def

Browse files
authored
feat(*): Add autocomplete feature (#41)
1 parent cdbb624 commit 4f65def

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,27 @@ Version command prints the version of pb and the Parseable Server it is configur
113113
```bash
114114
pb version
115115
```
116+
117+
### Add Autocomplete
118+
119+
To enable autocomplete for pb, run the following command according to your shell:
120+
121+
For bash:
122+
```bash
123+
pb autocomplete bash > /etc/bash_completion.d/pb
124+
source /etc/bash_completion.d/pb
125+
```
126+
127+
For zsh:
128+
```zsh
129+
pb autocomplete zsh > /usr/local/share/zsh/site-functions/_pb
130+
autoload -U compinit && compinit
131+
```
132+
133+
For powershell
134+
```powershell
135+
pb autocomplete powershell > $env:USERPROFILE\Documents\PowerShell\pb_complete.ps1
136+
. $PROFILE
137+
```
138+
139+

cmd/autocomplete.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// Copyright (c) 2024 Parseable, Inc
3+
//
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
package cmd
19+
20+
import (
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// AutocompleteCmd represents the autocomplete command
28+
var AutocompleteCmd = &cobra.Command{
29+
Use: "autocomplete [bash|zsh|powershell]",
30+
Short: "Generate autocomplete script",
31+
Long: `Generate autocomplete script for bash, zsh, or powershell`,
32+
Args: cobra.ExactArgs(1),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
var err error
35+
switch args[0] {
36+
case "bash":
37+
err = cmd.Root().GenBashCompletion(os.Stdout)
38+
case "zsh":
39+
err = cmd.Root().GenZshCompletion(os.Stdout)
40+
case "powershell":
41+
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
42+
default:
43+
err = fmt.Errorf("unsupported shell type: %s. Only bash, zsh, and powershell are supported", args[0])
44+
}
45+
46+
if err != nil {
47+
return fmt.Errorf("error generating autocomplete script: %w", err)
48+
}
49+
50+
return nil
51+
},
52+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func main() {
112112
cli.AddCommand(role)
113113
cli.AddCommand(cmd.TailCmd)
114114

115+
cli.AddCommand(cmd.AutocompleteCmd)
116+
115117
// Set as command
116118
cmd.VersionCmd.Run = func(_ *cobra.Command, _ []string) {
117119
cmd.PrintVersion(Version, Commit)

0 commit comments

Comments
 (0)