Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·190 lines (156 loc) · 5.86 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·190 lines (156 loc) · 5.86 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
#!/usr/bin/env bash
set -euo pipefail
readonly BLUE='\033[0;34m'
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly YELLOW='\033[1;33m'
readonly RESET='\033[0m'
readonly TMP_DIR=$(mktemp -d)
trap 'rm -rf "${TMP_DIR}"' EXIT INT TERM
log() {
local level=$1
shift
local color
case "$level" in
INFO) color="$GREEN" ;;
WARN) color="$YELLOW" ;;
ERROR) color="$RED" ;;
*) color="$BLUE" ;;
esac
echo -e "${color}[$(date +'%Y-%m-%dT%H:%M:%S')] ${level}: $*${RESET}" >&2
}
ensure_command() {
command -v "$1" >/dev/null 2>&1 || { log ERROR "Required command not found: $1"; exit 1; }
}
print_banner() {
echo -e "${BLUE}"
echo "██╗░░░██╗██████╗░██████╗░░█████╗░"
echo "██║░░░██║██╔══██╗██╔══██╗██╔══██╗"
echo "██║░░░██║██████╔╝██║░░██║██║░░██║"
echo "██║░░░██║██╔═══╝░██║░░██║██║░░██║"
echo "╚██████╔╝██║░░░░░██████╔╝╚█████╔╝"
echo "░╚═════╝░╚═╝░░░░░╚═════╝░░╚════╝░"
echo -e "${RESET}"
echo "Website Monitoring Tool - Installer"
echo
}
detect_platform() {
OS="$(uname -s)"
case "${OS}" in
Linux*) SYSTEM=Linux;;
Darwin*) SYSTEM=Darwin;;
MINGW*) SYSTEM=Windows;;
MSYS*) SYSTEM=Windows;;
*) log ERROR "Unsupported operating system: ${OS}"; exit 2;;
esac
ARCH="$(uname -m)"
case "${ARCH}" in
x86_64*) ARCH=x86_64;;
arm64*) ARCH=arm64;;
aarch64*) ARCH=arm64;;
*) log ERROR "Unsupported architecture: ${ARCH}"; exit 2;;
esac
log INFO "Detected system: ${SYSTEM}_${ARCH}"
}
get_latest_version() {
log INFO "Fetching latest release information"
LATEST_RELEASE_URL="https://api.github.com/repos/Owloops/updo/releases/latest"
RELEASE_DATA=$(curl -s -H "Accept: application/vnd.github.v3+json" "${LATEST_RELEASE_URL}")
VERSION=$(echo "${RELEASE_DATA}" | grep -o '"tag_name":[^,}]*' | sed 's/.*"tag_name"[": ]*//;s/[",]//g')
if [[ -z "${VERSION}" ]]; then
log ERROR "Failed to get latest version. Check your internet connection."
exit 1
fi
log INFO "Latest version: ${VERSION}"
}
get_download_url() {
local RELEASE_OS="${SYSTEM}"
local RELEASE_ARCH="${ARCH}"
ASSET_PATTERN="updo_${RELEASE_OS}_${RELEASE_ARCH}"
if [[ "${SYSTEM}" = "Windows" ]]; then
ASSET_PATTERN="${ASSET_PATTERN}.exe"
fi
DOWNLOAD_URL=$(echo "${RELEASE_DATA}" | grep -o "\"browser_download_url\": \"[^\"]*${ASSET_PATTERN}\"" | grep -o "https://[^\"]*")
if [[ -z "${DOWNLOAD_URL}" ]]; then
log ERROR "Could not find download URL for ${ASSET_PATTERN}"
echo "Available assets:" >&2
echo "${RELEASE_DATA}" | grep -o '"name": "[^"]*"' | grep -o '[^"]*$' | grep -v '^name$'
exit 1
fi
log INFO "Found download URL: ${DOWNLOAD_URL}"
}
download_binary() {
BINARY_NAME=$(basename "${DOWNLOAD_URL}")
BINARY_PATH="${TMP_DIR}/${BINARY_NAME}"
log INFO "Downloading binary from ${DOWNLOAD_URL}"
curl -SL -o "${BINARY_PATH}" "${DOWNLOAD_URL}"
chmod +x "${BINARY_PATH}"
}
install_binary() {
INSTALL_DIR="/usr/local/bin"
if [[ ! -d "${INSTALL_DIR}" ]] || [[ ! -w "${INSTALL_DIR}" ]]; then
INSTALL_DIR="${HOME}/.local/bin"
mkdir -p "${INSTALL_DIR}"
if [[ ":${PATH}:" != *":${INSTALL_DIR}:"* ]]; then
log INFO "Adding ${INSTALL_DIR} to your PATH"
SHELL_CONFIG=""
if [[ -f "${HOME}/.zshrc" ]] && [[ "${SHELL}" == */zsh ]]; then
SHELL_CONFIG="${HOME}/.zshrc"
elif [[ -f "${HOME}/.bashrc" ]]; then
SHELL_CONFIG="${HOME}/.bashrc"
fi
if [[ -n "${SHELL_CONFIG}" ]]; then
echo "export PATH=\"\$PATH:${INSTALL_DIR}\"" >> "${SHELL_CONFIG}"
log INFO "Added to ${SHELL_CONFIG}. Please restart your terminal or run 'source ${SHELL_CONFIG}'"
else
log WARN "Please add ${INSTALL_DIR} to your PATH manually."
fi
fi
fi
BINARY_PATH=$(find "${TMP_DIR}" -name "updo*" -type f | head -1)
if [[ -z "${BINARY_PATH}" ]]; then
log ERROR "Failed to find the updo binary in the download directory"
exit 1
fi
cp "${BINARY_PATH}" "${INSTALL_DIR}/updo"
chmod +x "${INSTALL_DIR}/updo"
log INFO "updo ${VERSION} has been successfully installed to ${INSTALL_DIR}/updo"
}
verify_installation() {
if command -v "${INSTALL_DIR}/updo" &>/dev/null; then
log INFO "Installed version:"
"${INSTALL_DIR}/updo" --version
fi
echo
echo "You can now run updo from the command line."
echo
echo -e "${BLUE}Get started:${RESET}"
echo " updo --help # Show help and usage information"
echo " updo --version # Show version information"
echo
echo -e "${BLUE}Enable shell completions:${RESET}"
echo " # For bash:"
echo " source <(updo completion bash)"
echo
echo " # For zsh:"
echo " source <(updo completion zsh)"
echo
echo " # For fish:"
echo " updo completion fish | source"
echo
echo -e "For full documentation, visit: ${GREEN}https://github.com/Owloops/updo#usage${RESET}"
log INFO "Thank you for installing updo!"
}
main() {
ensure_command curl
ensure_command grep
ensure_command sed
print_banner
detect_platform
get_latest_version
get_download_url
download_binary
install_binary
verify_installation
}
main "$@"