|
| 1 | +#!/bin/sh -e |
| 2 | + |
| 3 | +CHOOSE_ANOTHER_INSTALLATION_METHOD="Please choose another installation method https://github.com/scaleway/scaleway-cli#installation." |
| 4 | + |
| 5 | +# Detect the operating system and CPU architecture |
| 6 | +echo "Detecting your operating system and CPU architecture..." >&2 |
| 7 | + |
| 8 | +case "$(uname -s)" in |
| 9 | +Darwin) os="darwin" ;; |
| 10 | +FreeBSD) os="freebsd" ;; |
| 11 | +Linux) os="linux" ;; |
| 12 | +*) |
| 13 | + echo "$(uname -s) is not supported by this installation script. $CHOOSE_ANOTHER_INSTALLATION_METHOD" >&2 |
| 14 | + exit 1 |
| 15 | + ;; |
| 16 | +esac |
| 17 | + |
| 18 | +case "$(uname -m)" in |
| 19 | +x86_64) arch="amd64" ;; |
| 20 | +armv8*) arch="arm64" ;; |
| 21 | +armv*) arch="arm" ;; |
| 22 | +i386) arch="386" ;; |
| 23 | +*) |
| 24 | + echo "$(uname -m) is not supported by this installation script. $CHOOSE_ANOTHER_INSTALLATION_METHOD" >&2 |
| 25 | + exit 1 |
| 26 | + ;; |
| 27 | +esac |
| 28 | + |
| 29 | +# Check if curl or wget is available |
| 30 | +echo "Checking if curl or wget is available..." >&2 |
| 31 | + |
| 32 | +has_curl=$(which curl || true) |
| 33 | +has_wget=$(which wget || true) |
| 34 | + |
| 35 | +if [ -z "$has_curl" ] && [ -z "$has_wget" ]; then |
| 36 | + echo "You need curl or wget to proceed." >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Get the latest release from GitHub API |
| 41 | +echo "Fetching the latest release from GitHub..." >&2 |
| 42 | + |
| 43 | +if [ -n "$has_curl" ]; then |
| 44 | + latest_release_json=$(curl -s https://api.github.com/repos/scaleway/scaleway-cli/releases/latest) |
| 45 | +elif [ -n "$has_wget" ]; then |
| 46 | + latest_release_json=$(wget -q -O - https://api.github.com/repos/scaleway/scaleway-cli/releases/latest) |
| 47 | +fi |
| 48 | + |
| 49 | +latest=$(echo "$latest_release_json" | grep "browser_download_url.*${os}_${arch}" | cut -d : -f 2,3 | tr -d \" | tr -d " ") |
| 50 | +if [ -z "$latest" ]; then |
| 51 | + echo "Unable to find the latest ${os}_${arch} release. https://github.com/scaleway/scaleway-cli/releases" >&2 |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Check if sudo or root permissions are available |
| 56 | +echo "Checking if sudo or root permissions are available..." >&2 |
| 57 | + |
| 58 | +is_root=$(id -u) |
| 59 | +has_sudo=$(which sudo || true) |
| 60 | + |
| 61 | +if [ "$is_root" -eq 0 ]; then |
| 62 | + sudo="" |
| 63 | +elif [ -n "$has_sudo" ]; then |
| 64 | + sudo="sudo" |
| 65 | + echo "The installation script will use sudo to proceed. You may be asked to enter your password." >&2 |
| 66 | +else |
| 67 | + echo "You need sudo or root permissions to proceed." >&2 |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +# Download the latest release |
| 72 | +echo "Downloading the latest release..." >&2 |
| 73 | + |
| 74 | +if [ -n "$has_curl" ]; then |
| 75 | + $sudo curl -s -L "$latest" -o /usr/local/bin/scw |
| 76 | +elif [ -n "$has_wget" ]; then |
| 77 | + $sudo wget -q -O /usr/local/bin/scw "$latest" |
| 78 | +fi |
| 79 | + |
| 80 | +echo "Setting executable permissions..." >&2 |
| 81 | +$sudo chmod +x /usr/local/bin/scw |
| 82 | + |
| 83 | +# Success |
| 84 | +echo "\033[0;32mScaleway CLI has been successfully installed! Start using it with: \033[1;32mscw --help\033[0m" |
0 commit comments