From: sev Date: Mon, 14 Jun 2021 05:22:38 +0000 (-0500) Subject: add apkv, explicitly source XDG dirs in zshenv X-Git-Url: https://git.sev.monster/~sev/dotfiles.git/commitdiff_plain/c0b3d4b6bc9c9c8b2a88bc1035e877714d81ebf1 add apkv, explicitly source XDG dirs in zshenv new apkv script to make management of virtual packages easier on Alpine. small change to explicitly source XDG user-dirs in case host doesn't --- diff --git a/base/.zshenv b/base/.zshenv index 2660783..1f3e198 100644 --- a/base/.zshenv +++ b/base/.zshenv @@ -37,6 +37,9 @@ if [[ -v _sev_reset_shell || $SHLVL == 1 ]] { export XDG_DATA_DIRS=~/.local/share:/usr/pkg/share:/usr/local/share:/usr/share export XDG_CACHE_HOME=~/tmp export XDG_RUNTIME_DIR=~/tmp + if [[ -e $XDG_CONFIG_HOME/user-dirs.dirs ]] { + source $XDG_CONFIG_HOME/user-dirs.dirs + } ## create tmp link t=${TMPDIR:-/tmp}/home-$LOGNAME diff --git a/bin/apkv b/bin/apkv new file mode 100755 index 0000000..bf89590 --- /dev/null +++ b/bin/apkv @@ -0,0 +1,127 @@ +#!/bin/sh + +usage() { + echo "\ +apkv: Quickly add and remove packages to/from Alpine virtual packages. + +Usage: + $0 add + Add to virtual package . If the virtual package + does not exist, it will be created. + $0 del + Remove from virtual package . Empty virtual + packages are removed from world. The shorthand '$0 del * ' can + be used to empty . + $0 list [packages] + List all or specific virtual packages and their contents. + $0 help + Show this help. + +Virtual packages must be prefixed with a dot to be managed with apkv. If one is +not present, it will be added automatically. + +Methods can be shortened to their closest unambiguous forms, e.g. a, d, or l. +However, due to implementation specifics, any string starting with those forms +will work; that functionality is unspecified and is not guaranteed. + +For example, the following are equivalent: + apkv add syslinux .virt + apkv a syslinux virt + apkv abcdefg syslinux .virt + +" +} + +prepend_dot() { + case $1 in + .*) printf "$1";; + *) printf ".$1";; + esac +} + +show_reqs() { + _pkg="$1" + shift >/dev/null + [ -z "$_pkg" ] && return 1 + if [ $# -gt 0 ]; then + _list=$(echo "$(for x do echo $x; done)" | sort | uniq) + else + apk info -e "$_pkg" >/dev/null || return 1 + _list=$(apk info -R "$_pkg" | awk 'NR > 1 && NF' | sort) + fi + echo -e "\e[1m$_pkg\e[0m:" $_list +} + +method="$1" +shift >/dev/null +case $method in + l*) for x in ${*:-$(apk info | grep '^\.')}; do + show_reqs "$(prepend_dot "$x")" + done + exit;; + a*) method=add;; + d*) method=del;; + h*) usage; exit;; + -*) usage; exit;; + *) [ -n "$method" ] && echo "Invalid method $method" >&2; usage; exit 1;; +esac + +## add/del +# get and validate packages +pkgs= +virt= +for x; do + if [ -n "$pkgs" ]; then + pkgs="$pkgs $virt" + elif [ -n "$virt" ]; then + pkgs=$virt + fi + virt=$x +done +if [ -z "$pkgs" ]; then + echo 'Not enough arguments' >&2 + usage + exit 1 +fi +virt=$(prepend_dot $virt) +# exit if database locked (99) or no package found to delete from (1) +apk info -e "$virt" >/dev/null +code=$? +if [ $code -eq 99 -o \( $method = del -a $code -ne 0 \) ]; then + exit $code +fi + +# we have validated our args, so let's prepare to run apk: get sudo command if +# it exists and we are not already root +# TODO: support doas +if [ "$(id -u)" != 0 ] && command -v sudo >/dev/null 2>&1; then + sudo=$(command -v sudo) +fi + +# handle quick delete shorthand mentioned in help +if [ $method = del -a '*' = "$pkgs" ]; then + $sudo apk del "$virt" + exit $? +fi + +currpkgs=$(apk info -R "$virt" | awk 'NR > 1 && NF') +case $method in + # $(echo) construct is to use word splitting to normalize whitespace + add) currpkgs="$(echo $currpkgs) $pkgs";; + del) # XXX: there's probably a more efficient way to do this + for x in "$pkgs"; do + currpkgs=$(echo "$currpkgs" | grep -vFiw $x) + done;; +esac + +if [ -z "$currpkgs" ]; then + # virtual package is empty + $sudo apk del "$virt" +else + show_reqs "$virt" $currpkgs + $sudo apk add -t "$virt" $currpkgs +fi +# pass along sudo/apk exit code +exit $? + +# vi:sw=4:sts=4:et