-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinstall-synkronus-cli.sh
More file actions
215 lines (187 loc) · 4.74 KB
/
Copy pathinstall-synkronus-cli.sh
File metadata and controls
215 lines (187 loc) · 4.74 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bash
set -euo pipefail
VERSION="latest"
OWNER="OpenDataEnsemble"
REPO="ode"
ASSET_NAME="synkronus-cli-darwin-arm64"
BINARY_NAME="synk"
COMMAND_NAME="synk"
INSTALL_DIR="${HOME}/.local/bin"
ZSH_COMPLETIONS_DIR="${HOME}/.zsh/completions"
usage() {
cat <<EOF
Usage: install-synkronus-cli.sh [options]
Options:
--version <tag|latest> Release tag to install (default: latest)
--owner <owner> GitHub owner/org (default: OpenDataEnsemble)
--repo <repo> GitHub repo (default: ode)
--asset <name> Release asset name (default: synkronus-cli_Darwin_arm64.tar.gz)
--install-dir <dir> Install directory (default: ~/.local/bin)
--help Show this help
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
VERSION="$2"
shift 2
;;
--owner)
OWNER="$2"
shift 2
;;
--repo)
REPO="$2"
shift 2
;;
--asset)
ASSET_NAME="$2"
shift 2
;;
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
step() {
echo "==> $1"
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Required command not found: $1" >&2
exit 1
}
}
get_download_url() {
if [[ "$VERSION" == "latest" ]]; then
echo "https://github.com/$OWNER/$REPO/releases/latest/download/$ASSET_NAME"
else
echo "https://github.com/$OWNER/$REPO/releases/download/$VERSION/$ASSET_NAME"
fi
}
ensure_install_dir_on_path_in_zshrc() {
local zshrc="$HOME/.zshrc"
local marker_start="# >>> synkronus-cli path >>>"
local marker_end="# <<< synkronus-cli path <<<"
mkdir -p "$(dirname "$zshrc")"
touch "$zshrc"
local block
block=$(cat <<EOF
$marker_start
if [[ ":\$PATH:" != *":$INSTALL_DIR:"* ]]; then
export PATH="$INSTALL_DIR:\$PATH"
fi
$marker_end
EOF
)
if grep -Fq "$marker_start" "$zshrc"; then
python3 - "$zshrc" "$marker_start" "$marker_end" "$block" <<'PY'
import re
import sys
path, start, end, block = sys.argv[1:]
with open(path, "r", encoding="utf-8") as f:
content = f.read()
pattern = re.escape(start) + r".*?" + re.escape(end)
updated = re.sub(pattern, block, content, flags=re.S)
with open(path, "w", encoding="utf-8") as f:
f.write(updated)
PY
echo "Updated PATH block in $zshrc"
else
{
[[ -s "$zshrc" ]] && tail -c 1 "$zshrc" | read -r _ || true
echo
echo "$block"
} >> "$zshrc"
echo "Added PATH block to $zshrc"
fi
}
setup_zsh_completion() {
local zshrc="$HOME/.zshrc"
local completion_file="$ZSH_COMPLETIONS_DIR/_${COMMAND_NAME}"
local marker_start="# >>> synkronus-cli completion >>>"
local marker_end="# <<< synkronus-cli completion <<<"
mkdir -p "$ZSH_COMPLETIONS_DIR"
touch "$zshrc"
step "Generating zsh completion"
"$INSTALL_DIR/$BINARY_NAME" completion zsh > "$completion_file"
local block
block=$(cat <<EOF
$marker_start
fpath=("$ZSH_COMPLETIONS_DIR" \$fpath)
autoload -Uz compinit
compinit
$marker_end
EOF
)
if grep -Fq "$marker_start" "$zshrc"; then
python3 - "$zshrc" "$marker_start" "$marker_end" "$block" <<'PY'
import re
import sys
path, start, end, block = sys.argv[1:]
with open(path, "r", encoding="utf-8") as f:
content = f.read()
pattern = re.escape(start) + r".*?" + re.escape(end)
updated = re.sub(pattern, block, content, flags=re.S)
with open(path, "w", encoding="utf-8") as f:
f.write(updated)
PY
echo "Updated completion block in $zshrc"
else
{
[[ -s "$zshrc" ]] && tail -c 1 "$zshrc" | read -r _ || true
echo
echo "$block"
} >> "$zshrc"
echo "Added completion block to $zshrc"
fi
echo "Installed zsh completion to $completion_file"
}
need_cmd curl
need_cmd tar
need_cmd mktemp
need_cmd python3
DOWNLOAD_URL="$(get_download_url)"
TMP_DIR="$(mktemp -d)"
ARCHIVE_PATH="$TMP_DIR/$ASSET_NAME"
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
step "Preparing install directory"
mkdir -p "$INSTALL_DIR"
step "Downloading $DOWNLOAD_URL"
curl -fsSL "$DOWNLOAD_URL" -o "$ARCHIVE_PATH"
step "Extracting archive"
tar -xzf "$ARCHIVE_PATH" -C "$TMP_DIR"
step "Finding executable"
BIN_PATH="$(find "$TMP_DIR" -type f -name "$BINARY_NAME" | head -n 1 || true)"
if [[ -z "$BIN_PATH" ]]; then
echo "Could not find $BINARY_NAME inside archive $ASSET_NAME" >&2
exit 1
fi
step "Installing $BINARY_NAME to $INSTALL_DIR"
install -m 0755 "$BIN_PATH" "$INSTALL_DIR/$BINARY_NAME"
step "Updating zsh PATH config"
ensure_install_dir_on_path_in_zshrc
step "Updating zsh completion config"
setup_zsh_completion
step "Done"
echo
echo "Installed: $INSTALL_DIR/$BINARY_NAME"
echo
echo "Open a new terminal, or run:"
echo " source ~/.zshrc"
echo
echo "Then try:"
echo " $COMMAND_NAME --help"