-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprone.sh
executable file
·96 lines (69 loc) · 1.65 KB
/
prone.sh
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
#!/usr/bin/env bash
# vim:filetype=sh
set -e
lookup() {
local _dir="$1"
local plugin="$2"
local rev="$3"
local ref="$4"
# echo "downloading $plugin"
local tmpfile
tmpfile="$_dir/${plugin/\//_}"
# DEBUG:
# echo "{}" | jq '{name:$plugin}' --arg plugin "$plugin"
if [[ -z "$ref" ]]; then
ref=$(gh api "/repos/$plugin" | jq '.default_branch' | xargs -n 1 echo)
fi
gh api "/repos/$plugin/git/commits/$rev" |
jq '{name: $plugin, rev: .sha, ref: $ref}' --arg plugin "$plugin" --arg ref "$ref" |
tee "$tmpfile"
}
download() {
declare -a args
IFS=$'\n' read -r -d '' -a args < <(echo "$@" | tr " " "\n" && printf '\0')
for arg in "${args[@]}"; do
if ! grep -q ":" <<<"$arg"; then
arg="$arg::"
fi
declare -a config
IFS=$'\n' read -r -d '' -a config < <(echo "$arg" | tr ":" '\n')
plugin="${config[0]}"
rev="${config[1]}"
ref="${config[2]}"
lookup "$TMP" "$plugin" "$rev" "$ref" &
done
wait
}
_cleanup_tmp() {
\rm -rf "$TMP"
}
generate_file() {
declare -a args
IFS=$'\n' read -r -d '' -a args < <(ls "$TMP" && printf '\0')
OUTPUT=""
# shellcheck disable=2045
for arg in "${args[@]}"; do
OUTPUT="$(cat "$TMP/$arg"),$OUTPUT"
done
OUTPUT="[$OUTPUT]"
# shellcheck disable=2001
OUTPUT=$(echo "$OUTPUT" | sed -e "s_\},]_}]_g")
echo "$OUTPUT" | jq >versions.json
_cleanup_tmp
}
fromargs() {
declare -a args
IFS=$'\n' read -r -d '' -a args < <(echo "$@" && printf '\0')
download "${args[@]}" && generate_file
}
fromfile() {
local _file
_file="$1"
local args
args=$(jq '.[] | {name,rev,ref} | join(":")' versions.json | sed 's/\"//g')
fromargs "$args"
}
TMP="$(mktemp -d)"
# REVIEW: example
# fromfile versions.json
"$@"