-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·153 lines (134 loc) · 4.45 KB
/
install.sh
File metadata and controls
executable file
·153 lines (134 loc) · 4.45 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
#!/bin/sh
#
# This script downloads and installs the latest binary release of gitx.
# It detects the user's OS and architecture to download the correct binary.
#
# Usage:
# curl -sSL https://raw.githubusercontent.com/gitxtui/gitx/master/install.sh | bash
set -e
# The GitHub repository in the format "owner/repo".
REPO="gitxtui/gitx"
INSTALL_DIR="/usr/local/bin"
# Get the operating system.
get_os() {
case "$(uname -s)" in
Linux*) OS='Linux';;
Darwin*) OS='Darwin';;
*)
echo "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
echo "$OS"
}
# Get the architecture.
get_arch() {
case "$(uname -m)" in
x86_64|amd64) ARCH='x86_64';;
aarch64|arm64) ARCH='arm64';;
i386|i686) ARCH='i386';;
*)
echo "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
echo "$ARCH"
}
# Get the latest release tag from the GitHub API.
get_latest_release() {
API_URL="https://api.github.com/repos/$REPO/releases/latest"
# Use python if available, otherwise fall back to perl.
if command -v python3 >/dev/null 2>&1; then
curl -sL "$API_URL" | python3 -c "import sys, json; print(json.load(sys.stdin)['tag_name'])"
elif command -v python >/dev/null 2>&1; then
curl -sL "$API_URL" | python -c "import sys, json; print(json.load(sys.stdin)['tag_name'])"
elif command -v perl >/dev/null 2>&1; then
curl -sL "$API_URL" | perl -ne 'if (/\"tag_name\":\s*\"([^\"]+)\"/) { print $1; exit }'
else
# Fallback to grep/sed for systems without python/perl
curl -sL "$API_URL" |
grep '"tag_name":' |
head -n 1 |
sed -E 's/.*"([^"]+)".*/\1/'
fi
}
main() {
OS=$(get_os)
ARCH=$(get_arch)
VERSION=$(get_latest_release)
if [ -z "$VERSION" ]; then
echo "Error: Could not find any release version for $REPO."
echo "Please check that the repository has releases and that you are not being rate-limited by the GitHub API."
exit 1
fi
# Construct the archive filename and download URL.
FILENAME="gitx_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="https://github.com/$REPO/releases/download/${VERSION}/${FILENAME}"
# Download and extract the binary.
echo "Downloading gitx ${VERSION} for ${OS}/${ARCH}..."
TEMP_DIR=$(mktemp -d)
# Download to a temporary directory with error checking.
if ! curl -sSL -f -o "$TEMP_DIR/$FILENAME" "$DOWNLOAD_URL"; then
echo "Error: Failed to download gitx from $DOWNLOAD_URL"
echo "Please check that the release exists and try again."
rm -rf "$TEMP_DIR"
exit 1
fi
echo "Installing gitx to ${INSTALL_DIR}..."
# Extract the archive.
if ! tar -xzf "$TEMP_DIR/$FILENAME" -C "$TEMP_DIR"; then
echo "Error: Failed to extract archive. The downloaded file may be corrupted."
rm -rf "$TEMP_DIR"
exit 1
fi
# Ensure the installation directory exists.
if [ ! -d "$INSTALL_DIR" ]; then
echo "Creating ${INSTALL_DIR}..."
if sudo mkdir -p "$INSTALL_DIR" 2>/dev/null; then
echo "Directory created successfully."
else
echo "Warning: Could not create ${INSTALL_DIR}."
# Fall back to user's local bin directory.
INSTALL_DIR="$HOME/.local/bin"
echo "Falling back to ${INSTALL_DIR}..."
mkdir -p "$INSTALL_DIR"
fi
fi
# Move the binary to the installation directory.
# Use sudo if the directory is not writable by the current user.
if [ -w "$INSTALL_DIR" ]; then
mv "$TEMP_DIR/gitx" "${INSTALL_DIR}/gitx"
else
echo "Root permission is required to install gitx to ${INSTALL_DIR}"
if ! sudo mv "$TEMP_DIR/gitx" "${INSTALL_DIR}/gitx" 2>/dev/null; then
echo "Warning: Could not install to ${INSTALL_DIR}."
# Fall back to user's local bin directory.
INSTALL_DIR="$HOME/.local/bin"
echo "Falling back to ${INSTALL_DIR}..."
mkdir -p "$INSTALL_DIR"
mv "$TEMP_DIR/gitx" "${INSTALL_DIR}/gitx"
fi
fi
# Make the binary executable.
chmod +x "${INSTALL_DIR}/gitx"
# Clean up the temporary directory.
rm -rf "$TEMP_DIR"
echo ""
echo "gitx has been installed successfully to ${INSTALL_DIR}!"
# Check if the install directory is in PATH.
case ":$PATH:" in
*":${INSTALL_DIR}:"*)
echo "Run 'gitx' to get started."
;;
*)
echo "Note: ${INSTALL_DIR} is not in your PATH."
echo "Add it to your PATH by running:"
echo " echo 'export PATH=\"${INSTALL_DIR}:\$PATH\"' >> ~/.zshrc"
echo " source ~/.zshrc"
echo ""
echo "Or run gitx directly: ${INSTALL_DIR}/gitx"
;;
esac
}
# Run the main function.
main