| 1 |
# mirrorbits(1) completion -*- shell-script -*- |
| 2 |
# ex: ts=4 sw=4 et filetype=sh |
| 3 |
# |
| 4 |
# Copyright (c) 2024 Arnaud Rebillout <[email protected]> |
| 5 |
# Distributed under the same license as mirrorbits. |
| 6 |
|
| 7 |
_in_array() { |
| 8 |
local i |
| 9 |
for i in "${@:2}"; do |
| 10 |
[[ $1 = "$i" ]] && return |
| 11 |
done |
| 12 |
} |
| 13 |
|
| 14 |
_mirrorbits_list() { |
| 15 |
local port=$1 |
| 16 |
mirrorbits -p $port list -state=false | tail -n +2 || : |
| 17 |
} |
| 18 |
|
| 19 |
_mirrorbits() { |
| 20 |
# Variables assigned by _init_completion: |
| 21 |
# cur Current argument. |
| 22 |
# prev Previous argument. |
| 23 |
# words Argument array. |
| 24 |
# cword Argument array size. |
| 25 |
local cur prev words cword |
| 26 |
_init_completion || return |
| 27 |
|
| 28 |
# Mirrorbits cli options, must come before the command |
| 29 |
local CLI_OPTIONS=( |
| 30 |
"-P" |
| 31 |
"-a" |
| 32 |
"-cpuprofile" |
| 33 |
"-debug" |
| 34 |
"-h" |
| 35 |
"-p") |
| 36 |
|
| 37 |
# Mirrorbits commands |
| 38 |
local COMMANDS=( |
| 39 |
"add" |
| 40 |
"daemon" |
| 41 |
"disable" |
| 42 |
"edit" |
| 43 |
"enable" |
| 44 |
"export" |
| 45 |
"geoupdate" |
| 46 |
"list" |
| 47 |
"logs" |
| 48 |
"refresh" |
| 49 |
"reload" |
| 50 |
"remove" |
| 51 |
"scan" |
| 52 |
"show" |
| 53 |
"stats" |
| 54 |
"upgrade" |
| 55 |
"version") |
| 56 |
|
| 57 |
# Check if a command was already given |
| 58 |
local command i |
| 59 |
for (( i = 1; i < cword; i++ )); do |
| 60 |
if _in_array "${words[i]}" "${COMMANDS[@]}"; then |
| 61 |
command=${words[i]} |
| 62 |
break |
| 63 |
fi |
| 64 |
done |
| 65 |
|
| 66 |
# Check if a port was already given, tricky as we must |
| 67 |
# support -p X, -p=X, --p X and --p=X |
| 68 |
local port |
| 69 |
for (( i = 1; i < cword; i++ )); do |
| 70 |
if [[ ! ${words[i]} =~ ^[0-9]+$ ]]; then |
| 71 |
continue |
| 72 |
fi |
| 73 |
if [[ ${words[i - 1]} =~ ^-?-p$ ]]; then |
| 74 |
port=${words[i]} |
| 75 |
break |
| 76 |
fi |
| 77 |
if [[ ${words[i - 1]} == = ]] && (( i > 1 )) && [[ ${words[i - 2]} =~ ^-?-p$ ]]; then |
| 78 |
port=${words[i]} |
| 79 |
break |
| 80 |
fi |
| 81 |
done |
| 82 |
|
| 83 |
# No port? Default port is 3390 |
| 84 |
if [[ -z $port ]]; then |
| 85 |
port=3390 |
| 86 |
fi |
| 87 |
|
| 88 |
# Completion per command |
| 89 |
if [[ -n $command ]]; then |
| 90 |
case $command in |
| 91 |
daemon) |
| 92 |
COMPREPLY=( $( compgen -W '-help -config -cpuprofile |
| 93 |
-debug -log -monitor -p' -- "$cur" ) ) |
| 94 |
;; |
| 95 |
add) |
| 96 |
COMPREPLY=( $( compgen -W '-help -admin-email -admin-name |
| 97 |
-as-only -comment -continent-only -country-only |
| 98 |
-custom-data -ftp -http -rsync -score |
| 99 |
-sponsor-logo -sponsor-name -sponsor-url |
| 100 |
' -- "$cur" ) ) |
| 101 |
;; |
| 102 |
disable|edit|enable|show) |
| 103 |
case $cur in |
| 104 |
-*) |
| 105 |
COMPREPLY=( $( compgen -W '-help' -- "$cur" ) ) |
| 106 |
;; |
| 107 |
*) |
| 108 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 109 |
;; |
| 110 |
esac |
| 111 |
;; |
| 112 |
export) |
| 113 |
COMPREPLY=( $( compgen -W '-help -disabled -ftp |
| 114 |
-http -rsync' -- "$cur" ) ) |
| 115 |
;; |
| 116 |
geoupdate) |
| 117 |
case $cur in |
| 118 |
-*) |
| 119 |
COMPREPLY=( $( compgen -W '-help -f' -- "$cur" ) ) |
| 120 |
;; |
| 121 |
*) |
| 122 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 123 |
;; |
| 124 |
esac |
| 125 |
;; |
| 126 |
list) |
| 127 |
COMPREPLY=( $( compgen -W '-help -disabled -down -enabled |
| 128 |
-ftp -http -location -rsync -score -state |
| 129 |
' -- "$cur" ) ) |
| 130 |
;; |
| 131 |
logs) |
| 132 |
case $cur in |
| 133 |
-*) |
| 134 |
COMPREPLY=( $( compgen -W '-help -l' -- "$cur" ) ) |
| 135 |
;; |
| 136 |
*) |
| 137 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 138 |
;; |
| 139 |
esac |
| 140 |
;; |
| 141 |
refresh) |
| 142 |
COMPREPLY=( $( compgen -W '-help -rehash' -- "$cur" ) ) |
| 143 |
;; |
| 144 |
reload|upgrade|version) |
| 145 |
COMPREPLY=( $( compgen -W '-help' -- "$cur" ) ) |
| 146 |
;; |
| 147 |
remove) |
| 148 |
case $cur in |
| 149 |
-*) |
| 150 |
COMPREPLY=( $( compgen -W '-help -f' -- "$cur" ) ) |
| 151 |
;; |
| 152 |
*) |
| 153 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 154 |
;; |
| 155 |
esac |
| 156 |
;; |
| 157 |
scan) |
| 158 |
case $cur in |
| 159 |
-*) |
| 160 |
COMPREPLY=( $( compgen -W '-help -all -enable -ftp |
| 161 |
-rsync -timeout' -- "$cur" ) ) |
| 162 |
;; |
| 163 |
*) |
| 164 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 165 |
;; |
| 166 |
esac |
| 167 |
;; |
| 168 |
stats) |
| 169 |
case $cur in |
| 170 |
-*) |
| 171 |
COMPREPLY=( $( compgen -W '-help -end-date -h -start-date |
| 172 |
' -- "$cur" ) ) |
| 173 |
;; |
| 174 |
*) |
| 175 |
if _in_array mirror "${words[@]:2}"; then |
| 176 |
COMPREPLY=( $( compgen -W "$( _mirrorbits_list $port )" -- "$cur" ) ) |
| 177 |
elif _in_array file "${words[@]:2}"; then |
| 178 |
COMPREPLY=() |
| 179 |
else |
| 180 |
COMPREPLY=( $( compgen -W 'file mirror' -- "$cur" ) ) |
| 181 |
fi |
| 182 |
;; |
| 183 |
esac |
| 184 |
;; |
| 185 |
*) |
| 186 |
COMPREPLY=() |
| 187 |
;; |
| 188 |
esac |
| 189 |
else # no command yet |
| 190 |
case "$cur" in |
| 191 |
-*) |
| 192 |
COMPREPLY=( $( compgen -W '${CLI_OPTIONS[@]}' -- "$cur" ) ) |
| 193 |
;; |
| 194 |
*) |
| 195 |
COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur" ) ) |
| 196 |
;; |
| 197 |
esac |
| 198 |
fi |
| 199 |
|
| 200 |
return 0 |
| 201 |
} && |
| 202 |
complete -F _mirrorbits mirrorbits |