This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev/sg: command, flag, and argument autocompletions (#33817)
Adds a new dependency in `sg setup` that writes the appropriate completion script for your shell into `~/.sourcegraph` and adds a line to your shell config to use the completion script. Completions are generated by `sg` itself, so there is no need to be able to update this script (and if we do need to update it, we can consider a migration path then) `urfave/cli` completions work by providing command suggestions by default, and if you use `-` <tab><tab> you can get flag completions as well. Also adds: 1. Custom completions based on config for `sg start`, `sg run`, `sg test` 2. Custom completions based on valid values for`sg live`, `sg ci build`
- Loading branch information
Showing
15 changed files
with
221 additions
and
2 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,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// completeOptions provides autocompletions based on the options returned by generateOptions | ||
func completeOptions(generateOptions func() (options []string)) cli.BashCompleteFunc { | ||
return func(cmd *cli.Context) { | ||
for _, opt := range generateOptions() { | ||
fmt.Fprintf(cmd.App.Writer, "%s\n", opt) | ||
} | ||
// Also render default completions to support flags | ||
cli.DefaultCompleteWithFlags(cmd.Command)(cmd) | ||
} | ||
} |
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,23 @@ | ||
package usershell | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"path/filepath" | ||
) | ||
|
||
var ( | ||
//go:embed autocomplete/bash_autocomplete | ||
bashAutocompleteScript string | ||
//go:embed autocomplete/zsh_autocomplete | ||
zshAutocompleteScript string | ||
) | ||
|
||
var AutocompleteScripts = map[Shell]string{ | ||
BashShell: bashAutocompleteScript, | ||
ZshShell: zshAutocompleteScript, | ||
} | ||
|
||
func AutocompleteScriptPath(sgHome string, shell Shell) string { | ||
return filepath.Join(sgHome, fmt.Sprintf("sg.%s_autocomplete", shell)) | ||
} |
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,3 @@ | ||
# autocomplete scripts | ||
|
||
Autocomplete scripts are sourced from [`urfave/cli/autocomplete`](https://github.com/urfave/cli/tree/master/autocomplete). |
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,21 @@ | ||
#! /bin/bash | ||
|
||
: ${PROG:=$(basename ${BASH_SOURCE})} | ||
|
||
_cli_bash_autocomplete() { | ||
if [[ "${COMP_WORDS[0]}" != "source" ]]; then | ||
local cur opts base | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=$(${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion) | ||
else | ||
opts=$(${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion) | ||
fi | ||
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) | ||
return 0 | ||
fi | ||
} | ||
|
||
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG | ||
unset PROG |
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,25 @@ | ||
#compdef $PROG | ||
|
||
_CLI_ZSH_AUTOCOMPLETE_HACK=1 | ||
|
||
_cli_zsh_autocomplete() { | ||
|
||
local -a opts | ||
local cur | ||
cur=${words[-1]} | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") | ||
else | ||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") | ||
fi | ||
|
||
if [[ "${opts[1]}" != "" ]]; then | ||
_describe 'values' opts | ||
else | ||
_files | ||
fi | ||
|
||
return | ||
} | ||
|
||
compdef _cli_zsh_autocomplete $PROG |
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
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
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
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
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