Skip to content

Commit

Permalink
Merge pull request #71 from jrwren/002-generate-bash-completion
Browse files Browse the repository at this point in the history
add bash completion
  • Loading branch information
jujugui committed Jun 9, 2016
2 parents 95475c7 + 8954291 commit 8888953
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,38 @@ man/man1:
# The install-man make target are for use by debian packaging.
# The semantics should match autotools make files as dh_make expects it.
install-man: man/man1
mkdir -p $(DESTDIR)/usr/share/man/man1
for file in man/man1/* ; do \
$(INSTALL_FILE) $$file "$(DESTDIR)/usr/share/man/man1" ; done

uninstall-man: man/man1
for file in man/man1/* ; do \
rm "$(DESTDIR)/usr/share/$$file" ; done
-rmdir -p $(DESTDIR)/usr/share/man/man1

install-bash-completion:
mkdir -p $(DESTDIR)/usr/share/bash-completion/completions
$(INSTALL_FILE) config/bash/charm "$(DESTDIR)/usr/share/bash-completion/completions/"

uninstall-bash-completion:
rm "$(DESTDIR)/usr/share/bash-completion/completions/charm"
-rmdir -p $(DESTDIR)/usr/share/bash-completion/completions

help:
@echo -e 'Charmstore-client - list of make targets:\n'
@echo 'make - Build the package.'
@echo 'make check - Run tests.'
@echo 'make install - Install the package.'
@echo 'make install - Install the package to $$GOPATH/bin'
@echo 'make clean - Remove object files from package source directories.'
@echo 'make deps - Set up the project Go dependencies.'
@echo 'make create-deps - Generate the Go dependencies file.'
@echo 'make format - Format the source files.'
@echo 'make man - Generate man pages.'
@echo 'make simplify - Format and simplify the source files.'
@echo 'make install-man - Install man pages to $$DESTDIR/usr/share/man'
@echo 'make uninstall-man - Remove man pages from $$DESTDIR/usr/share/man'
@echo 'make install-bash-completion - Install completion to $$DESTDIR/usr/share/bash-completion/completions/'
@echo 'make uninstall-bash-completion - Remove completion from $$DESTDIR/usr/share/bash-completion/completions/'

.PHONY: build check clean create-deps deps format help install simplify
.PHONY: build check clean create-deps deps format help install simplify \
install-man uninstall-man install-bash-completion uninstall-bash-completion
47 changes: 47 additions & 0 deletions config/bash/charm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# bash completion for the charm command.
#
# adapted from https://github.com/jjo/juju/blob/11e1a81c012a45d285545ff84db336b3ed9a4b78/etc/bash_completion.d/juju
#
# Copyright 2016 Canonical Ltd.

_complete_with_func() {
local action="${1}" func=${2?}
local flags=$(_flags_for "${action}")
local cur="${COMP_WORDS[COMP_CWORD]}"
_get_comp_words_by_ref -n : cur
COMPREPLY=( $( compgen -W "$(${func} ${juju_status_file} ${postfix_str}) $flags" -- ${cur} ))
}

_list_commands() {
${COMP_WORDS[0]} help 2>/dev/null | awk '{if (cmd==1){print $1}} /commands:/{cmd=1;}'
}

_flags_for() {
test -z "${1}" && return 0
${COMP_WORDS[0]} help ${1} 2>/dev/null |egrep -o -- '(^|-)-[a-z-]+'|sort -u
}

_completion_func_for_cmd() {
local action=${1} cword=${2}
# if cword==1 or action==help use _list_commands
if [[ "${cword}" -eq 1 || "${action}" == help ]]; then
echo _list_commands
return 0
fi
case $(${COMP_WORDS[0]} help ${action} 2>/dev/null | head -1) in
?*) echo true;;
*) echo false;;
esac
}

_completer() {
local action parsing_func
action="${COMP_WORDS[1]}"
COMPREPLY=()
parsing_func=$(_completion_func_for_cmd "${action}" ${COMP_CWORD})
test -n "${parsing_func}" && _complete_with_func "${action}" "${parsing_func}"
return $?
}

complete -F _completer charm
# vim: ai et sw=2 ts=2

0 comments on commit 8888953

Please sign in to comment.