1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-08-15 20:13:16 +02:00

Converts to sh for compatibility.

This commit is contained in:
kallookoo
2025-04-30 18:37:17 +02:00
parent 5961bf000d
commit 658c94a2d1

View File

@@ -1,10 +1,10 @@
#!/usr/bin/env bash #!/bin/sh
# This script will install the latest version of Mailpit. # This script will install the latest version of Mailpit.
# Check dependencias is installed # Check dependencias is installed
for cmd in curl tar; do for cmd in curl tar; do
if ! command -v "$cmd" &>/dev/null; then if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Then $cmd command is required but not installed." echo "Then $cmd command is required but not installed."
echo "Please install $cmd and try again." echo "Please install $cmd and try again."
exit 1 exit 1
@@ -42,58 +42,75 @@ esac
GH_REPO="axllent/mailpit" GH_REPO="axllent/mailpit"
INSTALL_PATH="/usr/local/bin" INSTALL_PATH="/usr/local/bin"
TIMEOUT=90 # --max-time in curl TIMEOUT=90
# The arguments in extended format for the curl command.
CURL_ARGS=(
"--silent"
"--fail"
"--location"
"--max-time" "$TIMEOUT"
)
# This is used to authenticate with the GitHub API. (Fix the public rate limiting issue) # This is used to authenticate with the GitHub API. (Fix the public rate limiting issue)
AUTH_TOKEN="${GITHUB_TOKEN:-}" # Try the GITHUB_TOKEN environment variable is set globally.
GITHUB_API_TOKEN="${GITHUB_TOKEN:-}"
# Update the default values if the user has set. # Update the default values if the user has set.
while [[ $# -gt 0 ]]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--install-path) --install-path)
shift shift
INSTALL_PATH="$1" case "$1" in
*/*)
# Remove trailing slashes from the path.
INSTALL_PATH="$(echo "$1" | sed 's#/\+$##')"
[ -z "$INSTALL_PATH" ] && INSTALL_PATH="/"
;;
esac
;; ;;
--auth | --auth-token | --github-token | --token) --auth | --auth-token | --github-token | --token)
shift shift
[[ "${1:-}" =~ ^- ]] || AUTH_TOKEN="$1" case "$1" in
gh*)
GITHUB_API_TOKEN="$1"
;;
esac
;; ;;
*) ;; *) ;;
esac esac
shift shift
done done
# Set the header auth if the user has set a GitHub token. # Description of the sort parameters for curl command.
if [[ -n "$AUTH_TOKEN" ]] && [[ "$AUTH_TOKEN" =~ ^gh[pousr]_[A-Za-z0-9_]{36,251}$ ]]; then # -s: Silent mode.
CURL_ARGS+=("--header" "Authorization: 'Bearer $AUTH_TOKEN'") # -f: Fail silently on server errors.
# -L: Follow redirects.
# -m: Set maximum time allowed for the transfer.
if [ -n "$GITHUB_API_TOKEN" ] && [ "${#GITHUB_API_TOKEN}" -gt 36 ]; then
CURL_OUTPUT="$(curl -sfL -m $TIMEOUT -H "Authorization: Bearer $GITHUB_API_TOKEN" https://api.github.com/repos/${GH_REPO}/releases/latest)"
EXIT_CODE=$?
else
CURL_OUTPUT="$(curl --sfL -m $TIMEOUT https://api.github.com/repos/${GH_REPO}/releases/latest)"
EXIT_CODE=$?
fi fi
CURL_OUTPUT=$(curl "${CURL_ARGS[@]}" "https://api.github.com/repos/${GH_REPO}/releases/latest") VERSION=""
EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then
if [[ $EXIT_CODE -eq 0 ]]; then
# Extracts the latest version using jq, awk, or sed. # Extracts the latest version using jq, awk, or sed.
if command -v jq &>/dev/null; then if command -v jq >/dev/null 2>&1; then
VERSION=$(echo "$CURL_OUTPUT" | jq -r '.tag_name') # Use jq -n because the output is not a valid JSON in sh.
elif command -v awk &>/dev/null; then VERSION=$(jq -n "$CURL_OUTPUT" | jq -r '.tag_name')
elif command -v awk >/dev/null 2>&1; then
VERSION=$(echo "$CURL_OUTPUT" | awk -F: '$1 ~ /tag_name/ {gsub(/[^v0-9\.]+/, "", $2) ;print $2; exit}') VERSION=$(echo "$CURL_OUTPUT" | awk -F: '$1 ~ /tag_name/ {gsub(/[^v0-9\.]+/, "", $2) ;print $2; exit}')
else elif command -v sed >/dev/null 2>&1; then
VERSION=$(echo "$CURL_OUTPUT" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p') VERSION=$(echo "$CURL_OUTPUT" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p')
else
EXIT_CODE=3
fi fi
fi fi
# Validate the version. # Validate the version.
if ! [[ "$VERSION" =~ ^v[0-9]{1,}[0-9\.]+$ ]]; then case "$VERSION" in
v[0-9][0-9\.]*) ;;
*)
echo "There was an error trying to check what is the latest version of Mailpit." echo "There was an error trying to check what is the latest version of Mailpit."
echo "Please try again later." echo "Please try again later."
exit $EXIT_CODE exit $EXIT_CODE
fi ;;
esac
TEMP_DIR="$(mktemp -qd)" TEMP_DIR="$(mktemp -qd)"
EXIT_CODE=$? EXIT_CODE=$?
@@ -104,33 +121,64 @@ if [ -z "$TEMP_DIR" ] || [ ! -d "$TEMP_DIR" ]; then
fi fi
GH_REPO_BIN="mailpit-${OS}-${OS_ARCH}.tar.gz" GH_REPO_BIN="mailpit-${OS}-${OS_ARCH}.tar.gz"
CURL_ARGS+=("--output" "${GH_REPO_BIN}") if [ "$INSTALL_PATH" = "/" ]; then
INSTALL_BIN_PATH="/mailpit"
if ! cd "$TEMP_DIR"; then
EXIT_CODE=$?
echo "ERROR: Changing to temporary directory."
else else
INSTALL_BIN_PATH="${INSTALL_PATH}/mailpit"
fi
cd "$TEMP_DIR" || EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
# Download the latest release. # Download the latest release.
curl "${CURL_ARGS[@]}" "https://github.com/${GH_REPO}/releases/download/${VERSION}/${GH_REPO_BIN}" #
# Description of the sort parameters for curl command.
# -s: Silent mode.
# -f: Fail silently on server errors.
# -L: Follow redirects.
# -m: Set maximum time allowed for the transfer.
# -o: Write output to a file instead of stdout.
curl -sfL -m $TIMEOUT -o "${GH_REPO_BIN}" "https://github.com/${GH_REPO}/releases/download/${VERSION}/${GH_REPO_BIN}"
EXIT_CODE=$? EXIT_CODE=$?
# The following conditions check each step of the installation. # The following conditions check each step of the installation.
# If there is an error in any of the steps, an error message is printed. # If there is an error in any of the steps, an error message is printed.
if ! [[ -f "${GH_REPO_BIN}" ]]; then if [ $EXIT_CODE -eq 0 ]; then
echo "ERROR: Downloading latest release." if ! [ -f "${GH_REPO_BIN}" ]; then
elif ! tar zxf "$GH_REPO_BIN"; then EXIT_CODE=1
echo "ERROR: Downloading latest release."
fi
fi
if [ $EXIT_CODE -eq 0 ]; then
tar zxf "$GH_REPO_BIN"
EXIT_CODE=$? EXIT_CODE=$?
echo "ERROR: Extracting \"${GH_REPO_BIN}\"." if [ $EXIT_CODE -ne 0 ]; then
elif ! mkdir -p "${INSTALL_PATH}"; then echo "ERROR: Extracting \"${GH_REPO_BIN}\"."
fi
fi
if [ $EXIT_CODE -eq 0 ] && [ ! -d "$INSTALL_PATH" ]; then
mkdir -p "${INSTALL_PATH}"
EXIT_CODE=$? EXIT_CODE=$?
echo "ERROR: Creating \"${INSTALL_PATH}\" directory." if [ $EXIT_CODE -ne 0 ]; then
elif ! cp -f mailpit "${INSTALL_PATH}"; then echo "ERROR: Creating \"${INSTALL_PATH}\" directory."
fi
fi
if [ $EXIT_CODE -eq 0 ]; then
cp mailpit "$INSTALL_BIN_PATH"
EXIT_CODE=$? EXIT_CODE=$?
echo "ERROR: Copying mailpit to \"${INSTALL_PATH}\" directory." if [ $EXIT_CODE -ne 0 ]; then
elif ! chmod 755 "${INSTALL_PATH}/mailpit"; then echo "ERROR: Copying mailpit to \"${INSTALL_PATH}\" directory."
fi
fi
if [ $EXIT_CODE -eq 0 ]; then
chmod 755 "$INSTALL_BIN_PATH"
EXIT_CODE=$? EXIT_CODE=$?
echo "ERROR: Setting permissions for \"${INSTALL_PATH}/mailpit\" binary." if [ $EXIT_CODE -ne 0 ]; then
echo "ERROR: Setting permissions for \"$INSTALL_BIN_PATH\" binary."
fi
fi fi
# Set the owner and group to root:root if the script is run as root. # Set the owner and group to root:root if the script is run as root.
@@ -143,20 +191,25 @@ else
*) ;; *) ;;
esac esac
if ! chown "${OWNER}:${GROUP}" "${INSTALL_PATH}/mailpit"; then chown "${OWNER}:${GROUP}" "$INSTALL_BIN_PATH"
EXIT_CODE=$? EXIT_CODE=$?
echo "ERROR: Setting ownership for \"${INSTALL_PATH}/mailpit\" binary." if [ $EXIT_CODE -ne 0 ]; then
echo "ERROR: Setting ownership for \"$INSTALL_BIN_PATH\" binary."
fi fi
fi fi
else
echo "ERROR: Changing to temporary directory."
exit $EXIT_CODE
fi fi
# Cleanup the temporary directory. # Cleanup the temporary directory.
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
# Check the EXIT_CODE variable, and print the success or error message. # Check the EXIT_CODE variable, and print the success or error message.
if [[ $EXIT_CODE -eq 0 ]]; then if [ $EXIT_CODE -ne 0 ]; then
echo "Installed successfully to \"${INSTALL_PATH}/mailpit\""
else
echo "There was an error installing Mailpit." echo "There was an error installing Mailpit."
echo "Please try again later." echo "Please try again later."
exit $EXIT_CODE
fi fi
exit $EXIT_CODE
echo "Installed successfully to \"$INSTALL_BIN_PATH\"."
exit 0