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

Add bash shell completion command #3084

Merged
merged 3 commits into from
Mar 11, 2019
Merged
Changes from 2 commits
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
51 changes: 51 additions & 0 deletions dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"flag"
"os"
"strings"

"github.com/dgraph-io/dgraph/dgraph/cmd/alpha"
Expand Down Expand Up @@ -105,6 +106,9 @@ func initCmds() {
// environment variable.
sc.Conf.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
// For bash shell completion
RootCmd.AddCommand(shellCompletionCmd())

cobra.OnInitialize(func() {
cfg := rootConf.GetString("config")
if cfg == "" {
Expand Down Expand Up @@ -145,3 +149,50 @@ func setGlogFlags(conf *viper.Viper) {
}
}
}

func shellCompletionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "completion",
Short: "Generates shell completion scripts for bash or zsh",
}

// bash subcommand
cmd.AddCommand(&cobra.Command{
Use: "bash",
Short: "bash shell completion",
Long: `To load bash completion run:
. < (dgraph completion bash)

To configure your bash shell to load completions for each session,
add to your bashrc:

# ~/.bashrc or ~/.profile
. < (dgraph completion bash)
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return RootCmd.GenBashCompletion(os.Stdout)
},
})

// zsh subcommand
cmd.AddCommand(&cobra.Command{
Use: "zsh",
Short: "zsh shell completion",
Long: `To load bash completion run:
. < (dgraph completion zsh)

To configure your zsh shell to load completions for each session,
add to your zshrc:

# ~/.zshrc or ~/.profile
. < (dgraph completion zsh)
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return RootCmd.GenZshCompletion(os.Stdout)
},
})

return cmd
}