Add github helper util functions

main
Buddy Sandidge 3 years ago
parent 1996867364
commit 944dcd7d4b

@ -15,7 +15,7 @@ bblack='\e[1;30m'
PS1="\[$green\]"'\u '"\[$blue\]"'$(hostname -s | tr ':A-Z:' ':a-z:') '"\[$green\]\w " PS1="\[$green\]"'\u '"\[$blue\]"'$(hostname -s | tr ':A-Z:' ':a-z:') '"\[$green\]\w "
if command -v git &> /dev/null ; then if command -v git &> /dev/null ; then
function parse_git_status { parse_git_status () (
if [[ $(git status 2> /dev/null | wc -l) -eq 0 ]]; then if [[ $(git status 2> /dev/null | wc -l) -eq 0 ]]; then
return return
fi fi
@ -23,11 +23,11 @@ if command -v git &> /dev/null ; then
if [[ $(git status 2> /dev/null | grep -c "working tree clean") -eq 0 ]]; then if [[ $(git status 2> /dev/null | grep -c "working tree clean") -eq 0 ]]; then
echo ' ∓' echo ' ∓'
fi fi
} )
function parse_git_branch { parse_git_branch () (
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
} )
# display git branch # display git branch
PS1="$PS1\[$bblack\]"'$(parse_git_branch)' PS1="$PS1\[$bblack\]"'$(parse_git_branch)'
# display git status # display git status
@ -37,4 +37,3 @@ fi
# display date # display date
PS1="$PS1\[$purple\]\D{%F %I:%M%P} " PS1="$PS1\[$purple\]\D{%F %I:%M%P} "
PS1="$PS1\[$color_off\]\nλ " PS1="$PS1\[$color_off\]\nλ "

@ -3,19 +3,18 @@
alias slugify="sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z" alias slugify="sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z"
source_env_file () { source_env_file () {
file="${1}" if [ ! -f "${1}" ]; then
if [ ! -f "${file}" ]; then
return return
fi fi
eval "$( eval "$(
grep -v '^\s*\#' "${file}" | grep -v '^\s*\#' "${1}" |
grep -v '^\s*$' | grep -v '^\s*$' |
sed 's/^\s*export//g' | sed 's/^\s*export//g' |
sed 's/^/export /g' sed 's/^/export /g'
)" )"
} }
add_date_prefix () { add_date_prefix () (
DIR=$(dirname "$1") DIR=$(dirname "$1")
FILE=$(basename "$1") FILE=$(basename "$1")
DATE=$(date -r "$1" +"%F") DATE=$(date -r "$1" +"%F")
@ -24,9 +23,9 @@ add_date_prefix () {
return return
fi fi
mv "$1" "$DIR/${DATE}_${FILE}" mv "$1" "$DIR/${DATE}_${FILE}"
} )
get_bitrate () { get_bitrate () (
if [ ! -f "$1" ] ; then if [ ! -f "$1" ] ; then
echo "[ERROR] unknown file: $1" echo "[ERROR] unknown file: $1"
return return
@ -36,4 +35,4 @@ get_bitrate () {
return return
fi fi
exiftool -AudioBitrate "$1" | awk '{print $4}' exiftool -AudioBitrate "$1" | awk '{print $4}'
} )

@ -0,0 +1,31 @@
#!/usr/bin/env bash
latest_download () (
FILTER='import "github" as gh; gh::latest_download(env.OS; env.ARCH)'
OS=${2} ARCH="${3}" jq --raw-output "${FILTER}" <"$(release_json "${1}")"
)
latest_type () (
FILTER='import "github" as gh; gh::latest_type(env.OS; env.ARCH)'
OS=${2} ARCH="${3}" jq --raw-output "${FILTER}" <"$(release_json "${1}")"
)
latest_version () (
FILTER='import "github" as gh; gh::latest_version'
jq --raw-output "${FILTER}" <"$(release_json "${1}")"
)
age_in_seconds () (
echo $(( $(date +"%s") - $(stat -c "%Y" "${1}") ))
)
release_json () (
DAY=$((60 * 60 * 24))
PROJECT=$1
JSON=${XDG_CACHE_HOME}/apps/meta/${PROJECT}/releases.json
mkdir -p "$(dirname "${JSON}")"
if [[ ! -f "${JSON}" || "$(age_in_seconds "${JSON}")" -gt "${DAY}" ]]; then
curl --silent --output "${JSON}" "https://api.github.com/repos/${PROJECT}/releases"
fi
echo "${JSON}"
)

@ -1,29 +1,29 @@
#ft=bash #ft=bash
bail () { bail () (
>&2 echo "ERROR: $1" >&2 echo "ERROR: $1"
exit 1 exit 1
} )
must_be_root () { must_be_root () (
if [ "$(id --user)" != 0 ]; then if [ "$(id --user)" != 0 ]; then
bail "must run as root" bail "must run as root"
fi fi
} )
set_link () { set_link () (
TARGET="$1" TARGET=${1}
APP_DIR="$2" APP_DIR=${2}
APP_PATH="$3" APP_PATH=${3:-}
pushd "$(dirname "${TARGET}")" >/dev/null || exit 1 pushd "$(dirname "${TARGET}")" >/dev/null || exit 1
unlink_if_set "${TARGET}" unlink_if_set "${TARGET}"
REL_PATH=$(relative_path "$(dirname "${TARGET}")" "${APP_DIR}/current/${APP_PATH}") REL_PATH=$(relative_path "$(dirname "${TARGET}")" "${APP_DIR}/current/${APP_PATH}")
ln -s "${REL_PATH}" "$(basename "${TARGET}")" ln -s "${REL_PATH}" "$(basename "${TARGET}")"
popd >/dev/null || exit 1 popd >/dev/null || exit 1
} )
get_os () { get_os () (
case $(uname -s) in case $(uname -s) in
Linux*) echo linux;; Linux*) echo linux;;
Darwin*) echo darwin;; Darwin*) echo darwin;;
@ -36,9 +36,9 @@ get_os () {
CYGWIN_NT*) echo windows;; CYGWIN_NT*) echo windows;;
*) >&2 echo "unsupported os"; return; *) >&2 echo "unsupported os"; return;
esac esac
} )
get_arch () { get_arch () (
case $(uname -m) in case $(uname -m) in
x86_64*) echo amd64;; x86_64*) echo amd64;;
amd64*) echo amd64;; amd64*) echo amd64;;
@ -51,43 +51,43 @@ get_arch () {
aarch64*) echo arm64;; aarch64*) echo arm64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;; *) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac esac
} )
set_current_link () { set_current_link () (
prefix="${1}" prefix=${1}
version="${2}" version=${2}
sudo="${3}" sudo=${3:-}
current="${prefix}/current" current="${prefix}/current"
if [ -L "${current}" ] ; then if [ -L "${current}" ] ; then
$sudo unlink "${current}" $sudo unlink "${current}"
fi fi
rel="$(relative_path "${prefix}" "${prefix}/${version}")" rel="$(relative_path "${prefix}" "${prefix}/${version}")"
$sudo ln -s "$rel" "${current}" $sudo ln -s "$rel" "${current}"
} )
unlink_if_set () { unlink_if_set () (
dir="$1" dir=$1
sudo="$2" sudo=${2:-}
if [ -L "${dir}" ]; then if [ -L "${dir}" ]; then
# shellcheck disable=SC2086 # shellcheck disable=SC2086
${sudo} unlink "${dir}" ${sudo} unlink "${dir}"
fi fi
} )
mkdir_if_missing () { mkdir_if_missing () (
dir="$1" dir=${1}
sudo=$2 sudo=${2:-}
if [ ! -d "${dir}" ]; then if [ ! -d "${dir}" ]; then
# shellcheck disable=SC2086 # shellcheck disable=SC2086
$sudo mkdir -p "${dir}" $sudo mkdir -p "${dir}"
fi fi
} )
extract_zip () { extract_zip () (
url="${1}" url=${1}
dest="${2}" dest=${2}
args="${3}" args=${3}
sudo="${4}" sudo=${4:-}
if [ -d "${dest}" ]; then if [ -d "${dest}" ]; then
return return
fi fi
@ -99,13 +99,13 @@ extract_zip () {
# shellcheck disable=SC2086 # shellcheck disable=SC2086
$sudo unzip -o "${zipfile}" -d "${dest}" ${args} $sudo unzip -o "${zipfile}" -d "${dest}" ${args}
rm -rf "${tmp_dir}" rm -rf "${tmp_dir}"
} )
extract_tarball () { extract_tarball () (
url="${1}" url=${1}
dest="${2}" dest=${2}
args="${3}" args=${3}
sudo="${4}" sudo=${4:-}
if [ -d "${dest}" ]; then if [ -d "${dest}" ]; then
return return
fi fi
@ -117,9 +117,9 @@ extract_tarball () {
# shellcheck disable=SC2086 # shellcheck disable=SC2086
$sudo tar xf "${tarball}" --directory "${dest}" ${args} $sudo tar xf "${tarball}" --directory "${dest}" ${args}
rm -rf "${tmp_dir}" rm -rf "${tmp_dir}"
} )
relative_path() { relative_path() (
# both $1 and $2 are absolute paths beginning with / # both $1 and $2 are absolute paths beginning with /
# $1 must be a canonical path; that is none of its directory # $1 must be a canonical path; that is none of its directory
# components may be ".", ".." or a symbolic link # components may be ".", ".." or a symbolic link
@ -161,4 +161,4 @@ relative_path() {
fi fi
printf '%s' "$result" printf '%s' "$result"
} )

@ -30,12 +30,11 @@ fi
export XDG_RUNTIME_DIR export XDG_RUNTIME_DIR
env_file () { env_file () {
file="${1}" if [ ! -f "${1}" ]; then
if [ ! -f "${file}" ]; then
return return
fi fi
eval "$( eval "$(
grep -v '^\s*\#' "${file}" | grep -v '^\s*\#' "${1}" |
grep -v '^\s*$' | grep -v '^\s*$' |
sed 's/^\s*export//g' | sed 's/^\s*export//g' |
sed 's/^/export /g' sed 's/^/export /g'

@ -1,13 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -euo pipefail
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${XDG_DATA_HOME}/buddy-up/includes/node.sh" source "${XDG_DATA_HOME}/buddy-up/includes/node.sh"
VERSION=${VERSION:-14.17.6} VERSION=${VERSION:-node_lts}
DEST="${XDG_DATA_HOME}/apps/releases/node" DEST=${XDG_DATA_HOME}/apps/releases/node
CURRENT="${DEST}/current" CURRENT=${DEST}/current
# shellcheck disable=SC2034
URL=https://nodejs.org/dist/${VERSION}/node-${VERSION}-$(get_os)-$(get_arch).tar.xz
extract_tarball "${URL}" "${DEST}/${VERSION}" "--strip-components 1" extract_tarball "${URL}" "${DEST}/${VERSION}" "--strip-components 1"
set_current_link "${DEST}" "${VERSION}" set_current_link "${DEST}" "${VERSION}"

@ -9,6 +9,9 @@ PREFIX=${PREFIX:-/usr/local}
DEST="${PREFIX}/apps/releases/node" DEST="${PREFIX}/apps/releases/node"
CURRENT="${DEST}/current" CURRENT="${DEST}/current"
# shellcheck disable=SC2034
URL=https://nodejs.org/dist/${VERSION}/node-${VERSION}-$(get_os)-$(get_arch).tar.xz
extract_tarball "${URL}" "${DEST}/${VERSION}" "--strip-components 1" sudo extract_tarball "${URL}" "${DEST}/${VERSION}" "--strip-components 1" sudo
set_current_link "${DEST}" "${VERSION}" sudo set_current_link "${DEST}" "${VERSION}" sudo

@ -3,7 +3,12 @@
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${XDG_DATA_HOME}/buddy-up/includes/utils.sh" source "${XDG_DATA_HOME}/buddy-up/includes/utils.sh"
get_arch () { node_lts () (
curl --silent https://nodejs.org/dist/index.tab |
cut -f 1,10 | grep '-' | head -n 1 | cut -f 1
)
get_arch () (
case $(uname -m) in case $(uname -m) in
x86_64*) echo x64;; x86_64*) echo x64;;
amd64*) echo x64;; amd64*) echo x64;;
@ -14,12 +19,12 @@ get_arch () {
aarch64*) echo arm64;; aarch64*) echo arm64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;; *) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac esac
} )
set_symlink () { set_symlink () (
src="$1" src=${1}
dest="$2" dest=${2}
sudo="$3" sudo=${3:-}
dest_dir=$(dirname "${dest}") dest_dir=$(dirname "${dest}")
mkdir_if_missing "${dest_dir}" "$sudo" mkdir_if_missing "${dest_dir}" "$sudo"
@ -27,8 +32,4 @@ set_symlink () {
unlink_if_set "${dest}" "${sudo}" unlink_if_set "${dest}" "${sudo}"
$sudo ln -s "$(relative_path "${dest_dir}" "${src}")" "$(basename "${dest}")" $sudo ln -s "$(relative_path "${dest_dir}" "${src}")" "$(basename "${dest}")"
popd > /dev/null || exit 1 popd > /dev/null || exit 1
} )
VERSION=${VERSION:-v14.17.3}
# shellcheck disable=SC2034
URL=https://nodejs.org/dist/${VERSION}/node-${VERSION}-$(get_os)-$(get_arch).tar.xz

Loading…
Cancel
Save