forked from berkshelf/berkshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
berkshelf-complete.sh
75 lines (65 loc) · 1.63 KB
/
berkshelf-complete.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# Description
# Add tab completion for berkshelf
#
# Configuration:
# BERKSHELF_BERKSFILE (default is 'Berksfile')
#
# Notes:
# Must be added to your ~/.bashrc, ~/.zshrc, etc
#
# Author:
# Seth Vargo <[email protected]>
#
# License:
# Apache 2.0
#
_bundler() {
which bundle > /dev/null 2>&1 && [ -f "$(pwd)/Gemfile" ]
}
# Overwrite berks to use bundler if defined
_berks() {
[ _bundler ] && bundle exec berks $@ || berks $@
}
_berkshelf_commands() {
local cachefile=~/.berkshelf/.commands
[ ! -f $cachefile ] && $(_berks help | grep berks | cut -d " " -f 4 > $cachefile)
cat $cachefile
}
_berkshelf_cookbooks() {
local file=${BERKSHELF_BERKSFILE:-Berksfile}
if [ -e $file ]; then
# strip all quotes from cookbook name and remove trailing comma, if any
grep -w '^cookbook' $file \
| awk '{ print $2 }' \
| sed 's/"//g' \
| sed "s/'//g" \
| sed 's/,$//'
fi
}
_local_cookbooks() {
[ -d cookbooks ] && ls -d cookbooks/*/ | cut -d "/" -f 2
}
_berkshelf() {
# local curr prev commands
COMPREPLY=()
curr="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# List of commands to complete
commands=`_berkshelf_commands`
case "${prev}" in
"open"|"outdated"|"show"|"update"|"upload")
local berkshelf_cookbooks=`_berkshelf_cookbooks`
local local_cookbooks=`_local_cookbooks`
local cookbooks=`echo $berkshelf_cookbooks $local_cookbooks | sort -n | uniq`
COMPREPLY=($(compgen -W "${cookbooks}" -- ${curr}))
return 0
;;
*)
;;
esac
COMPREPLY=($(compgen -W "${commands}" -- ${curr}))
return 0
}
complete -F _berkshelf berks