2019-02-05 15:51:21 +03:00
package commands
import (
"fmt"
"os"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2022-11-07 14:46:08 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
2019-02-05 15:51:21 +03:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const completionCmdExample = ` # Bash
# Linux
# Add Bash completion file using :
2020-08-18 11:12:58 +03:00
$ sudo hasura completion bash -- file = / etc / bash_completion . d / hasura
2019-02-05 15:51:21 +03:00
# Mac
# Install bash - completion using homebrew :
$ brew install bash - completion
# Add to your ~ / . bash_profile :
if [ - f $ ( brew -- prefix ) / etc / bash_completion ] ; then
. $ ( brew -- prefix ) / etc / bash_completion
fi
# Add the completion file :
$ sudo hasura completion bash -- file = $ ( brew -- prefix ) / etc / bash_completion . d / hasura
# Windows ( Git Bash )
# open git bash
$ mkdir - p ~ / . bash_completion . d
# Add the completion file :
2019-03-28 04:58:20 +03:00
$ cd ~ && hasura completion bash -- file = . bash_completion . d / hasura
2019-02-05 15:51:21 +03:00
# Add the following to ~ / . bash_profile
if [ - f ~ / . bash_completion . d / hasura ] ; then
. ~ / . bash_completion . d / hasura
fi
# restart git bash
# Zsh ( using oh - my - zsh )
$ mkdir - p $ HOME / . oh - my - zsh / completions
$ hasura completion zsh -- file = $ HOME / . oh - my - zsh / completions / _hasura
# Reload the shell for the changes to take effect ! `
// NewCompletionCmd return the completion command.
func NewCompletionCmd ( ec * cli . ExecutionContext ) * cobra . Command {
opts := & completionOptions {
EC : ec ,
}
completionCmd := & cobra . Command {
Use : "completion [shell]" ,
2022-12-30 06:50:48 +03:00
Short : "Generate auto-completion code" ,
2019-02-05 15:51:21 +03:00
Args : cobra . ExactArgs ( 1 ) ,
2022-12-30 06:50:48 +03:00
Long : "Depending on your shell (bash or zsh), running `hasura completion [shell]` will generate the auto-completion code for the Hasura CLI. You can then add this to your shell config to enable auto-completion, which will allow you to tab through the available commands and options." ,
2019-02-05 15:51:21 +03:00
SilenceUsage : true ,
Example : completionCmdExample ,
PreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-07 14:46:08 +03:00
op := genOpName ( cmd , "PreRunE" )
2019-02-05 15:51:21 +03:00
ec . Viper = viper . New ( )
2022-11-07 14:46:08 +03:00
if err := ec . Prepare ( ) ; err != nil {
return errors . E ( op , err )
}
return nil
2019-02-05 15:51:21 +03:00
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-07 14:46:08 +03:00
op := genOpName ( cmd , "RunE" )
2019-02-05 15:51:21 +03:00
opts . Shell = args [ 0 ]
opts . Cmd = cmd
2022-11-07 14:46:08 +03:00
if err := opts . run ( ) ; err != nil {
return errors . E ( op , err )
}
return nil
2019-02-05 15:51:21 +03:00
} ,
}
completionCmd . Flags ( ) . StringVar ( & opts . File , "file" , "" , "file to which output has to be written" )
return completionCmd
}
type completionOptions struct {
EC * cli . ExecutionContext
Shell string
File string
Cmd * cobra . Command
}
func ( o * completionOptions ) run ( ) error {
2022-11-07 14:46:08 +03:00
var op errors . Op = "commands.completionOptions.run"
2019-02-05 15:51:21 +03:00
var err error
switch o . Shell {
case "bash" :
if o . File != "" {
err = o . Cmd . Root ( ) . GenBashCompletionFile ( o . File )
} else {
err = o . Cmd . Root ( ) . GenBashCompletion ( os . Stdout )
}
case "zsh" :
if o . File != "" {
err = o . Cmd . Root ( ) . GenZshCompletionFile ( o . File )
} else {
err = o . Cmd . Root ( ) . GenZshCompletion ( os . Stdout )
}
default :
2021-10-13 17:38:07 +03:00
err = fmt . Errorf ( "unknown shell: %s. Use bash or zsh" , o . Shell )
2019-02-05 15:51:21 +03:00
}
if err != nil {
2022-11-07 14:46:08 +03:00
return errors . E ( op , err )
2019-02-05 15:51:21 +03:00
}
return nil
}