Skip to content

Instantly share code, notes, and snippets.

@danielesegato
Forked from davidgf/serverless
Last active July 25, 2016 19:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielesegato/d4088ae0a1e67ba827cb0d8846038971 to your computer and use it in GitHub Desktop.
Save danielesegato/d4088ae0a1e67ba827cb0d8846038971 to your computer and use it in GitHub Desktop.
Bash completion for Serverless

To enable serverless bash completion:

  • Install serverless: npm -g serverless
  • Append the servereless.bash code to the .bashrc or .bash_profile file in your home directory

This gitst is a fork of https://gist.github.com/davidgf/5bd76141aa70f9ed4d8f716851e68830. It avoid using find .to look for lambda functions: the command, if executed in the root directory or a big directory, can be really bad for performances. It also add subcommand handling for non-functions.

The script instead look for the serverless project file s-project.json starting from the current directory and looking up, then if found look for lambda functions in that directory.

Keep an eye on serverless/serverless#187 to see if this feature is officially implemented by serverless.

###-begin-serverless-completion-###
_sls_upsearch () {
local slashes=${PWD//[^\/]/}
local directory="$PWD"
local n=0
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/$1" && echo `readlink -m "$directory/$1"` && return 0
directory="$directory/.."
done
return 1
}
_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"
"help"
)
local command
if [[ ${COMP_CWORD} -gt 1 && ${COMMANDS[@]} =~ ${COMP_WORDS[1]} ]]; then
command=${COMP_WORDS[1]}
fi
if [ "$command" = "" ]; then
# no command yet, show what commands we have
COMPREPLY=( $( compgen -W '${COMMANDS[@]}' ${cur} ) )
return 0
else
case $command in
project)
local SUBCOMMANDS=(
"create"
"init"
"install"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
function)
local SUBCOMMANDS=(
"run"
"deploy"
"create"
"logs"
"remove"
"rollback"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
elif [[ ${COMP_CWORD} -eq 3 ]]; then
local fnames='';
local projectFile=`_sls_upsearch 's-project.json'`;
if [ -f "$projectFile" ] ; then
local projectDir=`dirname "$projectFile"`;
fnames=`find "$projectDir" -type f -name "s-function.json" |sed "s#\(.*\)/.*#\1#" | rev |cut -d"/" -f1 | rev`
fi
COMPREPLY=( $( compgen -W '${fnames}' ${cur} ) )
return 0
fi
;;
endpoint)
local SUBCOMMANDS=(
"deploy"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
event)
local SUBCOMMANDS=(
"deploy"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
dash)
local SUBCOMMANDS=(
"deploy"
"summary"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
stage)
local SUBCOMMANDS=(
"create"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
region)
local SUBCOMMANDS=(
"create"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
resources)
local SUBCOMMANDS=(
"deploy"
"diff"
"remove"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
plugin)
local SUBCOMMANDS=(
"create"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
variables)
local SUBCOMMANDS=(
"list"
"set"
"unset"
)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $( compgen -W '${SUBCOMMANDS[@]}' ${cur} ) )
return 0
fi
;;
esac
fi
return 0
} &&
complete -F _sls serverless
complete -F _sls sls
complete -F _sls slss
###-end-serverless-completion-###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment