Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
In-snap bash tab completion #3150
85fd572
cd2573a
fe815e7
c7bcad0
46ea55c
b02ba90
6dcbcde
8a60343
a82ba0d
90a9b40
35d048a
7fc7cd7
41703e8
e75170d
address review feedback
- Loading branch information...
| @@ -89,14 +89,16 @@ func run() error { | ||
| return snapExecApp(snapApp, revision, opts.Command, extraArgs) | ||
| } | ||
| +const defaultShell = "/bin/bash" | ||
| + | ||
| func findCommand(app *snap.AppInfo, command string) (string, error) { | ||
| var cmd string | ||
| switch command { | ||
| case "shell": | ||
| - cmd = "/bin/bash" | ||
| + cmd = defaultShell | ||
| case "complete": | ||
| if app.Completer != "" { | ||
| - cmd = "/bin/bash" | ||
| + cmd = defaultShell | ||
| } | ||
|
|
||
| case "stop": | ||
| cmd = app.StopCommand | ||
| @@ -153,10 +155,10 @@ func snapExecApp(snapApp, revision, command string, args []string) error { | ||
| fullCmd := filepath.Join(app.Snap.MountDir(), cmd) | ||
| switch command { | ||
| case "shell": | ||
| - fullCmd = "/bin/bash" | ||
| + fullCmd = defaultShell | ||
| cmdArgs = nil | ||
| case "complete": | ||
| - fullCmd = "/bin/bash" | ||
| + fullCmd = defaultShell | ||
| cmdArgs = []string{ | ||
| dirs.CompletionHelper, | ||
| filepath.Join(app.Snap.MountDir(), app.Completer), | ||
zyga
Contributor
|
||
| @@ -6,7 +6,32 @@ | ||
| _complete_from_snap() { | ||
zyga
Contributor
|
||
| { | ||
jdstrand
Contributor
|
||
| read -a opts | ||
| + # opts is expected to be a series of compopt options | ||
| + if [[ ${#opts[@]} -gt 0 ]]; then | ||
| + if [[ "${opts[0]}" == "cannot" ]]; then | ||
| + # older snap-execs sent errors over stdout :-( | ||
| + return 1 | ||
| + fi | ||
| + | ||
| + for i in "${opts[@]}"; do | ||
| + if ! [[ "$i" =~ ^[a-z]+$ ]]; then | ||
| + # non-alphanumeric option; something awry | ||
jdstrand
Contributor
|
||
| + return 2 | ||
| + fi | ||
| + done | ||
| + fi | ||
| + | ||
| read bounced | ||
| + case "$bounced" in | ||
| + ""|"alias"|"export"|"job"|"variable") | ||
jdstrand
Contributor
|
||
| + # OK | ||
| + ;; | ||
| + *) | ||
| + # unrecognised bounce | ||
| + return 2 | ||
| + ;; | ||
| + esac | ||
| + | ||
| read sep | ||
| if [ "$sep" ]; then | ||
zyga
Contributor
|
||
| # non-blank separator? madness! | ||
| @@ -21,10 +46,6 @@ _complete_from_snap() { | ||
| fi | ||
| if [[ ${#opts[@]} -gt 0 ]]; then | ||
| - if [[ "${opts[0]}" == "cannot" ]]; then | ||
| - # older snap-execs sent errors over stdout :-( | ||
| - return 1 | ||
| - fi | ||
| compopt $(printf " -o %s" "${opts[@]}") | ||
jdstrand
Contributor
|
||
| fi | ||
| if [ "$bounced" ]; then | ||
| @@ -48,7 +48,7 @@ fi | ||
jdstrand
Contributor
|
||
| . /usr/share/bash-completion/bash_completion | ||
|
|
||
| -. $_compscript | ||
| +. "$_compscript" | ||
| # _compopts is an associative array, which keys are options. | ||
jdstrand
Contributor
|
||
| declare -A _compopts | ||
| @@ -201,8 +201,7 @@ var defaultTemplate = ` | ||
| /usr/lib/snapd/snap-exec m, | ||
| # For in-snap tab completion | ||
| - /etc/bash_completion.d/ r, | ||
| - /etc/bash_completion.d/* r, | ||
| + /etc/bash_completion.d/{,*} r, | ||
| /usr/lib/snapd/etelpmoc.sh ixr, # marshaller (see complete.sh for out-of-snap unmarshal) | ||
| /usr/share/bash-completion/bash_completion r, # user-provided completions (run in-snap) may use functions from here | ||
jdstrand
Contributor
|
||
Small nit-pick. It seems like perhaps setting
defaultShell := /bin/bashup above and then setting "shell" and the fallback for "complete" to this might be better.