Uplift cobra to v1.0.0

This commit uplifts cobra to the latest version. It also includes
various changes accounting for differences in the new version.

Change-Id: I0d1ed3ecd3b8d406428fded92abf6e782951d005
This commit is contained in:
Ian Howell 2020-08-13 11:32:03 -05:00
parent f61953bcf5
commit a8bdae6c9a
5 changed files with 136 additions and 5 deletions

View File

@ -36,6 +36,67 @@ __completion_contains_word()
return 1
}
__completion_handle_go_custom_completion()
{
__completion_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"
local out requestComp lastParam lastChar comp directive args
# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly completion allows to handle aliases
args=("${words[@]:1}")
requestComp="${words[0]} __completeNoDesc ${args[*]}"
lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1}
__completion_debug "${FUNCNAME[0]}: lastParam ${lastParam}, lastChar ${lastChar}"
if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__completion_debug "${FUNCNAME[0]}: Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi
__completion_debug "${FUNCNAME[0]}: calling ${requestComp}"
# Use eval to handle any environment variables and such
out=$(eval "${requestComp}" 2>/dev/null)
# Extract the directive integer at the very end of the output following a colon (:)
directive=${out##*:}
# Remove the directive
out=${out%:*}
if [ "${directive}" = "${out}" ]; then
# There is not directive specified
directive=0
fi
__completion_debug "${FUNCNAME[0]}: the completion directive is: ${directive}"
__completion_debug "${FUNCNAME[0]}: the completions are: ${out[*]}"
if [ $((directive & 1)) -ne 0 ]; then
# Error code. No completion.
__completion_debug "${FUNCNAME[0]}: received error from custom completion go code"
return
else
if [ $((directive & 2)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__completion_debug "${FUNCNAME[0]}: activating no space"
compopt -o nospace
fi
fi
if [ $((directive & 4)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__completion_debug "${FUNCNAME[0]}: activating no file completion"
compopt +o default
fi
fi
while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${out[*]}" -- "$cur")
fi
}
__completion_handle_reply()
{
__completion_debug "${FUNCNAME[0]}"
@ -99,6 +160,10 @@ __completion_handle_reply()
completions=("${commands[@]}")
if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
completions=("${must_have_one_noun[@]}")
elif [[ -n "${has_completion_function}" ]]; then
# if a go completion function is provided, defer to that function
completions=()
__completion_handle_go_custom_completion
fi
if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
completions+=("${must_have_one_flag[@]}")
@ -302,6 +367,7 @@ __start_completion()
local commands=("completion")
local must_have_one_flag=()
local must_have_one_noun=()
local has_completion_function
local last_command
local nouns=()

View File

@ -170,6 +170,67 @@ __completion_contains_word()
return 1
}
__completion_handle_go_custom_completion()
{
__completion_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"
local out requestComp lastParam lastChar comp directive args
# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly completion allows to handle aliases
args=("${words[@]:1}")
requestComp="${words[0]} __completeNoDesc ${args[*]}"
lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1}
__completion_debug "${FUNCNAME[0]}: lastParam ${lastParam}, lastChar ${lastChar}"
if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__completion_debug "${FUNCNAME[0]}: Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi
__completion_debug "${FUNCNAME[0]}: calling ${requestComp}"
# Use eval to handle any environment variables and such
out=$(eval "${requestComp}" 2>/dev/null)
# Extract the directive integer at the very end of the output following a colon (:)
directive=${out##*:}
# Remove the directive
out=${out%:*}
if [ "${directive}" = "${out}" ]; then
# There is not directive specified
directive=0
fi
__completion_debug "${FUNCNAME[0]}: the completion directive is: ${directive}"
__completion_debug "${FUNCNAME[0]}: the completions are: ${out[*]}"
if [ $((directive & 1)) -ne 0 ]; then
# Error code. No completion.
__completion_debug "${FUNCNAME[0]}: received error from custom completion go code"
return
else
if [ $((directive & 2)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__completion_debug "${FUNCNAME[0]}: activating no space"
compopt -o nospace
fi
fi
if [ $((directive & 4)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
__completion_debug "${FUNCNAME[0]}: activating no file completion"
compopt +o default
fi
fi
while IFS='' read -r comp; do
COMPREPLY+=("$comp")
done < <(compgen -W "${out[*]}" -- "$cur")
fi
}
__completion_handle_reply()
{
__completion_debug "${FUNCNAME[0]}"
@ -233,6 +294,10 @@ __completion_handle_reply()
completions=("${commands[@]}")
if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
completions=("${must_have_one_noun[@]}")
elif [[ -n "${has_completion_function}" ]]; then
# if a go completion function is provided, defer to that function
completions=()
__completion_handle_go_custom_completion
fi
if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
completions+=("${must_have_one_flag[@]}")
@ -436,6 +501,7 @@ __start_completion()
local commands=("completion")
local must_have_one_flag=()
local must_have_one_noun=()
local has_completion_function
local last_command
local nouns=()

View File

@ -57,19 +57,16 @@ func TestFlagLoading(t *testing.T) {
name string
args []string
expected string
Error error
}{
{
name: "default, no flags",
args: []string{},
expected: "",
Error: cobra.ErrSubCommandRequired,
},
{
name: "alternate airshipconfig",
args: []string{"--airshipconf", "/custom/path/to/airshipconfig"},
expected: "/custom/path/to/airshipconfig",
Error: cobra.ErrSubCommandRequired,
},
}
@ -83,7 +80,7 @@ func TestFlagLoading(t *testing.T) {
rootCmd.SetArgs(tt.args)
err = rootCmd.Execute()
assert.Equal(t, tt.Error, err)
require.NoError(t, err)
assert.Equal(t, settings.AirshipConfigPath, tt.expected)
})

2
go.mod
View File

@ -18,7 +18,7 @@ require (
github.com/metal3-io/baremetal-operator v0.0.0-20200501205115-2c0dc9997bfa
github.com/onsi/gomega v1.9.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v0.0.6
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.4.0
k8s.io/api v0.17.4
k8s.io/apiextensions-apiserver v0.17.4

2
go.sum
View File

@ -948,6 +948,8 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v0.0.6 h1:breEStsVwemnKh2/s6gMvSdMEkwW0sK8vGStnlVBMCs=
github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=