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

Conversation

srfrog
Copy link
Contributor

@srfrog srfrog commented Mar 1, 2019

This PR adds a new completion command to the Dgraph main binary. It is used to generate bash and zsh shell completion code.

Bash example:

$ dgraph completion bash > dgraph.sh

# source in .bashrc
source dgraph.sh

This change is Reviewable

@srfrog srfrog requested a review from a team March 1, 2019 23:17
. < (dgraph completion)
`,
Run: func(cmd *cobra.Command, args []string) {
RootCmd.GenBashCompletion(os.Stdout)

Choose a reason for hiding this comment

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

Error return value of RootCmd.GenBashCompletion is not checked (from errcheck)

@danielmai
Copy link
Contributor

danielmai commented Mar 1, 2019

Tried it out. It seems pretty useful. Here's what it like it action (<TAB> means I pressed the tab key):

$ dgraph <TAB><TAB>
acl              bash-completion  cert             debug            live             version          
alpha            bulk             conv             increment        restore          zero             
$ dgraph alpha -<TAB><TAB>
--acl_access_ttl=      --badger.vlog=         --expand_edge          --log_dir=             -p                     --tls_dir=             --wal=
--acl_cache_ttl=       --bindall              --export=              --logtostderr          --pending_proposals=   --tls_use_system_ca    --whitelist=
--acl_refresh_ttl=     --block_rate=          --expose_trace         --lru_mb=              --port_offset=         --trace=               -z
--acl_secret_file=     --config=              --idx=                 --max_retries=         --postings=            -v                     --zero=
--alsologtostderr      --custom_tokenizers=   --jaeger.collector=    --mutations=           --profile_mode=        --v=                   
--auth_token=          --debugmode            -l                     --my=                  --query_edge_limit=    --vmodule=             
--badger.tables=       --enterprise_features  --log_backtrace_at=    -o                     --tls_client_auth=     -w       

dgraph completion command with two subcommands for bash and zsh.
@danielmai
Copy link
Contributor

Zsh completion isn't working for me.

$ ./dgraph completion zsh
#compdef dgraph

_arguments \
  '1: :->level1' \
  '2: :->level2' \
  '3: :_files'
case $state in
  level1)
    case $words[1] in
      dgraph)
        _arguments '1: :(acl alpha bulk cert completion conv debug help increment live restore version zero)'
      ;;
      *)
        _arguments '*: :_files'
      ;;
    esac
  ;;
  level2)
    case $words[2] in
      acl)
        _arguments '2: :(chmod groupadd groupdel info passwd useradd userdel usermod)'
      ;;
      cert)
        _arguments '2: :(ls)'
      ;;
      completion)
        _arguments '2: :(bash zsh)'
      ;;
      *)
        _arguments '*: :_files'
      ;;
    esac
  ;;
  *)
    _arguments '*: :_files'
  ;;
esac
$ source <(./dgraph completion zsh)
_arguments:comparguments:319: can only be called from completion function
_arguments:comparguments:319: can only be called from completion function

Copy link
Contributor

@codexnull codexnull left a comment

Choose a reason for hiding this comment

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

I agree that shell completion would be helpful but I'm not sure that it should be a core dgraph command. The output is fixed for a given dgraph release. The same could be accomplished by just adding static completion files to the release package.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @srfrog)

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

:lgtm: This is useful. Either the user or our install scripts can use this to set up auto-completion based on whichever version dgraph is.

Reviewed 1 of 1 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @srfrog)

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @golangcibot)


dgraph/cmd/root.go, line 167 at r1 (raw file):

Previously, golangcibot (Bot from GolangCI) wrote…

Error return value of RootCmd.GenBashCompletion is not checked (from errcheck)

Done.

@srfrog srfrog marked this pull request as ready for review March 11, 2019 19:51
@srfrog srfrog merged commit 9bb6909 into master Mar 11, 2019
@srfrog srfrog deleted the srfrog/issue-3083_autocomplete_dgraph_tools branch March 11, 2019 19:52
dna2github pushed a commit to dna2fork/dgraph that referenced this pull request Jul 19, 2019
* dgraph/cmd/root.go: add bash shell completion command

* dgraph/cmd/root.go: add zsh shell completion

dgraph completion command with two subcommands for bash and zsh.

* dgraph/cmd/root.go: add usage instructions and help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants