forked from pkgxdev/mash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmash
executable file
·84 lines (72 loc) · 1.87 KB
/
mash
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
#!/usr/bin/env bash
set -eo pipefail
if [ -n "$RUNNER_DEBUG" -a -n "$GITHUB_ACTIONS" ] || [ -n "$VERBOSE" ]; then
set -x
fi
if ! command -v pkgx >/dev/null; then
echo "error: pkgx not found" 1>&2
exit 1
fi
if ! command -v curl >/dev/null; then
curl() {
pkgx curl "$@"
}
fi
run() {
SCRIPTNAME=$1
shift
# github won’t give us `github.com/pkgx` because they are meanies
if [[ $SCRIPTNAME == u/pkgx/* ]]; then
SCRIPTNAME="${SCRIPTNAME/pkgx/pkgxdev}"
fi
if [ "$(uname)" = Darwin ]; then
CACHE="${XDG_CACHE_HOME:-$HOME/Library/Caches}/mash/$SCRIPTNAME"
else
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/mash/$SCRIPTNAME"
fi
get_etag() {
grep -i ETag "$CACHE/headers.txt" | sed -e 's/ETag: "\(.*\)"/\1/I' | tr -d '\r'
}
if [ -f "$CACHE/headers.txt" ] && ETAG=$(get_etag); then
ETAG=(--header If-None-Match:\ $ETAG)
else
mkdir -p "$CACHE"
fi
URL="https://pkgxdev.github.io/mash/$SCRIPTNAME"
if curl \
"${ETAG[@]}" \
--silent \
--fail \
--show-error \
--dump-header "$CACHE/headers.txt" \
--output "$CACHE/script" \
"$URL"
then
chmod +x "$CACHE/script"
exec "$CACHE/script" "$@"
elif [ -f "$CACHE/script" ]; then
echo "warn: couldn’t update check" 1>&2
exec "$CACHE/script" "$@"
else
echo "error: $URL" 1>&2
exit 2
fi
}
if [ -z "$1" ]; then
curl -Ssf https://pkgxdev.github.io/mash/ | pkgx jq '.categories[]' --raw-output | sort | uniq
exit 0
elif [[ "$1" == *"/"* ]]; then
# fully quaified name not a category
cmd=$1
shift
run u/$cmd "$@"
elif [[ -z "$2" ]]; then
curl -Ssf https://pkgxdev.github.io/mash/$1/ |
pkgx jq -r '.scripts[] | [.cmd, .description] | @tsv' |
awk 'BEGIN {FS="\t"; print "| Script | Description |"; print "|-|-|"} {printf("| %s | %s |\n", $1, $2)}' |
pkgx gum format
else
cmd=$1/$2
shift; shift
run $cmd "$@"
fi