Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use --format=json, for juju-core nondependencies
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |