forked from booski/passman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completions
85 lines (78 loc) · 2.13 KB
/
bash_completions
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
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
__contains() {
local value item
value="$1"
shift
for item in "$@"; do
if [ "$item" = "$value" ]; then
return 0
fi
done
return 1
}
__prepend() {
local char list out
char="$1"
shift
out=''
for item in "$@"; do
out="$out ${char}${item}"
done
printf "%s" "$out"
}
_passman() {
local cur prev base switches completions
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
base="get list info passwd help add del modify manage promote demote"
switches="-u -e"
completions=''
if [ "$cur" = -* ]; then
completions="$switches"
elif __contains "manage" "${COMP_WORDS[@]}"; then
case "$prev" in
"manage" )
completions="user pass"
;;
"user"|"pass" )
completions=$(passman list "$prev")
;;
* )
local preprev initial
preprev="${COMP_WORDS[COMP_CWORD-2]}"
if [ "$preprev" = "user" ] || [ "$preprev" = "pass" ]; then
initial=$(passman list group)
completions="$(__prepend "+" $initial)"
completions="$completions $(__prepend "-" $initial)"
fi
;;
esac
else
case "$prev" in
"passman")
completions="$base $switches"
;;
"help" )
if [ "${COMP_WORDS[1]}" = "help" ]; then
completions="$base"
fi
;;
"get"|"modify" )
completions=$(passman list pass)
;;
"list"|"info"|"add"|"del" )
completions="user group pass"
;;
"-u"|"promote"|"demote" )
completions=$(passman list user)
;;
"user"|"group"|"pass" )
completions=$(passman list "$prev")
;;
esac
fi
COMPREPLY=($(compgen -W "${completions}" -- "${cur}"))
return 0
}
complete -F _passman passman