|
5 | 5 | # Inspired by https://github.com/felixr/docker-zsh-completion |
6 | 6 |
|
7 | 7 | __scw_get_servers() { |
8 | | - local kind expl |
9 | | - declare -a running stopped lines args |
10 | | - |
11 | | - kind=$1 |
12 | | - shift |
13 | | - [[ $kind = (stopped|all) ]] && args=($args -a) |
14 | | - |
15 | | - lines=(${(f)"$(_call_program commands scw ps ${args})"}) |
16 | | - |
17 | | - # Parse header line to find columns |
18 | | - local i=1 j=1 k header=${lines[1]} |
19 | | - declare -A begin end |
20 | | - while (( $j < ${#header} - 1 )) { |
21 | | - i=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 1)) |
22 | | - j=$(( $i + ${${header[$i,-1]}[(i) ]} - 1)) |
23 | | - k=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 2)) |
24 | | - begin[${header[$i,$(($j-1))]}]=$i |
25 | | - end[${header[$i,$(($j-1))]}]=$k |
26 | | - } |
27 | | - lines=(${lines[2,-1]}) |
28 | | - |
29 | | - # Server ID |
30 | | - local line |
31 | | - local s |
32 | | - for line in $lines; do |
33 | | - s="${line[${begin[SERVER ID]},${end[SERVER ID]}]%% ##}" |
34 | | - s="$s:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}" |
35 | | - s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}" |
36 | | - if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then |
37 | | - stopped=($stopped $s) |
38 | | - else |
39 | | - running=($running $s) |
40 | | - fi |
41 | | - done |
42 | | - |
43 | | - # Names |
44 | | - local name |
45 | | - local -a names |
46 | | - for line in $lines; do |
47 | | - names=(${(ps:,:)${${line[${begin[NAMES]},-1]}%% *}}) |
48 | | - for name in $names; do |
49 | | - s="${name}:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}" |
50 | | - s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}" |
51 | | - if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then |
52 | | - stopped=($stopped $s) |
53 | | - else |
54 | | - running=($running $s) |
55 | | - fi |
56 | | - done |
57 | | - done |
58 | | - |
59 | | - [[ $kind = (running|all) ]] && _describe -t servers-running "running servers" running |
60 | | - [[ $kind = (stopped|all) ]] && _describe -t servers-stopped "stopped servers" stopped |
| 8 | + local expl |
| 9 | + declare -a servers |
| 10 | + servers=(${(f)"$(_call_program commands scw _completion servers-all)"}) |
| 11 | + _describe -t servers "servers" servers |
61 | 12 | } |
62 | 13 |
|
63 | 14 | __scw_stoppedservers() { |
64 | | - __scw_get_servers stopped "$@" |
| 15 | + __scw_get_servers |
65 | 16 | } |
66 | 17 |
|
67 | 18 | __scw_runningservers() { |
68 | | - __scw_get_servers running "$@" |
| 19 | + __scw_get_servers |
69 | 20 | } |
70 | 21 |
|
71 | 22 | __scw_servers () { |
72 | | - __scw_get_servers all "$@" |
| 23 | + __scw_get_servers |
73 | 24 | } |
74 | 25 |
|
75 | 26 | __scw_images () { |
76 | 27 | local expl |
77 | 28 | declare -a images |
78 | | - images=(${${${${(f)"$(_call_program commands scw images)"}[2,-1]}/ ##/\\:}%% *}) |
79 | | - images=(${${images%\\:<none>}#<none>} ${${${(f)"$(_call_program commands scw images)"}[2,-1]}/(#b)([^ ]##) ##([^ ]##) ##([^ ]##)*/${match[3]}:${(r:15:: :::)match[2]} in ${match[1]}}) |
80 | | - _describe -t scw-images "images" images |
| 29 | + images=(${(f)"$(_call_program commands scw _completion images-all)"}) |
| 30 | + _describe -t images "images" images |
81 | 31 | } |
82 | 32 |
|
83 | 33 | __scw_tags() { |
84 | | - local expl |
85 | | - declare -a tags |
86 | | - tags=(${${${${${(f)"$(_call_program commands scw images)"}#* }## #}%% *}[2,-1]}) |
87 | | - _describe -t scw-tags "tags" tags |
| 34 | + __scw_images |
88 | 35 | } |
89 | 36 |
|
90 | 37 | __scw_repositories_with_tags() { |
91 | | - if compset -P '*:'; then |
92 | | - __scw_tags |
93 | | - else |
94 | | - __scw_repositories -qS ":" |
95 | | - fi |
| 38 | + __scw_images |
96 | 39 | } |
97 | 40 |
|
98 | 41 | __scw_search() { |
@@ -126,10 +69,7 @@ __scw_caching_policy() |
126 | 69 |
|
127 | 70 |
|
128 | 71 | __scw_repositories () { |
129 | | - local expl |
130 | | - declare -a repos |
131 | | - repos=(${${${(f)"$(_call_program commands scw images)"}%% *}[2,-1]}) |
132 | | - _describe -t scw-repos "repositories" repos "$@" |
| 72 | + __scw_images |
133 | 73 | } |
134 | 74 |
|
135 | 75 | __scw_commands () { |
|
0 commit comments