Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
178 lines (156 loc) · 4.18 KB
/
Copy pathinstall.sh
File metadata and controls
178 lines (156 loc) · 4.18 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env sh
set -eu
REPO="${REPO:-MotiaDev/motia-cli}"
BIN_NAME="${BIN_NAME:-motia-cli}"
VERSION="${VERSION:-${1:-}}"
err() {
echo "error: $*" >&2
exit 1
}
if ! command -v curl >/dev/null 2>&1; then
err "curl is required"
fi
if [ -n "${TARGET:-}" ]; then
target="$TARGET"
else
uname_s=$(uname -s 2>/dev/null || echo unknown)
uname_m=$(uname -m 2>/dev/null || echo unknown)
case "$uname_m" in
x86_64|amd64)
arch="x86_64"
;;
arm64|aarch64)
arch="aarch64"
;;
armv7*)
arch="armv7"
;;
*)
err "unsupported architecture: $uname_m"
;;
esac
case "$uname_s" in
Darwin)
os="apple-darwin"
;;
Linux)
case "$arch" in
x86_64)
if [ -n "${MOTIA_USE_GLIBC:-}" ]; then
sys_glibc=$(ldd --version 2>&1 | head -n 1 | grep -oE '[0-9]+\.[0-9]+$' || echo "0.0")
required_glibc="2.35"
if printf '%s\n%s\n' "$required_glibc" "$sys_glibc" | sort -V -C; then
os="unknown-linux-gnu"
echo "using glibc build (system glibc: $sys_glibc)"
else
echo "warning: system glibc $sys_glibc is older than required $required_glibc, falling back to musl" >&2
os="unknown-linux-musl"
fi
else
os="unknown-linux-musl"
fi
;;
aarch64)
os="unknown-linux-gnu"
;;
armv7)
os="unknown-linux-gnueabihf"
;;
esac
;;
*)
err "unsupported OS: $uname_s"
;;
esac
target="$arch-$os"
fi
api_headers="-H Accept:application/vnd.github+json -H X-GitHub-Api-Version:2022-11-28"
github_api() {
curl -fsSL $api_headers "$1"
}
if [ -n "$VERSION" ]; then
echo "installing version: $VERSION"
api_url="https://api.github.com/repos/$REPO/releases/tags/$VERSION"
json=$(github_api "$api_url") || {
if [ "${VERSION#v}" = "$VERSION" ]; then
api_url="https://api.github.com/repos/$REPO/releases/tags/v$VERSION"
json=$(github_api "$api_url") || err "release tag not found: $VERSION"
else
err "release tag not found: $VERSION"
fi
}
else
echo "installing latest version"
api_url="https://api.github.com/repos/$REPO/releases/latest"
json=$(github_api "$api_url")
fi
if command -v jq >/dev/null 2>&1; then
asset_url=$(printf '%s' "$json" \
| jq -r --arg target "$target" '.assets[] | select(.name | test($target)) | .browser_download_url' \
| head -n 1)
else
asset_url=$(printf '%s' "$json" \
| grep -oE '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]+"' \
| sed -E 's/.*"([^"]+)".*/\1/' \
| grep "$target" \
| head -n 1)
fi
if [ -z "$asset_url" ]; then
echo "available assets:" >&2
printf '%s' "$json" \
| grep -oE '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]+"' \
| sed -E 's/.*"([^"]+)".*/\1/' >&2
err "no release asset found for target: $target"
fi
asset_name=$(basename "$asset_url")
if [ -z "${BIN_DIR:-}" ]; then
if [ -n "${PREFIX:-}" ]; then
bin_dir="$PREFIX/bin"
else
bin_dir="$HOME/.local/bin"
fi
else
bin_dir="$BIN_DIR"
fi
mkdir -p "$bin_dir"
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t motia-cli-install)
cleanup() {
rm -rf "$tmpdir"
}
trap cleanup EXIT INT TERM
curl -fsSL -L "$asset_url" -o "$tmpdir/$asset_name"
case "$asset_name" in
*.tar.gz|*.tgz)
tar -xzf "$tmpdir/$asset_name" -C "$tmpdir"
;;
*.zip)
if ! command -v unzip >/dev/null 2>&1; then
err "unzip is required to extract $asset_name"
fi
unzip -q "$tmpdir/$asset_name" -d "$tmpdir"
;;
*)
;;
esac
if [ -f "$tmpdir/$BIN_NAME" ]; then
bin_file="$tmpdir/$BIN_NAME"
else
bin_file=$(find "$tmpdir" -type f \( -name "$BIN_NAME" -o -name "${BIN_NAME}.exe" \) | head -n 1)
fi
if [ -z "${bin_file:-}" ] || [ ! -f "$bin_file" ]; then
err "binary not found in downloaded asset"
fi
if command -v install >/dev/null 2>&1; then
install -m 755 "$bin_file" "$bin_dir/$BIN_NAME"
else
cp "$bin_file" "$bin_dir/$BIN_NAME"
chmod 755 "$bin_dir/$BIN_NAME"
fi
printf 'installed %s to %s\n' "$BIN_NAME" "$bin_dir/$BIN_NAME"
case ":$PATH:" in
*":$bin_dir:"*)
;;
*)
printf 'add %s to your PATH if needed\n' "$bin_dir"
;;
esac