From aee9e2ed5bcd1189dc07fb424c704dcd403705eb Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Sun, 25 Dec 2022 02:44:52 -0700 Subject: [PATCH] Feature/apk install (#134) * wip * wip * Updated install.sh to support alpine/apk * tidied up install.sh output for apk * refactor install.sh; hopefully embedded version info now includes 'v' * install.sh cleanup --- .goreleaser-darwin.yml | 2 +- .goreleaser-linux-amd64.yml | 2 +- .goreleaser-linux-arm64.yml | 2 +- .goreleaser-windows.yml | 2 +- install.sh | 112 +++++++++++++++++++++++++++++------- 5 files changed, 96 insertions(+), 24 deletions(-) diff --git a/.goreleaser-darwin.yml b/.goreleaser-darwin.yml index a22a4016..6eeb8bd1 100644 --- a/.goreleaser-darwin.yml +++ b/.goreleaser-darwin.yml @@ -16,7 +16,7 @@ builds: - arm64 ldflags: - -s -w - - -X github.com/neilotoole/sq/cli/buildinfo.Version={{ .Version }} + - -X github.com/neilotoole/sq/cli/buildinfo.Version=v{{ .Version }} - -X github.com/neilotoole/sq/cli/buildinfo.Commit={{ .ShortCommit }} - -X github.com/neilotoole/sq/cli/buildinfo.Timestamp={{ .Date }} diff --git a/.goreleaser-linux-amd64.yml b/.goreleaser-linux-amd64.yml index 8eb6fa67..6f18f326 100644 --- a/.goreleaser-linux-amd64.yml +++ b/.goreleaser-linux-amd64.yml @@ -17,7 +17,7 @@ builds: - amd64 ldflags: - -s -w - - -X github.com/neilotoole/sq/cli/buildinfo.Version={{ .Version }} + - -X github.com/neilotoole/sq/cli/buildinfo.Version=v{{ .Version }} - -X github.com/neilotoole/sq/cli/buildinfo.Commit={{ .ShortCommit }} - -X github.com/neilotoole/sq/cli/buildinfo.Timestamp={{ .Date }} diff --git a/.goreleaser-linux-arm64.yml b/.goreleaser-linux-arm64.yml index ab1e38ed..ef8a9942 100644 --- a/.goreleaser-linux-arm64.yml +++ b/.goreleaser-linux-arm64.yml @@ -20,7 +20,7 @@ builds: ldflags: - -extld=aarch64-linux-gnu-gcc - -s -w - - -X github.com/neilotoole/sq/cli/buildinfo.Version={{ .Version }} + - -X github.com/neilotoole/sq/cli/buildinfo.Version=v{{ .Version }} - -X github.com/neilotoole/sq/cli/buildinfo.Commit={{ .ShortCommit }} - -X github.com/neilotoole/sq/cli/buildinfo.Timestamp={{ .Date }} diff --git a/.goreleaser-windows.yml b/.goreleaser-windows.yml index 3b57cece..3699d95f 100644 --- a/.goreleaser-windows.yml +++ b/.goreleaser-windows.yml @@ -15,7 +15,7 @@ builds: - amd64 ldflags: - -s -w - - -X github.com/neilotoole/sq/cli/buildinfo.Version={{ .Version }} + - -X github.com/neilotoole/sq/cli/buildinfo.Version=v{{ .Version }} - -X github.com/neilotoole/sq/cli/buildinfo.Commit={{ .ShortCommit }} - -X github.com/neilotoole/sq/cli/buildinfo.Timestamp={{ .Date }} diff --git a/install.sh b/install.sh index 1111b62f..8974fc80 100755 --- a/install.sh +++ b/install.sh @@ -1,13 +1,33 @@ #!/usr/bin/env sh -# This script attempts to install sq via apt, yum, or brew. +# This script attempts to install sq via apt, yum, apk, or brew. +# Parts of the script are inspired by the get-docker.sh +# script at https://get.docker.com -# Test if apt is installed -apt --version >/dev/null 2>&1 -if [ "$?" -eq "0" ]; then +get_distribution() { + lsb_dist="" + # Every system that we officially support has /etc/os-release + if [ -r /etc/os-release ]; then + lsb_dist="$(. /etc/os-release && echo "$ID")" + fi + # Returning an empty string here should be alright since the + # case statements don't act unless you provide an actual value + echo "$lsb_dist" +} + +# Usage: +# +# if command_exists lsb_release; then +command_exists() { + command -v "$@" > /dev/null 2>&1 +} + +get_distribution + +# apt / deb +if [ -r /etc/debian_version ] && command_exists apt; then set -e - echo "Using apt to install sq..." - echo "" + printf "Using apt to install sq...\n\n" apt update -y && apt install -y --no-upgrade curl gpg @@ -23,16 +43,18 @@ EOF apt update -y && apt install -y sq + printf "\n" + sq version + printf "\n" exit fi -# Test if yum is installed -yum version >/dev/null 2>&1 -if [ "$?" -eq "0" ]; then +# Yum / rpm +if command_exists yum; then set -e - echo "Using yum to install sq..." - echo "" + set +x + printf "Using yum to install sq...\n\n" cat < /etc/yum.repos.d/sq.repo [sq] @@ -45,28 +67,78 @@ EOF yum install -y sq + printf "\n" + sq version + printf "\n" exit fi -# Test if brew is installed -brew --version >/dev/null 2>&1 -if [ "$?" -eq "0" ]; then +# apk / alpine +if command_exists apk; then set -e - echo "Using brew to install sq..." - echo "" + printf "Using apk to install sq...\n\n" + apk update + + # sq isn't published to an Alpine repo yet, so we download the + # file from GitHub, and execute "apk add" with the local apk file. + + # e.g. "v1.0.0" + semver=$(wget -qO- "https://api.github.com/repos/neilotoole/sq/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + + # e.g. "1.0.0" + ver=$(echo "$semver" | sed -e "s/^v//") + + # Should be "x86_64" for amd64, and "aarch64" for arm64 + arch=$(uname -m) + + if [ "$arch" == "x86_64" ]; then + arch="amd64" + elif [ "$arch" == "aarch64" ]; then + arch="arm64" + else + printf "sq install package not available for architecture %q\n" $arch + exit 1 + fi + + # e.g. "sq_0.18.1_linux_arm64.apk" + file_name=$(printf "sq_%s_linux_%s.apk" "$ver" $arch) + file_path="/tmp/$file_name" + + # https://github.com/neilotoole/sq/releases/download/v0.18.1/sq_0.18.1_linux_amd64.apk + # https://github.com/neilotoole/sq/releases/download/v0.18.1/sq_0.18.1_linux_arm64.apk + download_url=$(printf "https://github.com/neilotoole/sq/releases/download/%s/%s" "$semver" "$file_name") + + echo "Downloading apk from: $download_url" + wget "$download_url" -O "$file_path" + + apk add --allow-untrusted "$file_path" + rm "$file_path" + + printf "\n" + sq version + printf "\n" + exit +fi + +# brew +if command_exists brew; then + set -e + printf "Using brew to install sq...\n\n" brew install neilotoole/sq/sq + printf "\n" + sq version + printf "\n" exit fi -echo "" -echo "Could not find a suitable install mechanism to install sq." -echo "" -echo "Visit https://github.com/neilotoole/sq for more installation options." +printf "\nCould not find a suitable install mechanism to install sq.\n" +printf "\nVisit https://github.com/neilotoole/sq for more installation options.\n" exit 1 +