-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command to generate completion scripts
Rather than directing people to download a file, which could end up out of sync if the upstream version changes, instead bundle the scripts into the binary and add a command to output them. `//go:embed` requires the files to be in a descendant directory of the file containing the directive, so move them into a subdirectory, and symlink them back to the original locations so that people can still use the legacy method
- Loading branch information
Showing
10 changed files
with
211 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
_aws-vault_bash_autocomplete() { | ||
local i cur prev opts base | ||
|
||
for (( i=1; i < COMP_CWORD; i++ )); do | ||
if [[ ${COMP_WORDS[i]} == -- ]]; then | ||
_command_offset $i+1 | ||
return | ||
fi | ||
done | ||
|
||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
opts=$( ${COMP_WORDS[0]} --completion-bash "${COMP_WORDS[@]:1:$COMP_CWORD}" ) | ||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||
return 0 | ||
} | ||
complete -F _aws-vault_bash_autocomplete -o default aws-vault |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
if status --is-interactive | ||
complete -ec aws-vault | ||
|
||
# switch based on seeing a `--` | ||
complete -c aws-vault -n 'not __fish_aws_vault_is_commandline' -xa '(__fish_aws_vault_complete_arg)' | ||
complete -c aws-vault -n '__fish_aws_vault_is_commandline' -xa '(__fish_aws_vault_complete_commandline)' | ||
|
||
function __fish_aws_vault_is_commandline | ||
string match -q -r '^--$' -- (commandline -opc) | ||
end | ||
|
||
function __fish_aws_vault_complete_arg | ||
set -l parts (commandline -opc) | ||
set -e parts[1] | ||
|
||
aws-vault --completion-bash $parts | ||
end | ||
|
||
function __fish_aws_vault_complete_commandline | ||
set -l parts (string split --max 1 '--' -- (commandline -pc)) | ||
|
||
complete "-C$parts[2]" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#compdef aws-vault | ||
|
||
_aws-vault() { | ||
local i | ||
for (( i=2; i < CURRENT; i++ )); do | ||
if [[ ${words[i]} == -- ]]; then | ||
shift $i words | ||
(( CURRENT -= i )) | ||
_normal | ||
return | ||
fi | ||
done | ||
|
||
local matches=($(${words[1]} --completion-bash ${(@)words[2,$CURRENT]})) | ||
compadd -a matches | ||
|
||
if [[ $compstate[nmatches] -eq 0 && $words[$CURRENT] != -* ]]; then | ||
_files | ||
fi | ||
} | ||
|
||
if [[ "$(basename -- ${(%):-%x})" != "_aws-vault" ]]; then | ||
compdef _aws-vault aws-vault | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cli | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
) | ||
|
||
//go:embed completion-scripts/aws-vault.bash | ||
var bashCompletionScript string | ||
|
||
//go:embed completion-scripts/aws-vault.zsh | ||
var zshCompletionScript string | ||
|
||
//go:embed completion-scripts/aws-vault.fish | ||
var fishCompletionScript string | ||
|
||
type CompletionCommandInput struct { | ||
Shell string | ||
} | ||
|
||
var completionWriter io.Writer = os.Stdout | ||
|
||
func ConfigureCompletionCommand(app *kingpin.Application) { | ||
input := CompletionCommandInput{} | ||
|
||
cmd := app.Command("completion", "Output shell completion code for bash / zsh / fish.") | ||
|
||
cmd.Arg("shell", "Shell type: bash | zsh | fish"). | ||
Required(). | ||
Envar("SHELL"). | ||
HintOptions("bash", "zsh", "fish"). | ||
StringVar(&input.Shell) | ||
|
||
cmd.Action(func(c *kingpin.ParseContext) error { | ||
shell := path.Base(input.Shell) // strip any path (useful for $SHELL, doesn't hurt for other cases) | ||
|
||
switch shell { | ||
case "bash": | ||
fmt.Fprint(completionWriter, bashCompletionScript) | ||
case "zsh": | ||
fmt.Fprint(completionWriter, zshCompletionScript) | ||
case "fish": | ||
fmt.Fprint(completionWriter, fishCompletionScript) | ||
default: | ||
return fmt.Errorf("unknown shell: %s", input.Shell) | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
) | ||
|
||
func TestConfigureCompletionCommand(t *testing.T) { | ||
app := kingpin.New("test", "") | ||
ConfigureCompletionCommand(app) | ||
|
||
tests := []struct { | ||
shell string | ||
want string | ||
}{ | ||
{"bash", bashCompletionScript}, | ||
{"zsh", zshCompletionScript}, | ||
{"fish", fishCompletionScript}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.shell, func(t *testing.T) { | ||
var buf bytes.Buffer | ||
completionWriter = &buf | ||
_, err := app.Parse([]string{"completion", tt.shell}) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
got := buf.String() | ||
if got != tt.want { | ||
t.Errorf("Parse([]string{\"completion\", %q}) = %q, want %q", tt.shell, got, tt.want) | ||
} | ||
}) | ||
} | ||
|
||
// Test $SHELL envar | ||
|
||
envarTests := []struct { | ||
value string | ||
want string | ||
}{ | ||
{"/bin/bash", bashCompletionScript}, | ||
{"/bin/zsh", zshCompletionScript}, | ||
{"/bin/fish", fishCompletionScript}, | ||
} | ||
|
||
for _, tt := range envarTests { | ||
t.Run(fmt.Sprintf("$SHELL=%s", tt.value), func(t *testing.T) { | ||
var buf bytes.Buffer | ||
completionWriter = &buf | ||
os.Setenv("SHELL", tt.value) | ||
_, err := app.Parse([]string{"completion"}) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
got := buf.String() | ||
if got != tt.want { | ||
t.Errorf("SHELL=%q Parse([]string{\"completion\"}) = %q, want %q", tt.value, got, tt.want) | ||
} | ||
}) | ||
} | ||
|
||
// Test unknown shell | ||
t.Run("unknown shell", func(t *testing.T) { | ||
var buf bytes.Buffer | ||
app.UsageWriter(&buf) | ||
|
||
_, err := app.Parse([]string{"completion", "foo"}) | ||
|
||
if err == nil { | ||
t.Fatal("expected error, but got nil") | ||
} | ||
|
||
if err.Error() != "unknown shell: foo" { | ||
t.Errorf("ConfigureCompletionCommand(%q) = %q, want %q", "foo", err.Error(), "unknown shell: foo") | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../cli/completion-scripts/aws-vault.bash |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../cli/completion-scripts/aws-vault.fish |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../cli/completion-scripts/aws-vault.zsh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters