Skip to content

Instantly share code, notes, and snippets.

@narfman0
Forked from davidgf/serverless
Created June 28, 2018 13:41
Show Gist options
  • Select an option

  • Save narfman0/08046ee5cd6bc0f0791192f27e09dc2c to your computer and use it in GitHub Desktop.

Select an option

Save narfman0/08046ee5cd6bc0f0791192f27e09dc2c to your computer and use it in GitHub Desktop.
Bash completion for Serverless
_sls()
{
local cur prev words cword
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words="${COMP_WORDS}"
local COMMANDS=(
"project"
"function"
"endpoint"
"event"
"dash"
"stage"
"region"
"resources"
"plugin"
"variables"
)
local FCOMMANDS=(
"run"
"deploy"
"create"
"logs"
"remove"
"rollback"
)
local command
if [[ ${COMP_CWORD} -gt 1 && ${COMMANDS[@]} =~ ${COMP_WORDS[1]} ]]; then
command=${COMP_WORDS[1]}
fi
# command is function, showing subcommands
if [[ "$command" = "function" && ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${FCOMMANDS[@]}' ${cur} ) )
return 0
fi
# command is function and has subcommand, showing function names
if [[ "$command" = "function" && ${COMP_CWORD} -eq 3 ]]; then
local fnames=`find . -type f -name "s-function.json" |sed "s#\(.*\)/.*#\1#" | rev |cut -d"/" -f1 | rev`
COMPREPLY=( $( compgen -W '${fnames}' ${cur} ) )
return 0
fi
# no command yet, show what commands we have
if [ "$command" = "" ]; then
COMPREPLY=( $( compgen -W '${COMMANDS[@]}' ${cur} ) )
fi
return 0
} &&
complete -F _sls serverless
complete -F _sls sls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment