Skip to content

Commit

Permalink
Merge pull request #7 from rsteube/bash-support-spaces
Browse files Browse the repository at this point in the history
bash: support spaces in values
  • Loading branch information
rsteube authored Mar 29, 2020
2 parents 352a0bf + 95bc08e commit f555780
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
26 changes: 18 additions & 8 deletions bash/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ var sanitizer = strings.NewReplacer(
`$`, ``,
"`", ``,
`\`, ``,
`"`, `'`,
`"`, ``,
`'`, ``,
`|`, ``,
`>`, ``,
`<`, ``,
`&`, ``,
`(`, ``,
`)`, ``,
`;`, ``,
`#`, ``,
)

func Sanitize(values ...string) []string {
Expand All @@ -33,7 +42,7 @@ func ActionFiles(suffix string) string {
}

func ActionNetInterfaces() string {
return `compgen -W "$(ifconfig -a | grep -o '^[^ :]\+' | tr '\n' ' ')" -- $last`
return `compgen -W "$(ifconfig -a | grep -o '^[^ :]\+')" -- $last`
}

func ActionUsers() string {
Expand All @@ -49,17 +58,18 @@ func ActionHosts() string {
}

func ActionValues(values ...string) string {
if len(strings.TrimSpace(strings.Join(values, ""))) == 0 {
sanitized := Sanitize(values...)
if len(strings.TrimSpace(strings.Join(sanitized, ""))) == 0 {
return ActionMessage("no values to complete")
}

vals := make([]string, len(values))
for index, val := range Sanitize(values...) {
vals := make([]string, len(sanitized))
for index, val := range sanitized {
// TODO escape special characters
//vals[index] = strings.Replace(val, " ", `\ `, -1)
vals[index] = val
vals[index] = strings.Replace(val, ` `, `\\\ `, -1)
}
return fmt.Sprintf(`compgen -W "%v" -- $last`, strings.Join(vals, ` `))
return fmt.Sprintf(`compgen -W $'%v' -- $last`, strings.Join(vals, `\n`))
}

func ActionValuesDescribed(values ...string) string {
Expand All @@ -74,7 +84,7 @@ func ActionValuesDescribed(values ...string) string {
}

func ActionMessage(msg string) string {
return ActionValues("ERR", strings.Replace(Sanitize(msg)[0], " ", "_", -1)) // TODO escape characters
return ActionValues("ERR", Sanitize(msg)[0])
}

func ActionMultiParts(separator rune, values ...string) string {
Expand Down
3 changes: 3 additions & 0 deletions bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func snippetFunctions(cmd *cobra.Command, actions map[string]string) string {
function_pattern := `
'%v' )
if [[ $last == -* ]]; then
local IFS=$'\n'
COMPREPLY=($(%v))
else
case $previous in
%v
*)
local IFS=$'\n'
COMPREPLY=($(%v))
;;
esac
Expand Down Expand Up @@ -119,6 +121,7 @@ func snippetFlagCompletion(flag *pflag.Flag, action string) (snippet string) {
}

return fmt.Sprintf(` %v)
local IFS=$'\n'
COMPREPLY=($(%v))
;;
`, names, action)
Expand Down
45 changes: 34 additions & 11 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ _example_completions() {
'_example' )
if [[ $last == -* ]]; then
COMPREPLY=($(compgen -W "--array -a --persistentFlag -p --toggle -t" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'--array\n-a\n--persistentFlag\n-p\n--toggle\n-t' -- $last))
else
case $previous in
-a | --array)
local IFS=$'\n'
COMPREPLY=($())
;;
*)
COMPREPLY=($(compgen -W "action alias callback condition injection" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'action\nalias\ncallback\ncondition\ninjection' -- $last))
;;
esac
fi
Expand All @@ -43,50 +46,62 @@ _example_completions() {
'_example__action' )
if [[ $last == -* ]]; then
COMPREPLY=($(compgen -W "--custom -c --files -f --groups -g --hosts --message -m --multi_parts --net_interfaces -n --users -u --values -v --values_described -d" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'--custom\n-c\n--files\n-f\n--groups\n-g\n--hosts\n--message\n-m\n--multi_parts\n--net_interfaces\n-n\n--users\n-u\n--values\n-v\n--values_described\n-d' -- $last))
else
case $previous in
-c | --custom)
local IFS=$'\n'
COMPREPLY=($())
;;
-f | --files)
local IFS=$'\n'
COMPREPLY=($(compgen -f -o plusdirs -X "!*.go" -- $last))
;;
-g | --groups)
local IFS=$'\n'
COMPREPLY=($(compgen -g -- $last))
;;
--hosts)
local IFS=$'\n'
COMPREPLY=($(compgen -W "$(cat ~/.ssh/known_hosts | cut -d ' ' -f1 | cut -d ',' -f1)" -- $last))
;;
-m | --message)
COMPREPLY=($(compgen -W "ERR message_example" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'ERR\nmessage\\\ example' -- $last))
;;
--multi_parts)
COMPREPLY=($(compgen -W "multi/parts multi/parts/example multi/parts/test example/parts" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'multi/parts\nmulti/parts/example\nmulti/parts/test\nexample/parts' -- $last))
;;
-n | --net_interfaces)
COMPREPLY=($(compgen -W "$(ifconfig -a | grep -o '^[^ :]\+' | tr '\n' ' ')" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W "$(ifconfig -a | grep -o '^[^ :]\+')" -- $last))
;;
-u | --users)
local IFS=$'\n'
COMPREPLY=($(compgen -u -- $last))
;;
-v | --values)
COMPREPLY=($(compgen -W "values example" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'values\nexample' -- $last))
;;
-d | --values_described)
COMPREPLY=($(compgen -W "values example " -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'values\nexample\n\n' -- $last))
;;
*)
local IFS=$'\n'
COMPREPLY=($(eval $(_example_callback '_')))
;;
esac
Expand All @@ -96,14 +111,17 @@ _example_completions() {
'_example__callback' )
if [[ $last == -* ]]; then
COMPREPLY=($(compgen -W "--callback -c" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'--callback\n-c' -- $last))
else
case $previous in
-c | --callback)
local IFS=$'\n'
COMPREPLY=($(eval $(_example_callback '_example__callback##callback')))
;;
*)
local IFS=$'\n'
COMPREPLY=($(eval $(_example_callback '_')))
;;
esac
Expand All @@ -113,14 +131,17 @@ _example_completions() {
'_example__condition' )
if [[ $last == -* ]]; then
COMPREPLY=($(compgen -W "--required -r" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'--required\n-r' -- $last))
else
case $previous in
-r | --required)
COMPREPLY=($(compgen -W "valid invalid" -- $last))
local IFS=$'\n'
COMPREPLY=($(compgen -W $'valid\ninvalid' -- $last))
;;
*)
local IFS=$'\n'
COMPREPLY=($(eval $(_example_callback '_')))
;;
esac
Expand All @@ -130,11 +151,13 @@ _example_completions() {
'_example__injection' )
if [[ $last == -* ]]; then
local IFS=$'\n'
COMPREPLY=($())
else
case $previous in
*)
local IFS=$'\n'
COMPREPLY=($(eval $(_example_callback '_')))
;;
esac
Expand Down

0 comments on commit f555780

Please sign in to comment.