Skip to content

Commit

Permalink
use --format=json, for juju-core nondependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jjo committed Jan 20, 2014
1 parent 7022ced commit 1f086fe
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions juju/tools/juju.autocomp
@@ -0,0 +1,60 @@
#!/bin/bash
# juju.autocomp: bash completion add-on for juju {ssh,set,get,status,upgrade-charm,...}
# requires JUJU_ENV already setup.
#
# Author: JuanJo Ciarlante <jjo@canonical.com>
# Copyright 2013+, Canonical Ltd.
# License: GPLv3
#
# TODO: --format=json, for juju-core nondependencies
. /etc/bash_completion.d/juju

_juju_units_from_file() {
python -c 'import yaml, sys;j=yaml.load(sys.stdin);print "\n".join([" ".join(v.get("units",{}).keys()) for k,v in j["services"].items() if v.get("units", None)]);' < ${1?}
}
_juju_services_from_file() {
python -c 'import yaml, sys;j=yaml.load(sys.stdin);print "\n".join(j["services"].keys());' < ${1?}
}
_juju_complete_cached() {
local fun=${1?} add="$2"
local cur
cur="${COMP_WORDS[COMP_CWORD]}"
local cache_dir=$HOME/.cache/juju
local juju_cache_status=${cache_dir}/juju-status-${JUJU_ENV?}
test -d ${cache_dir} || install -d $HOME/.cache ${cache_dir} -m 700
if [[ -z $(find ${juju_cache_status} -mmin -60 -a -size +32c 2> /dev/null) ]]; then
[[ -n "${JUJU_STATUS}" && -f "${JUJU_STATUS}" ]] && cp -p "${JUJU_STATUS}" "${juju_cache_status}" || \
(juju status > "${juju_cache_status}".tmp && mv "${juju_cache_status}".tmp "${juju_cache_status}")
fi
if [ -n "${add}" ];then
# Insert runtime completions at front
COMPREPLY=( $( compgen -W "$(${fun} ${juju_cache_status})" -- ${cur} ) ${COMPREPLY[@]})
else
COMPREPLY=( $( compgen -W "$(${fun} ${juju_cache_status})" -- ${cur} ))
fi
return 0
}
_juju_cache_invalidate() {
rm -fv $HOME/.cache/juju/juju-status-${JUJU_ENV?}
}
_juju_runtime() {
local prev pprev
prev="${COMP_WORDS[COMP_CWORD-1]}"
_juju "$@"
case "${prev}" in
ssh|scp|remove-unit|debug-hooks|resolved)
_juju_complete_cached _juju_units_from_file;
return $?;;
status|upgrade-charm|*expose|destroy-service|set|get|add-unit|add-relation|remove-relation)
_juju_complete_cached _juju_services_from_file true;
return $?;;
esac
pprev="${COMP_WORDS[COMP_CWORD-2]}"
case "${pprev}" in
add-relation|remove-relation)
_juju_complete_cached _juju_services_from_file true;
return $?;;
esac
}
complete -F _juju_runtime juju
# vim: ai et sw=2 ts=2

0 comments on commit 1f086fe

Please sign in to comment.