|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Fancy colors for output |
| 5 | +RED='\033[0;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +NC='\033[0m' # No Color aka reset |
| 8 | + |
| 9 | +# Version to install. Defaults to latest or set by --version or -v |
| 10 | +VERSION="" |
| 11 | + |
| 12 | +# Print in colors - 0=green, 1=red, 2=neutral |
| 13 | +# e.g. fancy_print 0 "All is great" |
| 14 | +fancy_print() { |
| 15 | + if [[ $1 == 0 ]]; then |
| 16 | + echo -e "${GREEN}${2}${NC}" |
| 17 | + elif [[ $1 == 1 ]]; then |
| 18 | + echo -e "${RED}${2}${NC}" |
| 19 | + else |
| 20 | + echo -e "${2}" |
| 21 | + fi |
| 22 | +} |
| 23 | + |
| 24 | +# Function to print the help message |
| 25 | +print_help() { |
| 26 | + fancy_print 2 "" |
| 27 | + fancy_print 2 "---- Spin Installer Script ----" |
| 28 | + fancy_print 2 "This script installs Spin in the current directory." |
| 29 | + fancy_print 2 "" |
| 30 | + fancy_print 2 "Comand line arguments" |
| 31 | + fancy_print 2 "--version or -v : Provide what version to install e.g. \"v0.5.0\" or \"canary\"." |
| 32 | + fancy_print 2 "--help or -h : Shows this help message" |
| 33 | +} |
| 34 | + |
| 35 | +# Function used to check if utilities are available |
| 36 | +require() { |
| 37 | + if ! hash "$1" &>/dev/null; then |
| 38 | + fancy_print 1 "'$1' not found in PATH. This is required for this script to work." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +# Parse input arguments |
| 44 | +while [[ $# -gt 0 ]]; do |
| 45 | + case $1 in |
| 46 | + '--version' | -v) |
| 47 | + shift |
| 48 | + if [[ $# -ne 0 ]]; then |
| 49 | + VERSION="${1}" |
| 50 | + else |
| 51 | + fancy_print 1 "Please provide the desired version. e.g. --version v0.5.0 or -v canary" |
| 52 | + exit 0 |
| 53 | + fi |
| 54 | + ;; |
| 55 | + '--help' | -h) |
| 56 | + shift |
| 57 | + print_help |
| 58 | + ;; |
| 59 | + *) |
| 60 | + fancy_print 1 "Unknown argument ${1}." |
| 61 | + print_help |
| 62 | + exit 1 |
| 63 | + ;; |
| 64 | + esac |
| 65 | + shift |
| 66 | +done |
| 67 | + |
| 68 | +# Check all required utilities are available |
| 69 | +require wget |
| 70 | +require tar |
| 71 | +require uname |
| 72 | + |
| 73 | +# Check if we're on a suppoerted system and get OS and processor architecture to download the right version |
| 74 | +UNAME_ARC=$(uname -m) |
| 75 | + |
| 76 | +case $UNAME_ARC in |
| 77 | +"x86_64") |
| 78 | + ARC="amd64" |
| 79 | + ;; |
| 80 | +"arm64" | "aarch64") |
| 81 | + ARC="aarch64" |
| 82 | + ;; |
| 83 | +*) |
| 84 | + fancy_print 1 "The Processor type: ${UNAME_ARC} is not yet supported by Spin." |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | +esac |
| 88 | + |
| 89 | +case $OSTYPE in |
| 90 | +"linux-gnu"*) |
| 91 | + OS="linux" |
| 92 | + if [[ $ARC == "aarch64" ]]; then |
| 93 | + fancy_print 1 "The Processor type: ${ARC}, on ${OSTYPE} is not yet supported by Spin." |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + ;; |
| 97 | +"darwin"*) |
| 98 | + OS="macos" |
| 99 | + ;; |
| 100 | +*) |
| 101 | + fancy_print 1 "The OSTYPE: ${OSTYPE} is not supported by this script." |
| 102 | + fancy_print 2 "Please refer to this article to install Spin: https://spin.fermyon.dev/quickstart/" |
| 103 | + exit 1 |
| 104 | + ;; |
| 105 | +esac |
| 106 | + |
| 107 | +# Check desired version. Default to latest if no desired version was requested |
| 108 | +if [[ $VERSION = "" ]]; then |
| 109 | + VERSION=$(wget -qO- https://github.com/fermyon/spin/releases | grep 'href="/fermyon/spin/releases/tag/v[0-9]*.[0-9]*.[0-9]*\"' | sed -E 's/.*\/fermyon\/spin\/releases\/tag\/(v[0-9\.]+)".*/\1/g' | head -1) |
| 110 | +fi |
| 111 | + |
| 112 | +# Constructing download FILE and URL |
| 113 | +FILE="spin-${VERSION}-${OS}-${ARC}.tar.gz" |
| 114 | +URL="https://github.com/fermyon/spin/releases/download/${VERSION}/${FILE}" |
| 115 | + |
| 116 | +# Download file, exit if not found - e.g. version does not exist |
| 117 | +fancy_print 0 "Step 1: Downloading: ${URL}" |
| 118 | +wget -q $URL || (fancy_print 1 "The requested file does not exist: ${FILE}"; exit 1) |
| 119 | +fancy_print 0 "Done...\n" |
| 120 | + |
| 121 | +# Decompress the file |
| 122 | +fancy_print 0 "Step 2: Decompressing: ${FILE}" |
| 123 | +tar xfv $FILE |
| 124 | +./spin --version |
| 125 | +fancy_print 0 "Done...\n" |
| 126 | + |
| 127 | +# Remove the compressed file |
| 128 | +fancy_print 0 "Step 3: Removing the downloaded tarball" |
| 129 | +rm $FILE |
| 130 | +fancy_print 0 "Done...\n" |
| 131 | + |
| 132 | +# Direct to quicks-start doc |
| 133 | +fancy_print 0 "You're good to go. Check here for the next steps: https://spin.fermyon.dev/quickstart/" |
| 134 | +fancy_print 0 "Run './spin' to get started" |
0 commit comments