Skip to content

Commit 30a73b2

Browse files
authored
Fix bash completion bugs (#280)
1 parent b21ddf1 commit 30a73b2

5 files changed

Lines changed: 38 additions & 23 deletions

File tree

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ GOARCH := $(shell go env GOARCH)
1717
endif
1818

1919
# go.opentelemetry.io/otel/sdk/metric@v0.31.0 - there are breaking changes in v0.32.0.
20-
# 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
2120
PINNED_DEPENDENCIES := \
22-
go.opentelemetry.io/otel/sdk/metric@v0.31.0 \
23-
github.com/urfave/cli/v2@v2.23.6
21+
go.opentelemetry.io/otel/sdk/metric@v0.31.0
2422

2523
##### Build #####
2624

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ See the CLI docs for a [list of env vars](https://docs.temporal.io/cli#environme
169169

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

172-
Running `temporal completion SHELL` will output the related completion SHELL code.
172+
Running `temporal completion SHELL` will output completion setup code for the given shell.
173173

174174
### zsh auto-completion
175175

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

190-
After setting the variable, run:
190+
After editing the file, run:
191191

192192
`source ~/.zshrc`.
193193

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

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

222+
```bash
223+
source <(temporal completion bash)
224+
```
225+
226+
In an existing terminal, you can do that by running:
227+
222228
```bash
223229
echo 'source <(temporal completion bash)' >> ~/.bashrc
224230
source ~/.bashrc
@@ -227,8 +233,8 @@ source ~/.bashrc
227233
Now test by typing `temporal`, space, and then tab twice. You should see:
228234

229235
```bash
230-
$ temporal
231-
activity completion h operator server workflow
236+
$ temporal
237+
activity completion h operator server workflow
232238
batch env help schedule task-queue
233239
```
234240

completion/completion.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,56 @@ import (
77
"github.com/urfave/cli/v2"
88
)
99

10-
// taken from https://github.com/urfave/cli/blob/master/autocomplete/zsh_autocomplete
11-
var zsh_script = `
12-
#compdef temporal
10+
// taken from https://github.com/urfave/cli/blob/v2-maint/autocomplete/zsh_autocomplete
11+
var zsh_script = `#compdef temporal
1312
_cli_zsh_autocomplete() {
1413
local -a opts
1514
local cur
1615
cur=${words[-1]}
1716
if [[ "$cur" == "-"* ]]; then
18-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
17+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
1918
else
20-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
19+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
2120
fi
2221
if [[ "${opts[1]}" != "" ]]; then
2322
_describe 'values' opts
2423
else
2524
_files
2625
fi
27-
return
2826
}
2927
compdef _cli_zsh_autocomplete temporal
3028
`
3129

32-
var bash_script = `
33-
#! /bin/bash
30+
// taken from https://github.com/urfave/cli/blob/v2-maint/autocomplete/bash_autocomplete
31+
var bash_script = `#! /bin/bash
32+
# Macs have bash3 for which the bash-completion package doesn't include
33+
# _init_completion. This is a minimal version of that function.
34+
_cli_init_completion() {
35+
COMPREPLY=()
36+
_get_comp_words_by_ref "$@" cur prev words cword
37+
}
3438
_cli_bash_autocomplete() {
3539
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
36-
local cur opts base
40+
local cur opts base words
3741
COMPREPLY=()
3842
cur="${COMP_WORDS[COMP_CWORD]}"
43+
if declare -F _init_completion >/dev/null 2>&1; then
44+
_init_completion -n "=:" || return
45+
else
46+
_cli_init_completion -n "=:" || return
47+
fi
48+
words=("${words[@]:0:$cword}")
3949
if [[ "$cur" == "-"* ]]; then
40-
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
50+
requestComp="${words[*]} ${cur} --generate-bash-completion"
4151
else
42-
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
52+
requestComp="${words[*]} --generate-bash-completion"
4353
fi
44-
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
54+
opts=$(eval "${requestComp}" 2>/dev/null)
55+
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
4556
return 0
4657
fi
4758
}
48-
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete temporal
59+
complete -o bashdefault -o default -F _cli_bash_autocomplete temporal
4960
`
5061

5162
func NewCompletionCommands() []*cli.Command {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/stretchr/testify v1.8.4
1616
github.com/temporalio/tctl-kit v0.0.0-20230328153839-577f95d16fa0
1717
github.com/temporalio/ui-server/v2 v2.16.2
18-
github.com/urfave/cli/v2 v2.23.6
18+
github.com/urfave/cli/v2 v2.25.7
1919
go.temporal.io/api v1.23.0
2020
go.temporal.io/sdk v1.23.1
2121
go.temporal.io/sdk/contrib/tools/workflowcheck v0.0.0-20230328164709-88a40de39c33

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,8 @@ github.com/uber/jaeger-client-go v2.22.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
10901090
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
10911091
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
10921092
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
1093-
github.com/urfave/cli/v2 v2.23.6 h1:iWmtKD+prGo1nKUtLO0Wg4z9esfBM4rAV4QRLQiEmJ4=
1094-
github.com/urfave/cli/v2 v2.23.6/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
1093+
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
1094+
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
10951095
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
10961096
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
10971097
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=

0 commit comments

Comments
 (0)