-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·195 lines (166 loc) · 5.37 KB
/
install.sh
File metadata and controls
executable file
·195 lines (166 loc) · 5.37 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
#!/bin/bash
#
# Utility script to download git-drs from GitHub releases:
# https://github.com/calypr/git-drs/releases/latest
set -e
REPO="calypr/git-drs"
echo "Installing $REPO"
# GitHub API curl wrapper
fetch_github_api() {
_url="$1"
if [ -n "$GITHUB_TOKEN" ]; then
curl -LsS -H "Authorization: token $GITHUB_TOKEN" "$_url"
else
curl -LsS "$_url"
fi
}
# Latest Release URL
get_latest_release_url() {
echo "https://api.github.com/repos/$REPO/releases/latest"
}
# Tag URL
get_tag_release_url() {
echo "https://api.github.com/repos/$REPO/releases/tags/$1"
}
# Optional tag
VERSION_TAG=$1
# Get release URL
RELEASE_URL=""
if [ -z "$VERSION_TAG" ]; then
RELEASE_URL=$(get_latest_release_url)
echo "Fetching latest release information..."
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required but not installed."
exit 1
fi
# API call to get latest tag
RESPONSE=$(fetch_github_api "$RELEASE_URL" || true)
# Check if we got a valid JSON with tag_name
if echo "$RESPONSE" | grep -q '"tag_name":'; then
VERSION_TAG=$(echo "$RESPONSE" | grep '"tag_name":' | head -n 1 | cut -d '"' -f4)
else
echo "Error: Failed to fetch the latest release version. You might be rate-limited by the GitHub API."
if [ -z "$GITHUB_TOKEN" ]; then
echo "Try setting GITHUB_TOKEN as an environment variable."
fi
# If we can't get it from API, it's a fatal error for this mode
exit 1
fi
echo "No version specified, fetching the latest release ($VERSION_TAG)"
else
RELEASE_URL=$(get_tag_release_url "$VERSION_TAG")
# Verify the tag exists
RESPONSE=$(fetch_github_api "$RELEASE_URL" || true)
if ! echo "$RESPONSE" | grep -q '"tag_name":'; then
echo "Error: Release tag '$VERSION_TAG' not found."
exit 1
fi
fi
# Remove 'v' prefix
VERSION_NUMBER="${VERSION_TAG#v}"
# Determine OS and Architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
ARCH="arm64"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
# Define the filenames
CHECKSUM_FILE="git-drs_${VERSION_NUMBER}_checksums.txt"
# Fetch asset URLs
echo "Fetching download links..."
# RESPONSE is still from the last API call
ASSETS=$(echo "$RESPONSE" | grep "browser_download_url" | cut -d '"' -f 4)
# Download executable + checksums
echo "Downloading git-drs $VERSION_TAG for $OS $ARCH"
DOWNLOADED_TAR=false
DOWNLOADED_CHK=false
TAR_NAME=""
for asset in $ASSETS; do
# Check for OS-ARCH and tar.gz
if echo "$asset" | grep -q "${OS}-${ARCH}" && echo "$asset" | grep -q ".tar.gz"; then
TAR_URL="$asset"
TAR_NAME=$(basename "$asset")
echo "Downloading $TAR_NAME..."
curl -LsS -O "$TAR_URL"
DOWNLOADED_TAR=true
# Check for checksums file
elif echo "$asset" | grep -q "$CHECKSUM_FILE"; then
echo "Downloading $CHECKSUM_FILE..."
curl -LsS -O "$asset"
DOWNLOADED_CHK=true
fi
done
if [ "$DOWNLOADED_TAR" = false ]; then
echo "Error: Could not find a release asset for $OS $ARCH"
exit 1
fi
if [ "$DOWNLOADED_CHK" = true ]; then
echo "Verifying checksum"
CHECKSUM_EXPECTED=$(grep "$TAR_NAME" "$CHECKSUM_FILE" | awk '{print $1}' || true)
# Checksum utility
if command -v sha256sum >/dev/null 2>&1; then
CHECKSUM_ACTUAL=$(sha256sum "$TAR_NAME" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
CHECKSUM_ACTUAL=$(shasum -a 256 "$TAR_NAME" | awk '{print $1}')
else
echo "Warning: No SHA256 checksum utility found (sha256sum or shasum). Skipping verification."
CHECKSUM_ACTUAL="$CHECKSUM_EXPECTED"
fi
if [ -n "$CHECKSUM_EXPECTED" ] && [ "$CHECKSUM_EXPECTED" != "$CHECKSUM_ACTUAL" ]; then
echo "Error: Checksum verification failed for $TAR_NAME."
exit 1
fi
else
echo "Warning: Checksum file not found. Skipping verification."
fi
# Extract + Install
tar -xzf "$TAR_NAME"
DEST=$2
if [ -z "$DEST" ]; then
DEST="$HOME/.local/bin"
fi
echo "Installing to $DEST"
mkdir -p "$DEST"
mv git-drs "$DEST/"
# Update PATH in GitHub Actions
if [ -n "$GITHUB_PATH" ]; then
echo "Updating GITHUB_PATH..."
echo "$DEST" >> "$GITHUB_PATH"
fi
# Check PATH and update shell config if not in PATH
if ! command -v git-drs >/dev/null 2>&1; then
# Check if DEST is already in PATH
if ! echo ":$PATH:" | grep -q ":$DEST:"; then
echo "Adding $DEST to PATH in shell configuration"
# Determine shell config file
if [ -n "$ZSH_VERSION" ]; then
SHELL_CONFIG="$HOME/.zshrc"
elif [ -n "$BASH_VERSION" ]; then
SHELL_CONFIG="$HOME/.bashrc"
else
# Default to .bashrc or .profile
if [ -f "$HOME/.bashrc" ]; then
SHELL_CONFIG="$HOME/.bashrc"
else
SHELL_CONFIG="$HOME/.profile"
fi
fi
echo "export PATH=\$PATH:\"$DEST\"" >> "$SHELL_CONFIG"
echo "Run 'source $SHELL_CONFIG' to update your current shell session."
fi
fi
echo "Installation successful!"
echo ""
echo "Run 'git-drs --help' for more info"
# Clean up
if [ -n "$TAR_NAME" ] && [ -f "$TAR_NAME" ]; then
rm -f "$TAR_NAME"
fi
if [ -f "$CHECKSUM_FILE" ]; then
rm -f "$CHECKSUM_FILE"
fi