Merge lp:~clint-fewbar/charm-tools/add-bash-completion into lp:~charmers/charm-tools/trunk

Proposed by Clint Byrum on 2012-05-23
Status: Merged
Approved by: Juan L. Negron on 2012-05-23
Approved revision: 145
Merged at revision: 143
Proposed branch: lp:~clint-fewbar/charm-tools/add-bash-completion
Merge into: lp:~charmers/charm-tools/trunk
Diff against target: 142 lines (+96/-2)
3 files modified
Makefile (+3/-0)
charm (+8/-2)
misc/bash-completion (+85/-0)
To merge this branch: bzr merge lp:~clint-fewbar/charm-tools/add-bash-completion
Reviewer Review Type Date Requested Status
Juan L. Negron 2012-05-23 Pending
Review via email: mp+107127@code.launchpad.net

Description of the Change

Adds bash completion for commands and for charm names in charm get

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2012-05-14 22:54:56 +0000
3+++ Makefile 2012-05-23 22:09:19 +0000
4@@ -5,6 +5,7 @@
5 mandir = $(DDIR)/share/man/man1
6 datadir = $(DDIR)/share/charm-tools
7 helperdir = $(DDIR)/share/charm-helper
8+confdir = $(DDIR)/etc
9 INSTALL = install
10
11 all:
12@@ -16,6 +17,8 @@
13 $(INSTALL) -t $(datadir) charm
14 $(INSTALL) -d $(bindir)
15 $(INSTALL) -d $(helperdir)
16+ $(INSTALL) -d $(confdir)/bash_completion.d
17+ $(INSTALL) misc/bash-completion $(confdir)/bash_completion.d/charm
18 ln -sf $(datadir)/charm $(bindir)
19 gzip $(mandir)/charm.1
20 cp -rf scripts templates $(datadir)
21
22=== modified file 'charm'
23--- charm 2012-05-23 19:59:06 +0000
24+++ charm 2012-05-23 22:09:19 +0000
25@@ -11,18 +11,24 @@
26 {
27 echo "usage: $1 [ --help|-h ] [ script_name ]" >&2
28 echo script choices are: >&2
29- exec ls -1 $script_home|sed -e 's/^/ /' >&2
30+ exec list_commands|sed -e 's/^/ /' >&2
31 exit $2
32 }
33
34+list_commands()
35+{
36+ ls -1 $script_home
37+}
38+
39 # If the options seem invalid, pass thru to underlying scripts
40-if options=$(getopt -o h -l help -- "$@") ; then
41+if options=$(getopt -o hl -l help,list -- "$@") ; then
42 eval set -- "$options"
43
44 while true
45 do
46 case "$1" in
47 -h|--help) usage $(basename $0) 0;;
48+ -l|--list) list_commands ; exit 0;;
49 --) shift 1; break;;
50 *) break;;
51 esac
52
53=== added directory 'misc'
54=== added file 'misc/bash-completion'
55--- misc/bash-completion 1970-01-01 00:00:00 +0000
56+++ misc/bash-completion 2012-05-23 22:09:19 +0000
57@@ -0,0 +1,85 @@
58+#!/bin/bash
59+shopt -s progcomp
60+
61+_charm_official_charms() {
62+ charm list | awk -Flp:charms/ '/^lp:charms/ { print $2 }'
63+}
64+
65+_charm () {
66+ local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
67+ local curOpt optEnums
68+ local IFS=$' \n'
69+
70+ COMPREPLY=()
71+ cur=${COMP_WORDS[COMP_CWORD]}
72+ cmds=`charm --list`
73+ globalOpts=( -h )
74+
75+ # do ordinary expansion if we are anywhere after a -- argument
76+ for ((i = 1; i < COMP_CWORD; ++i)); do
77+ [[ ${COMP_WORDS[i]} == "--" ]] && return 0
78+ done
79+
80+ # find the command; it's the first word not starting in -
81+ cmd=
82+ for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
83+ if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
84+ cmd=${COMP_WORDS[cmdIdx]}
85+ break
86+ fi
87+ done
88+
89+ # complete command name if we are not already past the command
90+ if [[ $COMP_CWORD -le cmdIdx ]]; then
91+ COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- "$cur" ) )
92+ return 0
93+ fi
94+
95+ # find the option for which we want to complete a value
96+ curOpt=
97+ if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
98+ curOpt=${COMP_WORDS[COMP_CWORD - 1]}
99+ if [[ "$curOpt" == = ]]; then
100+ curOpt=${COMP_WORDS[COMP_CWORD - 2]}
101+ elif [[ "$cur" == : ]]; then
102+ cur=
103+ curOpt="$curOpt:"
104+ elif [[ "$curOpt" == : ]]; then
105+ curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
106+ fi
107+ fi
108+
109+ cmdOpts=( )
110+ cmdOpts=(--help)
111+ optEnums=( )
112+ fixedWords=( )
113+ case "$cmd" in
114+ get)
115+ for charm in $(_charm_official_charms) ; do
116+ cmdOpts+=($charm)
117+ done
118+ ;;
119+ *)
120+ ;;
121+ esac
122+
123+ IFS=$'\n'
124+ if [[ "$cur" == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
125+ # complete directly after "--option=", list all enum values
126+ COMPREPLY=( "${optEnums[@]}" )
127+ return 0
128+ else
129+ fixedWords=( "${cmdOpts[@]}"
130+ "${globalOpts[@]}"
131+ "${optEnums[@]}"
132+ "${fixedWords[@]}" )
133+ fi
134+
135+ if [[ ${#fixedWords[@]} -gt 0 ]]; then
136+ COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- "$cur" ) )
137+ fi
138+
139+ return 0
140+}
141+
142+complete -F _charm -o default charm

Subscribers

People subscribed via source and target branches