-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completion
65 lines (59 loc) · 1.89 KB
/
bash_completion
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
#!/bin/bash -e
if ! command -v curl > /dev/null && ! command -v wget > /dev/null; then
>&2 echo "WARNING: Could not find either curl ( https://curl.haxx.se/ ) or wget ( https://www.gnu.org/software/wget/ )."
fi
if ! command -v jq > /dev/null; then
>&2 echo "WARNING: Could not find jq ( https://stedolan.github.io/jq/ )."
fi
__cppsm_query() {
local URL="$1"
local QUERY="$2"
if command -v jq > /dev/null; then
if command -v curl > /dev/null; then
local DOWNLOAD_CMD=(curl -s "$URL")
elif command -v wget > /dev/null; then
local DOWNLOAD_CMD=(wget -qO- "$URL")
else
return 0
fi
"${DOWNLOAD_CMD[@]}" | jq "$QUERY"
fi
}
__cppsm_complete() {
local IFS=$' \t\n'
COMPREPLY=()
local PREVIOUS="${3#\"}"
PREVIOUS="${PREVIOUS%\"}"
local CURRENT="${2#\"}"
CURRENT="${CURRENT%\"}"
case "$PREVIOUS" in
cppsm)
local COMMANDS_DIR
COMMANDS_DIR="$(command -v cppsm)"
COMMANDS_DIR="${COMMANDS_DIR%/*}/../commands"
# shellcheck disable=SC2207 disable=SC2035
COMPREPLY=($(compgen -W "$(cd "$COMMANDS_DIR" && echo *)" -- "$CURRENT"))
;;
add)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "equipment requires" -- "$CURRENT"))
;;
clone)
# shellcheck disable=SC2207
COMPREPLY=($(__cppsm_query "https://api.github.com/search/repositories?q=topic:cppsm+${CURRENT##*/}" \
'.items | .[] | .ssh_url'))
;;
equipment|requires)
# shellcheck disable=SC2207
COMPREPLY=($(__cppsm_query "https://api.github.com/search/repositories?q=topic:cppsm+${CURRENT##*/}" \
'.items | .[] | .clone_url'))
;;
https:*.git|git*.git)
# shellcheck disable=SC2207
COMPREPLY=($(git ls-remote --heads "$PREVIOUS" | sed -e 's#[^ ]*\s*refs/heads/##g'))
;;
*)
;;
esac
}
complete -F __cppsm_complete -o default cppsm