From fb74e39a2bed48ed61b1bb7de64ec5fb13873016 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 14 Mar 2021 19:29:44 -0700 Subject: [PATCH] Reduce boilerplate of install scripts --- base/.local/share/buddy-up/includes/utils.sh | 102 ++++++++++++++++++ base/.zshrc | 2 +- go/.config/profile/profile.d/go.sh | 9 +- go/.local/bin/install-go-system | 22 ++++ go/.local/bin/install-gomock-home | 22 ++++ .../.local/bin/install-etcd-system | 74 +++---------- install-scripts/.local/bin/install-go-system | 59 ---------- install-scripts/.local/bin/install-hugo-home | 48 ++++----- .../.local/bin/install-lego-system | 75 +++---------- .../.local/bin/install-minikube-system | 71 ++++-------- .../{install-protoc => install-protoc-system} | 9 +- 11 files changed, 238 insertions(+), 255 deletions(-) create mode 100644 base/.local/share/buddy-up/includes/utils.sh create mode 100755 go/.local/bin/install-go-system create mode 100755 go/.local/bin/install-gomock-home delete mode 100755 install-scripts/.local/bin/install-go-system rename install-scripts/.local/bin/{install-protoc => install-protoc-system} (58%) diff --git a/base/.local/share/buddy-up/includes/utils.sh b/base/.local/share/buddy-up/includes/utils.sh new file mode 100644 index 0000000..c386bd3 --- /dev/null +++ b/base/.local/share/buddy-up/includes/utils.sh @@ -0,0 +1,102 @@ +#ft=sh + +bail () { + >&2 echo "ERROR: $1" + exit 1 +} + +must_be_root () { + if [ "$(id --user)" != 0 ]; then + bail "must run as root" + fi +} + +get_os () { + case $(uname -s) in + Linux*) echo linux;; + Darwin*) echo darwin;; + *) bail "unsupported os: $(uname -s)" ;; + esac +} + +get_arch () { + case $(uname -m) in + x86_64*) echo amd64;; + amd64*) echo amd64;; + *) bail "unsupported architecture: $(uname -m)" ;; + esac +} + +set_current_link () { + prefix="${1}" + version="${2}" + sudo="${3}" + current="${prefix}/current" + if [ -s "${current}" ] ; then + $sudo unlink "${current}" + fi + $sudo ln --symbolic --relative "${prefix}/${version}" "${current}" +} + +extract_tarball () { + url="${1}" + dest="${2}" + args="${3}" + sudo="${4}" + + if [ -d "${dest}" ]; then + return + fi + $sudo mkdir -p "${dest}" + + tmp_dir="$(mktemp --directory)" + tarball=${tmp_dir}/release.tar.gz + curl --silent --location --output "${tarball}" "${url}" + # shellcheck disable=SC2086 + $sudo tar xf "${tarball}" --directory "${dest}" ${args} + rm -rf "${tmp_dir}" +} + +relative_path() { + # both $1 and $2 are absolute paths beginning with / + # $1 must be a canonical path; that is none of its directory + # components may be ".", ".." or a symbolic link + # + # returns relative path to $2/$target from $1/$src + src=$1 + target=$2 + + common_part=$src + result= + + while [ "${target#"$common_part"}" = "$target" ]; do + # no match, means that candidate common part is not correct + # go up one level (reduce common part) + common_part=$(dirname "$common_part") + # and record that we went back, with correct / handling + if [ -z "$result" ]; then + result=.. + else + result=../$result + fi + done + + if [ "$common_part" = / ]; then + # special case for root (no common path) + result=$result/ + fi + + # since we now have identified the common part, + # compute the non-common part + forward_part=${target#"$common_part"} + + # and now stick all parts together + if [ -n "$result" ] && [ -n "$forward_part" ]; then + result=$result$forward_part + elif [ -n "$forward_part" ]; then + # extra slash removal + result=${forward_part#?} + fi + + printf '%s' "$result" +} diff --git a/base/.zshrc b/base/.zshrc index 7f5f679..5fe723a 100644 --- a/base/.zshrc +++ b/base/.zshrc @@ -9,7 +9,7 @@ if [[ -d "${XDG_CONFIG_HOME}/zsh/env.d" ]]; then done fi -if [ -d "${XDG_CONFIG_HOME}/zsh/zshrc.d" ]; then +if [[ -d "${XDG_CONFIG_HOME}/zsh/zshrc.d" ]]; then for file in "${XDG_CONFIG_HOME}"/zsh/zshrc.d/*.zsh(N); do if [ -r "$file" ]; then #shellcheck disable=1090 diff --git a/go/.config/profile/profile.d/go.sh b/go/.config/profile/profile.d/go.sh index 4a9dcf4..ae12dba 100644 --- a/go/.config/profile/profile.d/go.sh +++ b/go/.config/profile/profile.d/go.sh @@ -1,6 +1,9 @@ -if [ -d /usr/local/go/bin ] ; then - PATH="${PATH}:/usr/local/go/bin" +__APPS_GO_PATH=/usr/local/apps/golang/current/bin +if [ -d "${__APPS_GO_PATH}" ] && ! echo "${PATH}" | grep -q ${__APPS_GO_PATH} ; then + PATH="$__APPS_GO_PATH:${PATH}" fi -if [ -d "${HOME}/go/bin" ] ; then +unset __APPS_GO_PATH + +if [ -d "${HOME}/go/bin" ] && ! echo "$PATH" | grep -q "${HOME}/go/bin" ; then PATH="${PATH}:${HOME}/go/bin" fi diff --git a/go/.local/bin/install-go-system b/go/.local/bin/install-go-system new file mode 100755 index 0000000..883b212 --- /dev/null +++ b/go/.local/bin/install-go-system @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -e + +# shellcheck disable=SC1090 +source "${XDG_DATA_HOME}/buddy-up/includes/utils.sh" + +VERSION=${VERSION:-1.16.2} +DEST=/usr/local/apps/golang +URL=https://dl.google.com/go/go${VERSION}.$(get_os)-$(get_arch).tar.gz +DEST_BIN=$DEST/current/bin + +extract_tarball "${URL}" "${DEST}/${VERSION}" "--strip-components 1" sudo +set_current_link "${DEST}" "${VERSION}" sudo + +if [[ ! -f /etc/profile.d/go-path.sh ]]; then + cat <