-
Notifications
You must be signed in to change notification settings - Fork 5
/
dnsx.completion.bash
131 lines (110 loc) · 3.05 KB
/
dnsx.completion.bash
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
## http://kodango.com/bash-competion-programming
# 例如我们定义这样一个补全函数_foo:
# $ function _foo()
# > {
# > echo -e "\n"
# >
# > declare -p COMP_WORDS
# > declare -p COMP_CWORD
# > declare -p COMP_LINE
# > declare -p COMP_WORDBREAKS
# > }
# $ complete -F _foo foo
# 假设我们在命令行下输入以下内容,再按下Tab键补全:
# $ foo b
# declare -a COMP_WORDS='([0]="foo" [1]="b")'
# declare -- COMP_CWORD="1"
# declare -- COMP_LINE="foo b"
# declare -- COMP_WORDBREAKS="
# \"'><=;|&(:"
__dnsx_add_completions()
{
# domainOpts="qq.com rockontrol querycap rktl"
domainOpts="${*}"
# domainOpts=$(dnsx configure domains ${COMP_LINE})
typeOpts="A CNAME"
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case ${prev} in
add) COMPREPLY=( $(compgen -W "${typeOpts}" -- ${cur}) ); return 0 ;;
CNAME|A|a|cname) COMPREPLY=( $(compgen -W "${domainOpts}" -- ${cur}) ); return 0 ;;
*) COMPREPLY=( $(compgen -W "record value" -- ${cur}) ); return 0 ;;
esac
}
__dnsx__delete_completions()
{
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case ${prev} in
delete) COMPREPLY=( $(compgen -W "${domainOpts}" -- ${cur}) ); return 0 ;;
search) COMPREPLY=( $(compgen -W "${domainOpts}" -- ${cur}) ); return 0 ;;
switch) COMPREPLY=( $(compgen -W "${domainOpts}" -- ${cur}) ); return 0 ;;
esac
}
__dnsx_subcommand()
{
local prev
prev="${COMP_WORDS[COMP_CWORD-1]}"
case ${prev} {
}
}
_dnsx_completions()
{
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# params
case ${cur} in
-*)
COMPREPLY=( $(compgen -W "-p" -- ${cur}) )
return 0
;;
esac
case ${prev} in
"-p")
COMPREPLY=( $(compgen -W "$(dnsx configure list)" -- "${cur}") )
return 0
;;
esac
# commands
if [[ "${COMP_WORDS[1]}" == "-p" ]]; then
{
## dnsx -p profile add ...
[ -z "${COMP_WORDS[3]}" ] && {
command=${COMP_WORDS[0]}
} || {
command=${COMP_WORDS[3]}
}
}
elif [[ "${COMP_WORDS[2]}" == "-p" ]]; then
{
## dnsx add -p profile ...
command=${COMP_WORDS[1]}
}
else
{
## dnsx add ...
[ -n "${COMP_WORDS[1]}" ] && command=${COMP_WORDS[1]} || command=${COMP_WORDS[0]}
}
fi
# command=${prev}
# main command
subcmdOpts="add delete search switch configure help"
domainOpts=$(dnsx configure domains ${COMP_LINE})
case ${command} in
# "dnsx")
# COMPREPLY=( $(compgen -W "${subcmdOpts}" -- ${cur}) )
# return 0
# ;;
"add") __dnsx_add_completions ${domainOpts}; return 0 ;;
"delete"|"search"|"switch") __dnsx__delete_completions ${domainOpts} ; return 0 ;;
*)
COMPREPLY=( $(compgen -W "${subcmdOpts}" -- ${cur}) )
return 0
;;
esac
}
complete -F _dnsx_completions dnsx