Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 9d77d36

Browse files
bobheadxijhchabran
andauthored
sg: sync completion scripts from upstream (#58698)
* dev/sg/autocomplete: sync completion scripts from upstream * Correctly close sg completion script --------- Co-authored-by: Jean-Hadrien Chabran <[email protected]>
1 parent 63e9efd commit 9d77d36

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

dev/sg/dependencies/shared.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,15 @@ func categoryAdditionalSGConfiguration() category {
239239
}
240240
shell := usershell.ShellType(ctx)
241241
autocompletePath := usershell.AutocompleteScriptPath(sgHome, shell)
242-
if _, err := os.Stat(autocompletePath); err != nil {
242+
completionScript, err := os.ReadFile(autocompletePath)
243+
if err != nil {
243244
return errors.Wrapf(err, "autocomplete script for shell %s not found", shell)
244245
}
245246

247+
if string(completionScript) != usershell.AutocompleteScripts[shell] {
248+
return errors.Wrapf(err, "autocomplete script for shell %s is not up to date", shell)
249+
}
250+
246251
shellConfig := usershell.ShellConfigPath(ctx)
247252
conf, err := os.ReadFile(shellConfig)
248253
if err != nil {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# autocomplete scripts
22

3-
Autocomplete scripts are sourced from [`urfave/cli/autocomplete`](https://github.com/urfave/cli/tree/master/autocomplete).
3+
Autocomplete scripts are sourced from [`urfave/cli/autocomplete`](https://github.com/urfave/cli/tree/main/autocomplete).

dev/sg/internal/usershell/autocomplete/bash_autocomplete

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22

33
: ${PROG:=$(basename ${BASH_SOURCE})}
44

5+
# Macs have bash3 for which the bash-completion package doesn't include
6+
# _init_completion. This is a minimal version of that function.
7+
_cli_init_completion() {
8+
COMPREPLY=()
9+
_get_comp_words_by_ref "$@" cur prev words cword
10+
}
11+
512
_cli_bash_autocomplete() {
613
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
7-
local cur opts base
14+
local cur opts base words
815
COMPREPLY=()
916
cur="${COMP_WORDS[COMP_CWORD]}"
17+
if declare -F _init_completion >/dev/null 2>&1; then
18+
_init_completion -n "=:" || return
19+
else
20+
_cli_init_completion -n "=:" || return
21+
fi
22+
words=("${words[@]:0:$cword}")
1023
if [[ "$cur" == "-"* ]]; then
11-
opts=$(${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion)
24+
requestComp="${words[*]} ${cur} --generate-bash-completion"
1225
else
13-
opts=$(${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion)
26+
requestComp="${words[*]} --generate-bash-completion"
1427
fi
28+
opts=$(eval "${requestComp}" 2>/dev/null)
1529
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
1630
return 0
1731
fi
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
#compdef $PROG
22

3-
_CLI_ZSH_AUTOCOMPLETE_HACK=1
4-
53
_cli_zsh_autocomplete() {
6-
74
local -a opts
85
local cur
96
cur=${words[-1]}
107
if [[ "$cur" == "-"* ]]; then
11-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
8+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
129
else
13-
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
10+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
1411
fi
1512

1613
if [[ "${opts[1]}" != "" ]]; then
1714
_describe 'values' opts
1815
else
1916
_files
2017
fi
21-
22-
return
2318
}
2419

2520
compdef _cli_zsh_autocomplete $PROG

0 commit comments

Comments
 (0)