From 82ce67f572147390b8376566bd4d20a5630ebee3 Mon Sep 17 00:00:00 2001 From: Gregory Chamberlain Date: Sun, 18 Apr 2021 10:00:06 +0100 Subject: [PATCH] improve portability of install.sh (#107) * Replace echo with printf The echo(1) command varies among sh implementations. Some interpret hyphen command-line options. It would be unusual, but in theory if $PREFIX began with a hyphen or two then this usage of echo might cause an error or unknown behaviour in such implementations. The printf(1) command is consistent across shell implementations. * Leave privilege elevation to the user * Add die function * Break long line --- README.md | 10 +++++++++- install.sh | 29 +++++++++++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d796bb2..073468e 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,16 @@ more flexibility. ## Installation +As root, using sudo: + ```sh -curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | sh +curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | sudo sh +``` + +As a regular user: + +```sh +curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | PREFIX=~/.local sh ``` Read the Nix section below for a Nix-based installation method. *See the [wiki section](https://github.com/soywod/himalaya/wiki/Installation) diff --git a/install.sh b/install.sh index 88cd81d..48b095b 100644 --- a/install.sh +++ b/install.sh @@ -2,6 +2,11 @@ set -eu +die() { + printf '%s\n' "$1" >&2 + exit "${2-1}" +} + DESTDIR="${DESTDIR:-/}" PREFIX="${PREFIX:-"$DESTDIR/usr/local"}" RELEASES_URL="https://github.com/soywod/himalaya/releases" @@ -12,28 +17,20 @@ case $system in msys*|mingw*|cygwin*|win*) system=windows;; linux|freebsd) system=linux;; darwin) system=macos;; - *) echo "Error: Unsupported system: $system"; exit 1;; + *) die "Unsupported system: $system" ;; esac -if ! tmpdir=$(mktemp -d); then - echo "Error: Failed to create tmpdir" - exit 1 -else - trap "rm -rf $tmpdir" EXIT -fi +tmpdir=$(mktemp -d) || die "Failed to create tmpdir" +trap "rm -rf $tmpdir" EXIT echo "Downloading latest $system release…" -curl -sLo "$tmpdir/himalaya.tar.gz" "$RELEASES_URL/latest/download/himalaya-$system.tar.gz" +curl -sLo "$tmpdir/himalaya.tar.gz" \ + "$RELEASES_URL/latest/download/himalaya-$system.tar.gz" echo "Installing binary…" tar -xzf "$tmpdir/himalaya.tar.gz" -C "$tmpdir" -if [ -w "$PREFIX" ]; then - mkdir -p "$PREFIX/bin" - cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya" -else - sudo mkdir -p "$PREFIX/bin" - sudo cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya" -fi +mkdir -p "$PREFIX/bin" +cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya" -echo "$("$PREFIX/bin/himalaya" --version) installed!" +die "$("$PREFIX/bin/himalaya" --version) installed!" 0