Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add bash completion #721

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,52 @@ func SetupZSHAutoCompleteFolderGranted() (string, error) {
return zshPath, nil
}

// checks and or creates the config folder on startup
func SetupBashAutoCompleteFolderAssume() (string, error) {
grantedFolder, err := GrantedConfigFolder()
if err != nil {
return "", err
}
bashPath := path.Join(grantedFolder, "bash_autocomplete")
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err := os.Mkdir(bashPath, 0700)
if err != nil {
return "", err
}
}
bashPath = path.Join(bashPath, build.AssumeScriptName())
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err := os.Mkdir(bashPath, 0700)
if err != nil {
return "", err
}
}
return bashPath, nil
}

// checks and or creates the config folder on startup
func SetupBashAutoCompleteFolderGranted() (string, error) {
grantedFolder, err := GrantedConfigFolder()
if err != nil {
return "", err
}
bashPath := path.Join(grantedFolder, "bash_autocomplete")
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err := os.Mkdir(bashPath, 0700)
if err != nil {
return "", err
}
}
bashPath = path.Join(bashPath, build.GrantedBinaryName())
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err := os.Mkdir(bashPath, 0700)
if err != nil {
return "", err
}
}
return bashPath, nil
}

func GrantedConfigFolder() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
Expand Down
57 changes: 56 additions & 1 deletion pkg/granted/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,61 @@ func installZSHCompletions(c *cli.Context) error {
}

func installBashCompletions(c *cli.Context) error {
clio.Info("We don't have completion support for bash yet, check out our docs to find out how to let us know you want this feature https://granted.dev/autocompletion")
file, err := shells.GetBashConfigFile()
if err != nil {
return err
}

tmpl, err := template.ParseFS(templateFiles, "templates/*")
if err != nil {
return err
}

assumeData := AutoCompleteTemplateData{
Program: build.AssumeScriptName(),
}
assume := new(bytes.Buffer)
err = tmpl.ExecuteTemplate(assume, "bash_autocomplete_assume.tmpl", assumeData)
if err != nil {
return err
}
grantedData := AutoCompleteTemplateData{
Program: build.GrantedBinaryName(),
}
granted := new(bytes.Buffer)
err = tmpl.ExecuteTemplate(granted, "bash_autocomplete_granted.tmpl", grantedData)
if err != nil {
return err
}

bashPathAssume, err := config.SetupBashAutoCompleteFolderAssume()
if err != nil {
return err
}

err = os.WriteFile(path.Join(bashPathAssume, "_"+assumeData.Program), assume.Bytes(), 0666)
if err != nil {
return err
}
bashPathGranted, err := config.SetupBashAutoCompleteFolderGranted()
if err != nil {
return err
}
err = os.WriteFile(path.Join(bashPathGranted, "_"+grantedData.Program), granted.Bytes(), 0666)
if err != nil {
return err
}
err = shells.AppendLine(file, fmt.Sprintf("fpath=(%s/ $fpath)", bashPathAssume))
var lae *shells.ErrLineAlreadyExists
if is := errors.As(err, &lae); err != nil && !is {
return err
}
err = shells.AppendLine(file, fmt.Sprintf("fpath=(%s/ $fpath)", bashPathGranted))
lae = nil
if is := errors.As(err, &lae); err != nil && !is {
return err
}
clio.Success("Bash autocompletions generated successfully")
clio.Warn("A shell restart is required to apply changes, please open a new terminal to test that autocomplete is working")
return nil
}
35 changes: 35 additions & 0 deletions pkg/granted/templates/bash_autocomplete_assume.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /bin/bash
# adapted from https://github.com/urfave/cli/blob/main/autocomplete/bash_autocomplete
: ${PROG:=$(basename ${BASH_SOURCE})}

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="FORCE_NO_ALIAS=true ${words[*]} ${cur} --generate-bash-completion"
else
requestComp="FORCE_NO_ALIAS=true ${words[*]} --generate-bash-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
35 changes: 35 additions & 0 deletions pkg/granted/templates/bash_autocomplete_granted.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /bin/bash
# adapted from https://github.com/urfave/cli/blob/main/autocomplete/bash_autocomplete
: ${PROG:=$(basename ${BASH_SOURCE})}

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
else
requestComp="${words[*]} --generate-bash-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
Loading