-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·121 lines (99 loc) · 3.4 KB
/
install.sh
File metadata and controls
executable file
·121 lines (99 loc) · 3.4 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
#!/bin/bash
#
# A script to install the latest release of dbdump from GitHub.
#
# Usage:
# curl -sSL https://raw.githubusercontent.com/manydesigns/dbdump/main/install.sh | bash
#
# This script will:
# 1. Detect the user's OS and architecture.
# 2. Fetch the latest release from GitHub.
# 3. Download the correct release asset.
# 4. Unpack the binary and move it to /usr/local/bin.
# 5. Make the binary executable.
set -e
# --- Configuration ---
GITHUB_REPO="ManyDesigns/dbdump"
BINARY_NAME="dbdump"
INSTALL_DIR="/usr/local/bin"
# --- Helper Functions ---
# Function to print informational messages.
msg() {
echo -e "\033[32mINFO:\033[0m $1"
}
# Function to print error messages and exit.
err() {
echo -e "\033[31mERROR:\033[0m $1" >&2
exit 1
}
# Check for required tools before starting.
check_dependencies() {
for cmd in curl tar gzip; do
if ! command -v "$cmd" &>/dev/null; then
err "'$cmd' is not installed, but is required. Please install it and try again."
fi
done
}
# Detect the operating system and architecture.
detect_os_and_arch() {
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$OS" in
linux) OS="linux" ;;
darwin) OS="darwin" ;;
*) err "Unsupported operating system: $OS" ;;
esac
case "$ARCH" in
x86_64 | amd64) ARCH="amd64" ;;
aarch64 | arm64) ARCH="arm64" ;;
*) err "Unsupported architecture: $ARCH" ;;
esac
}
# Fetch the latest release version from the GitHub API.
get_latest_release_version() {
msg "Fetching the latest release version..."
local api_url="https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
# Use curl with grep and sed to extract the tag name, avoiding a dependency on jq.
VERSION=$(curl -s "$api_url" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$VERSION" ]; then
err "Could not fetch the latest release version. Please check that the GITHUB_REPO variable is set correctly."
fi
msg "The latest version is $VERSION"
}
# Download and install the binary.
download_and_install() {
local asset_filename="${BINARY_NAME}-${OS}-${ARCH}.tar.gz"
local download_url="https://github.com/${GITHUB_REPO}/releases/download/${VERSION}/${asset_filename}"
local tmp_dir=$(mktemp -d)
# Ensure the temporary directory is cleaned up on exit.
trap 'rm -rf "$tmp_dir"' EXIT
msg "Downloading from $download_url"
if ! curl -L "$download_url" -o "${tmp_dir}/${asset_filename}"; then
err "Failed to download the release asset. Please check the URL and your network connection."
fi
msg "Extracting the binary..."
tar -xzf "${tmp_dir}/${asset_filename}" -C "$tmp_dir"
msg "Installing '${BINARY_NAME}' to '${INSTALL_DIR}'..."
if [ -w "$INSTALL_DIR" ]; then
mv "${tmp_dir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
else
msg "Write permissions are required for ${INSTALL_DIR}. Using sudo..."
sudo mv "${tmp_dir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
fi
msg "'${BINARY_NAME}' has been installed successfully!"
msg "You can now run '${BINARY_NAME}' from your terminal."
}
# --- Main Logic ---
main() {
if [ "$GITHUB_REPO" == "YOUR_USER/YOUR_REPO" ]; then
err "Please edit the script and set the GITHUB_REPO variable to your repository."
fi
check_dependencies
detect_os_and_arch
get_latest_release_version
download_and_install
}
# --- Run the Script ---
main