Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tab completion functionality for Bash and Zsh #56

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/PremiereGlobal/stim/stim"
"github.com/PremiereGlobal/stim/stimpacks/aws"
"github.com/PremiereGlobal/stim/stimpacks/completion"
"github.com/PremiereGlobal/stim/stimpacks/deploy"
"github.com/PremiereGlobal/stim/stimpacks/kubernetes"
"github.com/PremiereGlobal/stim/stimpacks/pagerduty"
Expand All @@ -14,6 +15,7 @@ import (
func main() {
stim := stim.New()
stim.AddStimpack(aws.New())
stim.AddStimpack(completion.New())
stim.AddStimpack(deploy.New())
stim.AddStimpack(kubernetes.New())
stim.AddStimpack(pagerduty.New())
Expand Down
21 changes: 21 additions & 0 deletions stim/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stim

import (
"fmt"
"io"
"os"
)

func (stim *Stim) GetCompletion(shell string) error {
switch shell {
case `bash`:
stim.rootCmd.GenBashCompletion(os.Stdout)
case `zsh`:
stim.rootCmd.GenZshCompletion(os.Stdout)
io.WriteString(os.Stdout, "\ncompdef _stim stim\n")
default:
return fmt.Errorf("Unknown shell: %s", shell)
}

return nil
}
51 changes: 51 additions & 0 deletions stimpacks/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package completion

import (
"fmt"
"github.com/PremiereGlobal/stim/stim"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type Completion struct {
name string
stim *stim.Stim
}

func New() *Completion {
return &Completion{name: "completion"}
}

func (c *Completion) Name() string {
return c.name
}

func (c *Completion) BindStim(s *stim.Stim) {
c.stim = s
}

func (c *Completion) Command(viper *viper.Viper) *cobra.Command {

var cmd = &cobra.Command{
Use: "completion SHELL",
Short: "Output shell completion for the given shell (bash or zsh)",
Long: `Output shell completion for the given shell (bash or zsh)
The following ought to suffice for loading the Bash completions:
source <(stim completion bash)

Zsh is more complicated because there is more than one completion engine for
Zsh. Try putting the completion output into a script (in your $fpath) and
loading it with compinit.
stim completion zsh > /path/to/script

`,
ValidArgs: []string{"bash", "zsh"},
Run: func(cmd *cobra.Command, args []string) {
if err := c.stim.GetCompletion(args[0]); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to check that len(args) > 0 before hitting this line. stim completion causes a stack trace w/o useful messages. There might be a way to have cobra/viper do that for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try to do the arg validation a more extravagant way, but I think this will suffice. It will fail gracefully & exit for len(args) != 1 and in the switch statement in stim/completion.go I am logging out "unknown shells".

fmt.Println(err)
}
},
}

return cmd
}