From eb313a8608a0c86e7cd62e60dc15237d4ebc81b4 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 29 Mar 2020 23:08:05 +0200 Subject: [PATCH 1/3] bash: support spaces in values - broke sth in inject command --- bash/action.go | 8 ++++---- bash/snippet.go | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bash/action.go b/bash/action.go index c2ff6dbbe..efdd35065 100644 --- a/bash/action.go +++ b/bash/action.go @@ -33,7 +33,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 { @@ -57,9 +57,9 @@ func ActionValues(values ...string) string { for index, val := range Sanitize(values...) { // 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 { @@ -74,7 +74,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 { diff --git a/bash/snippet.go b/bash/snippet.go index 174c3a2d1..e18568c51 100644 --- a/bash/snippet.go +++ b/bash/snippet.go @@ -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 @@ -119,6 +121,7 @@ func snippetFlagCompletion(flag *pflag.Flag, action string) (snippet string) { } return fmt.Sprintf(` %v) + local IFS=$'\n' COMPREPLY=($(%v)) ;; `, names, action) From 06225350a4689bedc3a1c31b813f5314d3776453 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 29 Mar 2020 23:37:12 +0200 Subject: [PATCH 2/3] bash: fixed empty values warning - fixed tests --- bash/action.go | 20 +++++++++++++----- example/cmd/root_test.go | 45 ++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/bash/action.go b/bash/action.go index efdd35065..15d6c388f 100644 --- a/bash/action.go +++ b/bash/action.go @@ -6,10 +6,19 @@ import ( ) var sanitizer = strings.NewReplacer( - `$`, ``, + `$`, ``, "`", ``, `\`, ``, - `"`, `'`, + `"`, ``, + `'`, ``, + `|`, ``, + `>`, ``, + `<`, ``, + `&`, ``, + `(`, ``, + `)`, ``, + `;`, ``, + `#`, ``, ) func Sanitize(values ...string) []string { @@ -49,12 +58,13 @@ 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] = strings.Replace(val, ` `, `\\\ `, -1) diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index 841126b9e..b2923043a 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 From 95bc08e180dc6aa2d626b1618ff6c9bcf6e7557a Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 29 Mar 2020 23:38:55 +0200 Subject: [PATCH 3/3] go fmt --- bash/action.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bash/action.go b/bash/action.go index 15d6c388f..06796d76b 100644 --- a/bash/action.go +++ b/bash/action.go @@ -6,7 +6,7 @@ import ( ) var sanitizer = strings.NewReplacer( - `$`, ``, + `$`, ``, "`", ``, `\`, ``, `"`, ``, @@ -58,7 +58,7 @@ func ActionHosts() string { } func ActionValues(values ...string) string { - sanitized := Sanitize(values...) + sanitized := Sanitize(values...) if len(strings.TrimSpace(strings.Join(sanitized, ""))) == 0 { return ActionMessage("no values to complete") }