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 "
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
return
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
echo ' ∓'
fi
}
)
function parse_git_branch {
parse_git_branch () (
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
)
# display git branch
PS1="$PS1\[$bblack\]"'$(parse_git_branch)'
# display git status
@ -37,4 +37,3 @@ fi
# display date
PS1="$PS1\[$purple\]\D{%F %I:%M%P} "
PS1="$PS1\[$color_off\]\nλ "

@ -3,19 +3,18 @@
alias slugify="sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z"
source_env_file () {
file="${1}"
if [ ! -f "${file}" ]; then
if [ ! -f "${1}" ]; then
return
fi
eval "$(
grep -v '^\s*\#' "${file}" |
grep -v '^\s*\#' "${1}" |
grep -v '^\s*$' |
sed 's/^\s*export//g' |
sed 's/^/export /g'
)"
}
add_date_prefix () {
add_date_prefix () (
DIR=$(dirname "$1")
FILE=$(basename "$1")
DATE=$(date -r "$1" +"%F")
@ -24,9 +23,9 @@ add_date_prefix () {
return
fi
mv "$1" "$DIR/${DATE}_${FILE}"
}
)
get_bitrate () {
get_bitrate () (
if [ ! -f "$1" ] ; then
echo "[ERROR] unknown file: $1"
return
@ -36,4 +35,4 @@ get_bitrate () {
return
fi
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
bail () {
bail () (
>&2 echo "ERROR: $1"
exit 1
}
)
must_be_root () {
must_be_root () (
if [ "$(id --user)" != 0 ]; then
bail "must run as root"
fi
}
)
set_link () {
TARGET="$1"
APP_DIR="$2"
APP_PATH="$3"
set_link () (
TARGET=${1}
APP_DIR=${2}
APP_PATH=${3:-}
pushd "$(dirname "${TARGET}")" >/dev/null || exit 1
unlink_if_set "${TARGET}"
REL_PATH=$(relative_path "$(dirname "${TARGET}")" "${APP_DIR}/current/${APP_PATH}")
ln -s "${REL_PATH}" "$(basename "${TARGET}")"
popd >/dev/null || exit 1
}
)
get_os () {
get_os () (
case $(uname -s) in
Linux*) echo linux;;
Darwin*) echo darwin;;
@ -36,9 +36,9 @@ get_os () {
CYGWIN_NT*) echo windows;;
*) >&2 echo "unsupported os"; return;
esac
}
)
get_arch () {
get_arch () (
case $(uname -m) in
x86_64*) echo amd64;;
amd64*) echo amd64;;
@ -51,43 +51,43 @@ get_arch () {
aarch64*) echo arm64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac
}
)
set_current_link () {
prefix="${1}"
version="${2}"
sudo="${3}"
set_current_link () (
prefix=${1}
version=${2}
sudo=${3:-}
current="${prefix}/current"
if [ -L "${current}" ] ; then
$sudo unlink "${current}"
fi
rel="$(relative_path "${prefix}" "${prefix}/${version}")"
$sudo ln -s "$rel" "${current}"
}
)
unlink_if_set () {
dir="$1"
sudo="$2"
unlink_if_set () (
dir=$1
sudo=${2:-}
if [ -L "${dir}" ]; then
# shellcheck disable=SC2086
${sudo} unlink "${dir}"
fi
}
)
mkdir_if_missing () {
dir="$1"
sudo=$2
mkdir_if_missing () (
dir=${1}
sudo=${2:-}
if [ ! -d "${dir}" ]; then
# shellcheck disable=SC2086
$sudo mkdir -p "${dir}"
fi
}
)
extract_zip () {
url="${1}"
dest="${2}"
args="${3}"
sudo="${4}"
extract_zip () (
url=${1}
dest=${2}
args=${3}
sudo=${4:-}
if [ -d "${dest}" ]; then
return
fi
@ -99,13 +99,13 @@ extract_zip () {
# shellcheck disable=SC2086
$sudo unzip -o "${zipfile}" -d "${dest}" ${args}
rm -rf "${tmp_dir}"
}
)
extract_tarball () {
url="${1}"
dest="${2}"
args="${3}"
sudo="${4}"
extract_tarball () (
url=${1}
dest=${2}
args=${3}
sudo=${4:-}
if [ -d "${dest}" ]; then
return
fi
@ -117,9 +117,9 @@ extract_tarball () {
# shellcheck disable=SC2086
$sudo tar xf "${tarball}" --directory "${dest}" ${args}
rm -rf "${tmp_dir}"
}
)
relative_path() {
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
@ -161,4 +161,4 @@ relative_path() {
fi
printf '%s' "$result"
}
)

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

@ -1,13 +1,16 @@
#!/usr/bin/env bash
set -e
set -euo pipefail
# shellcheck disable=SC1090
source "${XDG_DATA_HOME}/buddy-up/includes/node.sh"
VERSION=${VERSION:-14.17.6}
DEST="${XDG_DATA_HOME}/apps/releases/node"
CURRENT="${DEST}/current"
VERSION=${VERSION:-node_lts}
DEST=${XDG_DATA_HOME}/apps/releases/node
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"
set_current_link "${DEST}" "${VERSION}"

@ -9,6 +9,9 @@ PREFIX=${PREFIX:-/usr/local}
DEST="${PREFIX}/apps/releases/node"
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
set_current_link "${DEST}" "${VERSION}" sudo

@ -3,7 +3,12 @@
# shellcheck disable=SC1090
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
x86_64*) echo x64;;
amd64*) echo x64;;
@ -14,12 +19,12 @@ get_arch () {
aarch64*) echo arm64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac
}
)
set_symlink () {
src="$1"
dest="$2"
sudo="$3"
set_symlink () (
src=${1}
dest=${2}
sudo=${3:-}
dest_dir=$(dirname "${dest}")
mkdir_if_missing "${dest_dir}" "$sudo"
@ -27,8 +32,4 @@ set_symlink () {
unlink_if_set "${dest}" "${sudo}"
$sudo ln -s "$(relative_path "${dest_dir}" "${src}")" "$(basename "${dest}")"
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