1 # -*- shell-script -*- |
|
2 |
|
3 _ec_commands() |
|
4 { |
|
5 local commands |
|
6 commands="$("$ec" listcommands 2>/dev/null)" || commands="" |
|
7 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur")) |
|
8 } |
|
9 |
|
10 _ec() |
|
11 { |
|
12 local cur prev cmd cmd_index opts i |
|
13 local ec="$1" |
|
14 |
|
15 COMPREPLY=() |
|
16 cur="$2" |
|
17 prev="$3" |
|
18 |
|
19 # searching for the command |
|
20 # (first non-option argument that doesn't follow a global option that |
|
21 # receives an argument) |
|
22 for ((i=1; $i<=$COMP_CWORD; i++)); do |
|
23 if [[ ${COMP_WORDS[i]} != -* ]]; then |
|
24 cmd="${COMP_WORDS[i]}" |
|
25 cmd_index=$i |
|
26 break |
|
27 fi |
|
28 done |
|
29 |
|
30 if [[ "$cur" == -* ]]; then |
|
31 if [ -z "$cmd" ]; then |
|
32 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '--help' -- "$cur")) |
|
33 else |
|
34 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || commands="" |
|
35 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options' -- "$cur")) |
|
36 fi |
|
37 return |
|
38 fi |
|
39 |
|
40 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then |
|
41 _ec_commands |
|
42 return |
|
43 fi |
|
44 |
|
45 # try to generate completion candidates for whatever command the user typed |
|
46 if _ec_command_specific; then |
|
47 return |
|
48 fi |
|
49 } |
|
50 |
|
51 _ec_command_specific() |
|
52 { |
|
53 if [ "$(type -t "_ec_cmd_$cmd")" = function ]; then |
|
54 "_ec_cmd_$cmd" |
|
55 return 0 |
|
56 fi |
|
57 |
|
58 case "$cmd" in |
|
59 client) |
|
60 if [ "$prev" == "-b" ] || [ "$prev" == "--batch" ]; then |
|
61 COMPREPLY=( $( compgen -o filenames -G "$cur*" ) ) |
|
62 return |
|
63 fi |
|
64 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options="" |
|
65 instances="$("$ec" listinstances 2>/dev/null)" || instances="" |
|
66 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
67 ;; |
|
68 db-dump) |
|
69 if [ "$prev" == "-o" ] || [ "$prev" == "--output" ]; then |
|
70 COMPREPLY=( $( compgen -o filenames -G "$cur*" ) ) |
|
71 return |
|
72 fi |
|
73 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options="" |
|
74 instances="$("$ec" listinstances 2>/dev/null)" || instances="" |
|
75 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
76 ;; |
|
77 # commands with template as argument |
|
78 i18ncube) |
|
79 cubes="$("$ec" listcubes 2>/dev/null)" || cubes="" |
|
80 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $cubes' -- "$cur")) |
|
81 ;; |
|
82 # generic commands with instance as argument |
|
83 start|stop|reload|restart|upgrade|start-repository|db-create|db-init|db-check|db-grant-user) |
|
84 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options="" |
|
85 instances="$("$ec" listinstances 2>/dev/null)" || instances="" |
|
86 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
87 ;; |
|
88 # generic commands without argument |
|
89 list|newtemplate|i18ncubicweb|live-server) |
|
90 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options="" |
|
91 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
92 ;; |
|
93 # generic commands without option |
|
94 shell|i18ninstance|delete|status|schema-sync) |
|
95 instances="$("$ec" listinstances 2>/dev/null)" || instances="" |
|
96 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
97 ;; |
|
98 # XXX should do better |
|
99 create) |
|
100 options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options="" |
|
101 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
102 ;; |
|
103 db-copy,db-restore,mboximport) |
|
104 instances="$("$ec" listinstances 2>/dev/null)" || instances="" |
|
105 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur")) |
|
106 ;; |
|
107 *) |
|
108 return 1 |
|
109 ;; |
|
110 esac |
|
111 |
|
112 return 0 |
|
113 } |
|
114 |
|
115 complete -o bashdefault -o default -F _ec cubicweb-ctl 2>/dev/null \ |
|
116 || complete -o default -F _ec cubicweb-ctl |
|