|
1 |
| -#!/usr/bin/env bash |
| 1 | +#!/bin/sh |
2 | 2 |
|
3 |
| -GH_REPO="axllent/mailpit" |
4 |
| -TIMEOUT=90 |
| 3 | +# This script will install the latest release of Mailpit. |
5 | 4 |
|
6 |
| -set -e |
| 5 | +# Check dependencies is installed |
| 6 | +for cmd in curl tar; do |
| 7 | + if ! command -v "$cmd" >/dev/null 2>&1; then |
| 8 | + echo "Then $cmd command is required but not installed." |
| 9 | + echo "Please install $cmd and try again." |
| 10 | + exit 1 |
| 11 | + fi |
| 12 | +done |
7 | 13 |
|
8 |
| -VERSION=$(curl --silent --location --max-time "${TIMEOUT}" "https://api.github.com/repos/${GH_REPO}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') |
9 |
| -if [ $? -ne 0 ]; then |
10 |
| - echo -ne "\nThere was an error trying to check what is the latest version of Mailpit.\nPlease try again later.\n" |
11 |
| - exit 1 |
12 |
| -fi |
13 |
| - |
14 |
| -# detect the platform |
15 |
| -OS="$(uname)" |
16 |
| -case $OS in |
17 |
| -Linux) |
18 |
| - OS='linux' |
19 |
| - ;; |
20 |
| -FreeBSD) |
21 |
| - OS='freebsd' |
22 |
| - echo 'OS not supported' |
23 |
| - exit 2 |
24 |
| - ;; |
25 |
| -NetBSD) |
26 |
| - OS='netbsd' |
27 |
| - echo 'OS not supported' |
28 |
| - exit 2 |
29 |
| - ;; |
30 |
| -OpenBSD) |
31 |
| - OS='openbsd' |
32 |
| - echo 'OS not supported' |
33 |
| - exit 2 |
34 |
| - ;; |
35 |
| -Darwin) |
36 |
| - OS='darwin' |
37 |
| - ;; |
38 |
| -SunOS) |
39 |
| - OS='solaris' |
40 |
| - echo 'OS not supported' |
41 |
| - exit 2 |
42 |
| - ;; |
| 14 | +# Check if the OS is supported. |
| 15 | +OS= |
| 16 | +case "$(uname -s)" in |
| 17 | +Linux) OS="linux" ;; |
| 18 | +Darwin) OS="Darwin" ;; |
43 | 19 | *)
|
44 |
| - echo 'OS not supported' |
| 20 | + echo "OS not supported." |
45 | 21 | exit 2
|
46 | 22 | ;;
|
47 | 23 | esac
|
48 | 24 |
|
49 |
| -# detect the arch |
50 |
| -OS_type="$(uname -m)" |
51 |
| -case "$OS_type" in |
| 25 | +# Detect the architecture of the OS. |
| 26 | +OS_ARCH= |
| 27 | +case "$(uname -m)" in |
52 | 28 | x86_64 | amd64)
|
53 |
| - OS_type='amd64' |
| 29 | + OS_ARCH="amd64" |
54 | 30 | ;;
|
55 | 31 | i?86 | x86)
|
56 |
| - OS_type='386' |
| 32 | + OS_ARCH="386" |
57 | 33 | ;;
|
58 | 34 | aarch64 | arm64)
|
59 |
| - OS_type='arm64' |
| 35 | + OS_ARCH="arm64" |
60 | 36 | ;;
|
61 | 37 | *)
|
62 |
| - echo 'OS type not supported' |
| 38 | + echo "OS architecture not supported." |
63 | 39 | exit 2
|
64 | 40 | ;;
|
65 | 41 | esac
|
66 | 42 |
|
67 |
| -GH_REPO_BIN="mailpit-${OS}-${OS_type}.tar.gz" |
| 43 | +GH_REPO="axllent/mailpit" |
| 44 | +INSTALL_PATH="${INSTALL_PATH:-/usr/local/bin}" |
| 45 | +TIMEOUT=90 |
| 46 | +# This is used to authenticate with the GitHub API. (Fix the public rate limiting issue) |
| 47 | +# Try the GITHUB_TOKEN environment variable is set globally. |
| 48 | +GITHUB_API_TOKEN="${GITHUB_TOKEN:-}" |
68 | 49 |
|
69 |
| -#create tmp directory and move to it with macOS compatibility fallback |
70 |
| -tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'mailpit-install.XXXXXXXXXX') |
71 |
| -cd "$tmp_dir" |
| 50 | +# Update the default values if the user has set. |
| 51 | +while [ $# -gt 0 ]; do |
| 52 | + case $1 in |
| 53 | + --install-path) |
| 54 | + shift |
| 55 | + case "$1" in |
| 56 | + */*) |
| 57 | + # Remove trailing slashes from the path. |
| 58 | + INSTALL_PATH="$(echo "$1" | sed 's#/\+$##')" |
| 59 | + [ -z "$INSTALL_PATH" ] && INSTALL_PATH="/" |
| 60 | + ;; |
| 61 | + esac |
| 62 | + ;; |
| 63 | + --auth | --auth-token | --github-token | --token) |
| 64 | + shift |
| 65 | + case "$1" in |
| 66 | + gh*) |
| 67 | + GITHUB_API_TOKEN="$1" |
| 68 | + ;; |
| 69 | + esac |
| 70 | + ;; |
| 71 | + *) ;; |
| 72 | + esac |
| 73 | + shift |
| 74 | +done |
72 | 75 |
|
73 |
| -echo "Downloading Mailpit $VERSION" |
74 |
| -LINK="https://github.com/${GH_REPO}/releases/download/${VERSION}/${GH_REPO_BIN}" |
| 76 | +# Description of the sort parameters for curl command. |
| 77 | +# -s: Silent mode. |
| 78 | +# -f: Fail silently on server errors. |
| 79 | +# -L: Follow redirects. |
| 80 | +# -m: Set maximum time allowed for the transfer. |
75 | 81 |
|
76 |
| -curl --silent --location --max-time "${TIMEOUT}" "${LINK}" | tar zxf - || { |
77 |
| - echo "Error downloading" |
78 |
| - exit 2 |
79 |
| -} |
80 |
| - |
81 |
| -mkdir -p /usr/local/bin || exit 2 |
82 |
| -cp mailpit /usr/local/bin/ || exit 2 |
83 |
| -chmod 755 /usr/local/bin/mailpit || exit 2 |
84 |
| -case "$OS" in |
85 |
| -'linux') |
86 |
| - chown root:root /usr/local/bin/mailpit || exit 2 |
87 |
| - ;; |
88 |
| -'freebsd' | 'openbsd' | 'netbsd' | 'darwin') |
89 |
| - chown root:wheel /usr/local/bin/mailpit || exit 2 |
90 |
| - ;; |
| 82 | +if [ -n "$GITHUB_API_TOKEN" ] && [ "${#GITHUB_API_TOKEN}" -gt 36 ]; then |
| 83 | + CURL_OUTPUT="$(curl -sfL -m $TIMEOUT -H "Authorization: Bearer $GITHUB_API_TOKEN" https://api.github.com/repos/${GH_REPO}/releases/latest)" |
| 84 | + EXIT_CODE=$? |
| 85 | +else |
| 86 | + CURL_OUTPUT="$(curl -sfL -m $TIMEOUT https://api.github.com/repos/${GH_REPO}/releases/latest)" |
| 87 | + EXIT_CODE=$? |
| 88 | +fi |
| 89 | + |
| 90 | +VERSION="" |
| 91 | +if [ $EXIT_CODE -eq 0 ]; then |
| 92 | + # Extracts the latest version using jq, awk, or sed. |
| 93 | + if command -v jq >/dev/null 2>&1; then |
| 94 | + # Use jq -n because the output is not a valid JSON in sh. |
| 95 | + VERSION=$(jq -n "$CURL_OUTPUT" | jq -r '.tag_name') |
| 96 | + elif command -v awk >/dev/null 2>&1; then |
| 97 | + VERSION=$(echo "$CURL_OUTPUT" | awk -F: '$1 ~ /tag_name/ {gsub(/[^v0-9\.]+/, "", $2) ;print $2; exit}') |
| 98 | + elif command -v sed >/dev/null 2>&1; then |
| 99 | + VERSION=$(echo "$CURL_OUTPUT" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p') |
| 100 | + else |
| 101 | + EXIT_CODE=3 |
| 102 | + fi |
| 103 | +fi |
| 104 | + |
| 105 | +# Validate the version. |
| 106 | +case "$VERSION" in |
| 107 | +v[0-9][0-9\.]*) ;; |
91 | 108 | *)
|
92 |
| - echo 'OS not supported' |
93 |
| - exit 2 |
| 109 | + echo "There was an error trying to check what is the latest version of Mailpit." |
| 110 | + echo "Please try again later." |
| 111 | + exit $EXIT_CODE |
94 | 112 | ;;
|
95 | 113 | esac
|
96 | 114 |
|
97 |
| -rm -rf "$tmp_dir" |
98 |
| -echo "Installed successfully to /usr/local/bin/mailpit" |
| 115 | +TEMP_DIR="$(mktemp -qd)" |
| 116 | +EXIT_CODE=$? |
| 117 | +# Ensure the temporary directory exists and is a directory. |
| 118 | +if [ -z "$TEMP_DIR" ] || [ ! -d "$TEMP_DIR" ]; then |
| 119 | + echo "ERROR: Creating temporary directory." |
| 120 | + exit $EXIT_CODE |
| 121 | +fi |
| 122 | + |
| 123 | +GH_REPO_BIN="mailpit-${OS}-${OS_ARCH}.tar.gz" |
| 124 | +if [ "$INSTALL_PATH" = "/" ]; then |
| 125 | + INSTALL_BIN_PATH="/mailpit" |
| 126 | +else |
| 127 | + INSTALL_BIN_PATH="${INSTALL_PATH}/mailpit" |
| 128 | +fi |
| 129 | +cd "$TEMP_DIR" || EXIT_CODE=$? |
| 130 | +if [ $EXIT_CODE -eq 0 ]; then |
| 131 | + # Download the latest release. |
| 132 | + # |
| 133 | + # Description of the sort parameters for curl command. |
| 134 | + # -s: Silent mode. |
| 135 | + # -f: Fail silently on server errors. |
| 136 | + # -L: Follow redirects. |
| 137 | + # -m: Set maximum time allowed for the transfer. |
| 138 | + # -o: Write output to a file instead of stdout. |
| 139 | + curl -sfL -m $TIMEOUT -o "${GH_REPO_BIN}" "https://github.com/${GH_REPO}/releases/download/${VERSION}/${GH_REPO_BIN}" |
| 140 | + EXIT_CODE=$? |
| 141 | + |
| 142 | + # The following conditions check each step of the installation. |
| 143 | + # If there is an error in any of the steps, an error message is printed. |
| 144 | + |
| 145 | + if [ $EXIT_CODE -eq 0 ]; then |
| 146 | + if ! [ -f "${GH_REPO_BIN}" ]; then |
| 147 | + EXIT_CODE=1 |
| 148 | + echo "ERROR: Downloading latest release." |
| 149 | + fi |
| 150 | + fi |
| 151 | + |
| 152 | + if [ $EXIT_CODE -eq 0 ]; then |
| 153 | + tar zxf "$GH_REPO_BIN" |
| 154 | + EXIT_CODE=$? |
| 155 | + if [ $EXIT_CODE -ne 0 ]; then |
| 156 | + echo "ERROR: Extracting \"${GH_REPO_BIN}\"." |
| 157 | + fi |
| 158 | + fi |
| 159 | + |
| 160 | + if [ $EXIT_CODE -eq 0 ] && [ ! -d "$INSTALL_PATH" ]; then |
| 161 | + mkdir -p "${INSTALL_PATH}" |
| 162 | + EXIT_CODE=$? |
| 163 | + if [ $EXIT_CODE -ne 0 ]; then |
| 164 | + echo "ERROR: Creating \"${INSTALL_PATH}\" directory." |
| 165 | + fi |
| 166 | + fi |
| 167 | + |
| 168 | + if [ $EXIT_CODE -eq 0 ]; then |
| 169 | + cp mailpit "$INSTALL_BIN_PATH" |
| 170 | + EXIT_CODE=$? |
| 171 | + if [ $EXIT_CODE -ne 0 ]; then |
| 172 | + echo "ERROR: Copying mailpit to \"${INSTALL_PATH}\" directory." |
| 173 | + fi |
| 174 | + fi |
| 175 | + |
| 176 | + if [ $EXIT_CODE -eq 0 ]; then |
| 177 | + chmod 755 "$INSTALL_BIN_PATH" |
| 178 | + EXIT_CODE=$? |
| 179 | + if [ $EXIT_CODE -ne 0 ]; then |
| 180 | + echo "ERROR: Setting permissions for \"$INSTALL_BIN_PATH\" binary." |
| 181 | + fi |
| 182 | + fi |
| 183 | + |
| 184 | + # Set the owner and group to root:root if the script is run as root. |
| 185 | + if [ $EXIT_CODE -eq 0 ] && [ "$(id -u)" -eq "0" ]; then |
| 186 | + OWNER="root" |
| 187 | + GROUP="root" |
| 188 | + # Set the OWNER, GROUP variable when the OS not use the default root:root. |
| 189 | + case "$OS" in |
| 190 | + darwin) GROUP="wheel" ;; |
| 191 | + *) ;; |
| 192 | + esac |
| 193 | + |
| 194 | + chown "${OWNER}:${GROUP}" "$INSTALL_BIN_PATH" |
| 195 | + EXIT_CODE=$? |
| 196 | + if [ $EXIT_CODE -ne 0 ]; then |
| 197 | + echo "ERROR: Setting ownership for \"$INSTALL_BIN_PATH\" binary." |
| 198 | + fi |
| 199 | + fi |
| 200 | +else |
| 201 | + echo "ERROR: Changing to temporary directory." |
| 202 | + exit $EXIT_CODE |
| 203 | +fi |
| 204 | + |
| 205 | +# Cleanup the temporary directory. |
| 206 | +rm -rf "$TEMP_DIR" |
| 207 | +# Check the EXIT_CODE variable, and print the success or error message. |
| 208 | +if [ $EXIT_CODE -ne 0 ]; then |
| 209 | + echo "There was an error installing Mailpit." |
| 210 | + exit $EXIT_CODE |
| 211 | +fi |
| 212 | + |
| 213 | +echo "Installed successfully to \"$INSTALL_BIN_PATH\"." |
| 214 | +exit 0 |
0 commit comments