Skip to content

Commit

Permalink
Fix bash completion bugs (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnr committed Jul 13, 2023
1 parent b21ddf1 commit 30a73b2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
4 changes: 1 addition & 3 deletions Makefile
Expand Up @@ -17,10 +17,8 @@ GOARCH := $(shell go env GOARCH)
endif

# go.opentelemetry.io/otel/sdk/metric@v0.31.0 - there are breaking changes in v0.32.0.
# github.com/urfave/cli/v2@v2.23.6 - newer version regressed reading JSON values in subcommands. TODO apply this to subcommands https://github.com/urfave/cli/commit/dc6dfb7851fbaa6519a9691ac921c9c7e072abc8#diff-6c4b6ed7dc8834cef100f50dae61c30ffe7775a3f3f6f5a557517cb740c44a2dR237
PINNED_DEPENDENCIES := \
go.opentelemetry.io/otel/sdk/metric@v0.31.0 \
github.com/urfave/cli/v2@v2.23.6
go.opentelemetry.io/otel/sdk/metric@v0.31.0

##### Build #####

Expand Down
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -169,7 +169,7 @@ See the CLI docs for a [list of env vars](https://docs.temporal.io/cli#environme

The Temporal CLI has the capability to auto-complete commands.

Running `temporal completion SHELL` will output the related completion SHELL code.
Running `temporal completion SHELL` will output completion setup code for the given shell.

### zsh auto-completion

Expand All @@ -187,7 +187,7 @@ If you're running auto-completion from the terminal, run the command below:
echo 'source <(temporal completion zsh)' >> ~/.zshrc
```

After setting the variable, run:
After editing the file, run:

`source ~/.zshrc`.

Expand Down Expand Up @@ -219,6 +219,12 @@ It should say `_init_completion is a function` and print the function.

Enable completion for Temporal by adding the following code to your bash file:

```bash
source <(temporal completion bash)
```

In an existing terminal, you can do that by running:

```bash
echo 'source <(temporal completion bash)' >> ~/.bashrc
source ~/.bashrc
Expand All @@ -227,8 +233,8 @@ source ~/.bashrc
Now test by typing `temporal`, space, and then tab twice. You should see:

```bash
$ temporal
activity completion h operator server workflow
$ temporal
activity completion h operator server workflow
batch env help schedule task-queue
```

Expand Down
37 changes: 24 additions & 13 deletions completion/completion.go
Expand Up @@ -7,45 +7,56 @@ import (
"github.com/urfave/cli/v2"
)

// taken from https://github.com/urfave/cli/blob/master/autocomplete/zsh_autocomplete
var zsh_script = `
#compdef temporal
// taken from https://github.com/urfave/cli/blob/v2-maint/autocomplete/zsh_autocomplete
var zsh_script = `#compdef temporal
_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
return
}
compdef _cli_zsh_autocomplete temporal
`

var bash_script = `
#! /bin/bash
// taken from https://github.com/urfave/cli/blob/v2-maint/autocomplete/bash_autocomplete
var bash_script = `#! /bin/bash
# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}
_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
requestComp="${words[*]} ${cur} --generate-bash-completion"
else
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
requestComp="${words[*]} --generate-bash-completion"
fi
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete temporal
complete -o bashdefault -o default -F _cli_bash_autocomplete temporal
`

func NewCompletionCommands() []*cli.Command {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -15,7 +15,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/temporalio/tctl-kit v0.0.0-20230328153839-577f95d16fa0
github.com/temporalio/ui-server/v2 v2.16.2
github.com/urfave/cli/v2 v2.23.6
github.com/urfave/cli/v2 v2.25.7
go.temporal.io/api v1.23.0
go.temporal.io/sdk v1.23.1
go.temporal.io/sdk/contrib/tools/workflowcheck v0.0.0-20230328164709-88a40de39c33
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1090,8 +1090,8 @@ github.com/uber/jaeger-client-go v2.22.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/urfave/cli/v2 v2.23.6 h1:iWmtKD+prGo1nKUtLO0Wg4z9esfBM4rAV4QRLQiEmJ4=
github.com/urfave/cli/v2 v2.23.6/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
Expand Down

0 comments on commit 30a73b2

Please sign in to comment.