Created
May 7, 2011 20:15
-
-
Save ches/960799 to your computer and use it in GitHub Desktop.
bash completion for Chef's knife command (not yet updated for 0.10)
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
# vim: ft=sh:ts=4:sw=4:autoindent: | |
# Author: Avishai Ish-Shalom <[email protected]> | |
# We need to specify GNU sed for OS X, BSDs, etc. | |
if [[ "$(uname -s)" == "Darwin" ]]; then | |
SED=gsed | |
else | |
SED=sed | |
fi | |
# first argument set the command level | |
_get_knife_completions() { | |
n=$1 | |
shift | |
# first argument is knife, so shift it | |
#[ "$1" == "knife" ] && shift | |
local opts | |
opts="$($@ --help | grep -E '^knife' | cut -f$n -d" " | grep -v '(options)')" | |
_upvar opts "$opts" | |
} | |
_flatten_knife_command() { | |
echo ${words[*]} |${SED} -r -e "s/\ *${words[$cword]}\$//" -e 's/\W/_/g' | |
} | |
# Check cache file for category ( passed as $1 ) and run command if cache is empty | |
# Designed to be used with _get_knife_completions() and use the opts variables for options | |
_completion_cache() { | |
local CACHE_DIR=${CHEF_HOME:-"$HOME/.chef"}/.completion_cache | |
local flag COMMAND | |
local OPTIND=1 | |
while getopts "c" flag "$@"; do | |
case $flag in | |
c) | |
COMMAND=yes | |
;; | |
*) | |
;; | |
esac | |
done | |
shift $(( $OPTIND - 1 )) | |
local CACHE_FILE="$CACHE_DIR/$1" | |
shift | |
if [ ! -f "$CACHE_FILE" ]; then | |
if [[ "$COMMAND" == "yes" ]]; then | |
opts=$( eval $@ ) | |
else | |
$@ | |
fi | |
[ -d "$CACHE_DIR" ] && echo $opts >"$CACHE_FILE" | |
else | |
opts=$(cat "$CACHE_FILE") | |
fi | |
_upvar opts "$opts" | |
} | |
_knife() { | |
local opts cur prev cword words flattened_knife_command | |
opts="bootstrap client configure cookbook data ec2 exec index node rackspace recipe role search slicehost ssh status terremark windows" | |
_get_comp_words_by_ref cur prev cword words | |
flattened_knife_command=$(_flatten_knife_command) | |
COMPREPLY=() | |
case $flattened_knife_command in | |
*knife_cookbook_upload|*knife_cookbook_test) | |
local chef_repos | |
if [[ -z $CHEF_REPOS ]]; then | |
chef_repos=( $(${SED} -rn '/cookbook_path/ {s/.*\[(.*)\]/\1/g; s/[,'\'']//g; p}' ${CHEF_HOME:-"$HOME/.chef"}/knife.rb) ) | |
else | |
chef_repos=( ${CHEF_REPOS[@]} ) | |
fi | |
if [[ -n "$chef_repos" ]]; then | |
opts=$( ls -1p ${chef_repos[@]} | sort -u | ${SED} -n 's/\/$//p' ) | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
fi | |
;; | |
*knife_data) | |
opts="bag" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*knife_node_show|*knife_node_edit|*knife_node_delete) | |
_completion_cache -c knife_nodes "${words[0]} node list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*knife_role_edit|*knife_role_show|*knife_role_delete) | |
_completion_cache -c knife_roles "${words[0]} role list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*knife_data_bag_delete|*knife_data_bag_show|*knife_data_bag_edit) | |
_completion_cache -c knife_data_bags "${words[0]} data bag list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*knife_data_bag_delete_*|*knife_data_bag_show_*|*knife_data_bag_edit_*) | |
_completion_cache -c knife_data_bag_$prev "${words[0]} data bag show $prev 2>/dev/null|${SED} -r -e 's/[\"\ ,]//g' -e '/^[^0-9A-Za-z._-]+/d'" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*knife_client_list|*knife_client_show|*knife_client_edit) | |
_completion_cache -c knife_clients "${words[0]} client list|${SED} -r -e 's/[\"\ ,]//g' -e '/[^0-9A-Za-z._-]+/d'" | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*) | |
case $cword in | |
1) | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
*) | |
_completion_cache $flattened_knife_command _get_knife_completions $(( $cword + 1 )) ${words[*]} | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
;; | |
esac | |
;; | |
esac | |
[[ ${#COMPREPLY[@]} -ge 1 ]] && return 0 | |
} | |
complete -F _knife knife |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I added options fro data bag upload from file. Maybe usefull to others.