Created
May 22, 2020 15:28
-
-
Save rgatti/43f6d04c7c1c00463e65fac80640593c to your computer and use it in GitHub Desktop.
vela bash completion (wip)
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
#!/usr/bin/env bash | |
# Get org names from previously added repos | |
__get_org_names() { | |
vela get repo --output yaml | awk '/ org:/{ print $2 }' | sort -u | |
} | |
# Get repo names from previously added repos | |
__get_repo_names() { | |
vela get repo --output yaml | awk '/ name:/{ print $2 }' | sort -u | |
} | |
# Get build numbers for a repo | |
__get_build_numbers() { | |
local i j org repo | |
# pull in env vars | |
if ! [ -z "$BUILD_ORG" ]; then | |
org="$BUILD_ORG" | |
fi | |
if ! [ -z "$BUILD_REPO" ]; then | |
repo="$BUILD_REPO" | |
fi | |
# lookup if there's org/repo to fetch build numbers | |
for j in ${COMP_WORDS[@]}; do | |
case "$i" in | |
--org ) org=$j ;; | |
--repo ) repo=$j ;; | |
* ) ;; | |
esac | |
i=$j | |
done | |
# fetch build numbers | |
if ! [ -z "$org" ] && ! [ -z "$repo" ]; then | |
vela get build --org $org --repo $repo --output yaml \ | |
| awk '/number:/{ print $2 }' \ | |
| sort -u | |
fi | |
} | |
_vela() { | |
local cur prev split=false | |
COMPREPLY=() | |
_get_comp_words_by_ref -n : cur prev | |
# Top level commands | |
if [ $COMP_CWORD -eq 1 ]; then | |
COMPREPLY=( $(compgen -W 'help login restart validate repair chown | |
add update remove get view generate' -- "$cur") ) | |
return 0 | |
fi | |
local opts='' | |
# Second level commands | |
if [ $COMP_CWORD -eq 2 ]; then | |
case "$prev" in | |
repair ) opts='build' ;; | |
add ) opts='deployment repo secret help' ;; | |
update|remove ) opts='config repo secret help' ;; | |
get ) opts='build deployment repo secret service step help' ;; | |
view ) opts='build deployment config log repo secret service step help' ;; | |
generate ) opts='config pipe help' ;; | |
* ) ;; | |
esac | |
# Args for second level commands | |
# | |
# This is only handling the second level for commands I've been using frequently: | |
# secret, build, and log. | |
else | |
local i cmd sub | |
_split_longopt && split=true | |
# Lookup command and subcommand we're in | |
for i in ${COMP_WORDS[@]}; do | |
case "$i" in | |
repair|add|update|update|remove|get|view|generate ) cmd=$i ;; | |
secret|build|log ) sub=$i ;; | |
* ) ;; | |
esac | |
done | |
# Handle _some_ arg values | |
case "$prev" in | |
--engine ) opts='native' ;; | |
--type ) opts='org shared repo' ;; | |
--org ) opts="$(__get_org_names)" ;; | |
--repo ) opts="$(__get_repo_names)" ;; | |
-b|--build|--build-number ) opts="$(__get_build_numbers)" ;; | |
-* ) ;; | |
* ) | |
# Handle second level args | |
case "$sub" in | |
secret ) | |
opts='--engine --type --org --repo --team ' | |
case "$cmd" in | |
add|update ) opts+='--name --value --image --event --filename' ;; | |
get ) opts+='--page --per-page -p --pp --output';; | |
remove ) opts+='--name' ;; | |
view ) opts+='--name --output' ;; | |
* ) ;; | |
esac | |
;; | |
build ) | |
opts='--org --repo ' | |
case "$cmd" in | |
get ) opts+='--page -p --per-page --pp --output' ;; | |
view ) opts+='--build-number -b --build --output' ;; | |
* ) ;; | |
esac | |
;; | |
log ) opts='--org --repo --build-number -b --build' ;; | |
* ) ;; | |
esac | |
;; | |
esac | |
fi | |
COMPREPLY=( $(compgen -W '$opts' -- "$cur") ) | |
return 0 | |
} | |
complete -F _vela vela |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment