goaegis/cmd/goaegis/cmd_hook.go

55 lines
1.3 KiB
Go

package main
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
)
var cmdHook = &cli.Command{
Name: "hook",
Usage: "Prints the shell function used to execute " + appName,
Subcommands: []*cli.Command{
{
Name: "bash",
Action: func(_ *cli.Context) error {
_, err := os.Stdout.WriteString(fmt.Sprintf(`: ${PROG:=%s}
# 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`, appName))
return err
},
},
},
}