Skip to content

Commit

Permalink
feat(*): Add autocomplete feature (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-pi-e authored Jul 1, 2024
1 parent cdbb624 commit 4f65def
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,27 @@ Version command prints the version of pb and the Parseable Server it is configur
```bash
pb version
```

### Add Autocomplete

To enable autocomplete for pb, run the following command according to your shell:

For bash:
```bash
pb autocomplete bash > /etc/bash_completion.d/pb
source /etc/bash_completion.d/pb
```

For zsh:
```zsh
pb autocomplete zsh > /usr/local/share/zsh/site-functions/_pb
autoload -U compinit && compinit
```

For powershell
```powershell
pb autocomplete powershell > $env:USERPROFILE\Documents\PowerShell\pb_complete.ps1
. $PROFILE
```


52 changes: 52 additions & 0 deletions cmd/autocomplete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

// Copyright (c) 2024 Parseable, Inc
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// AutocompleteCmd represents the autocomplete command
var AutocompleteCmd = &cobra.Command{
Use: "autocomplete [bash|zsh|powershell]",
Short: "Generate autocomplete script",
Long: `Generate autocomplete script for bash, zsh, or powershell`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
err = fmt.Errorf("unsupported shell type: %s. Only bash, zsh, and powershell are supported", args[0])
}

if err != nil {
return fmt.Errorf("error generating autocomplete script: %w", err)
}

return nil
},
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func main() {
cli.AddCommand(role)
cli.AddCommand(cmd.TailCmd)

cli.AddCommand(cmd.AutocompleteCmd)

// Set as command
cmd.VersionCmd.Run = func(_ *cobra.Command, _ []string) {
cmd.PrintVersion(Version, Commit)
Expand Down

0 comments on commit 4f65def

Please sign in to comment.