~axwalk/juju-core/lp1303195-manual-ubuntuuser-bash

« back to all changes in this revision

Viewing changes to etc/bash_completion.d/juju-core

  • Committer: JuanJo Ciarlante
  • Date: 2014-02-07 20:16:04 UTC
  • mto: This revision was merged to the branch mainline in revision 2386.
  • Revision ID: jjo@canonical.com-20140207201604-funtzzi7f9ohjz5h
add etc/bash_completion.d/juju-core

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# juju.autocomp: dynamic bash completion for juju cmdline
 
3
#
 
4
# Author: JuanJo Ciarlante <jjo@canonical.com>
 
5
# Copyright 2013+, Canonical Ltd.
 
6
# License: GPLv3
 
7
#
 
8
# DISCLAIMER: using bash completion is great,
 
9
#             cleanly implementing it is ... impossible ?.
 
10
#             DEAL WITH IT --jjo
 
11
 
 
12
# Show all machines
 
13
_juju_machines_from_file() {
 
14
    python -c 'import json, sys;j=json.load(sys.stdin);print "\n".join(j["machines"].keys());' < ${1?}
 
15
}
 
16
 
 
17
# Show all units, each optionally postfixed by $2 (eg. 'myservice/0:')
 
18
_juju_units_from_file() {
 
19
    python -c '
 
20
s="'${2}'"
 
21
import json, sys
 
22
j=json.load(sys.stdin)
 
23
all_units=[]
 
24
for k,v in j["services"].items():
 
25
    if v.get("units"):
 
26
        all_units.extend(v.get("units",{}).keys())
 
27
print "\n".join([unit + s for unit in all_units])
 
28
' < ${1?}
 
29
}
 
30
 
 
31
# Show services
 
32
_juju_services_from_file() {
 
33
    python -c 'import json, sys;j=json.load(sys.stdin);print "\n".join(j["services"].keys());' < ${1?}
 
34
}
 
35
 
 
36
# Show both services and units, currently used for juju status completion
 
37
_juju_services_and_units_from_file() {
 
38
    _juju_services_from_file "$@"
 
39
    _juju_units_from_file "$@"
 
40
}
 
41
 
 
42
# Main completion function:
 
43
# - setups caching dir if non-existent
 
44
# - caches juju status output, $cache_mins minutes max
 
45
# - parses cached status file with passed func
 
46
# - optionally adds compgen_xtra options, words_xtra
 
47
_juju_complete_cached() {
 
48
    local func=${1?} postfix_str="${2}" compgen_xtra="${3}" words_xtra="${4}"
 
49
    local cache_mins=60
 
50
    local cur
 
51
    local cache_dir=$HOME/.cache/juju
 
52
    local juju_status_file=${cache_dir}/juju-status-${JUJU_ENV:-default}
 
53
    cur="${COMP_WORDS[COMP_CWORD]}"
 
54
    # setup caching dir under ~/.cache/juju
 
55
    test -d ${cache_dir} || install -d ${cache_dir} -m 700
 
56
    if [[ -z $(find "${juju_status_file}" -mmin -${cache_mins} -a -size +32c 2> /dev/null) ]]; then
 
57
        juju status --format=json > "${juju_status_file}".tmp && \
 
58
            mv "${juju_status_file}".tmp "${juju_status_file}"
 
59
        rm -f "${juju_status_file}".tmp
 
60
    fi
 
61
    # if a postfix_str is passed (eg ':'), allow nospace
 
62
    if [ -n "${postfix_str}" ]; then
 
63
        compopt -o nospace
 
64
    else
 
65
        compopt +o nospace
 
66
    fi
 
67
    [[ -r "${juju_status_file}" ]] || return 0
 
68
    # build COMPREPLY from passed function stdout
 
69
    COMPREPLY=( $( compgen ${compgen_xtra} -W "${words_xtra} $(${func} ${juju_status_file} ${postfix_str})" -- ${cur} ))
 
70
    return 0
 
71
}
 
72
 
 
73
# Not used here, available to the user for quick cache removal
 
74
_juju_completion_cache_rm() {
 
75
    rm -fv $HOME/.cache/juju/juju-status-${JUJU_ENV?}
 
76
}
 
77
 
 
78
# Get juju commands from its help
 
79
_juju_commands()
 
80
{
 
81
    local cur prev options files targets
 
82
    COMPREPLY=()
 
83
    cur="${COMP_WORDS[COMP_CWORD]}"
 
84
    prev="${COMP_WORDS[COMP_CWORD-1]}"
 
85
    actions=$(juju help commands 2>/dev/null | awk '{print $1}')
 
86
    COMPREPLY=( $( compgen -W "${actions}" -- ${cur} ) )
 
87
    return 0
 
88
}
 
89
 
 
90
# Entry completion function
 
91
_juju() {
 
92
    local prev status
 
93
    local orig_comp_wordbreaks="${COMP_WORDBREAKS}"
 
94
    prev="${COMP_WORDS[COMP_CWORD-1]}"
 
95
    COMPREPLY=()
 
96
    case "${prev}" in
 
97
        # non dynamic completions:
 
98
        juju|help)
 
99
            _juju_commands "$@";;
 
100
        # services:
 
101
        status)
 
102
            _juju_complete_cached _juju_services_and_units_from_file;;
 
103
        upgrade-charm|*expose|*-service|*-constraints|set|get)
 
104
            _juju_complete_cached _juju_services_from_file;;
 
105
        add-unit)
 
106
            _juju_complete_cached _juju_services_from_file "" "" "-n --to";;
 
107
        # units:
 
108
        ssh|remove-unit|debug-hooks)
 
109
            _juju_complete_cached _juju_units_from_file;;
 
110
        scp)
 
111
            # tmp remove ':' from as word separator
 
112
            COMP_WORDBREAKS="${COMP_WORDBREAKS/:/}"
 
113
            # append ':' to units, also complete filenames
 
114
            _juju_complete_cached _juju_units_from_file ":" "-A file";;
 
115
        resolved)
 
116
            _juju_complete_cached _juju_units_from_file "" "" "-r";;
 
117
        *-relation)
 
118
            _juju_complete_cached _juju_services_from_file;;
 
119
        # machines
 
120
        *-machine)
 
121
            _juju_complete_cached _juju_machines_from_file;;
 
122
    esac
 
123
    status=$?
 
124
    if [ -z "${COMPREPLY}" ]; then
 
125
        # another case switch, for actions requiring completion for >3rd
 
126
        # argument, e.g.: add-relation srv1 srv2
 
127
        local juju_action="${COMP_WORDS[1]}"
 
128
        case "${juju_action}" in
 
129
            # non-dynamic actions (simplified)
 
130
            bootstrap)
 
131
                local words="--constraints --series --source --upload-tools"
 
132
                COMPREPLY=( $( compgen -W "${words}" -- ${COMP_WORDS[COMP_CWORD]} ));;
 
133
            upgrade-juju)
 
134
                local words="--dev --series --upload-tools --version"
 
135
                COMPREPLY=( $( compgen -W "${words}" -- ${COMP_WORDS[COMP_CWORD]} ));;
 
136
            get-env*)
 
137
                local words="--format -o"
 
138
                COMPREPLY=( $( compgen -W "${words}" -- ${COMP_WORDS[COMP_CWORD]} ));;
 
139
            # services:
 
140
            *-relation)
 
141
                _juju_complete_cached _juju_services_from_file;;
 
142
            # (some) actions with optional switches:
 
143
            scp)
 
144
                COMP_WORDBREAKS="${COMP_WORDBREAKS/:/}"
 
145
                _juju_complete_cached _juju_units_from_file ":" "-A file";;
 
146
            resolved)
 
147
                _juju_complete_cached _juju_units_from_file;;
 
148
            add-unit)
 
149
                _juju_complete_cached _juju_services_from_file;;
 
150
        esac
 
151
        status=$?
 
152
    fi
 
153
    COMP_WORDBREAKS="${orig_comp_wordbreaks}"
 
154
    return ${status}
 
155
}
 
156
complete -F _juju juju
 
157
# vim: ai et sw=2 ts=2