]> git.sev.monster Git - dotfiles.git/blame - bin/apkv
user-dirs: fix XDG_PUBLICSHARE_DIR
[dotfiles.git] / bin / apkv
CommitLineData
c0b3d4b6 1#!/bin/sh
2
3usage() {
4 echo "\
5apkv: Quickly add and remove packages to/from Alpine virtual packages.
6
7Usage:
8 $0 add <packages> <virtual>
9 Add <packages> to virtual package <virtual>. If the virtual package
10 does not exist, it will be created.
11 $0 del <packages> <virtual>
12 Remove <packages> from virtual package <virtual>. Empty virtual
13 packages are removed from world. The shorthand '$0 del * <virtual>' can
14 be used to empty <virtual>.
15 $0 list [packages]
16 List all or specific virtual packages and their contents.
17 $0 help
18 Show this help.
19
20Virtual packages must be prefixed with a dot to be managed with apkv. If one is
21not present, it will be added automatically.
22
23Methods can be shortened to their closest unambiguous forms, e.g. a, d, or l.
24However, due to implementation specifics, any string starting with those forms
25will work; that functionality is unspecified and is not guaranteed.
26
27For example, the following are equivalent:
bf0563c2 28 $0 add syslinux .virt
29 $0 a syslinux virt
30 $0 abcdefg syslinux .virt
c0b3d4b6 31
32"
33}
34
35prepend_dot() {
36 case $1 in
37 .*) printf "$1";;
38 *) printf ".$1";;
39 esac
40}
41
42show_reqs() {
43 _pkg="$1"
44 shift >/dev/null
45 [ -z "$_pkg" ] && return 1
46 if [ $# -gt 0 ]; then
47 _list=$(echo "$(for x do echo $x; done)" | sort | uniq)
48 else
49 apk info -e "$_pkg" >/dev/null || return 1
50 _list=$(apk info -R "$_pkg" | awk 'NR > 1 && NF' | sort)
51 fi
52 echo -e "\e[1m$_pkg\e[0m:" $_list
53}
54
55method="$1"
56shift >/dev/null
57case $method in
58 l*) for x in ${*:-$(apk info | grep '^\.')}; do
59 show_reqs "$(prepend_dot "$x")"
60 done
61 exit;;
62 a*) method=add;;
63 d*) method=del;;
64 h*) usage; exit;;
65 -*) usage; exit;;
66 *) [ -n "$method" ] && echo "Invalid method $method" >&2; usage; exit 1;;
67esac
68
69## add/del
70# get and validate packages
71pkgs=
72virt=
73for x; do
74 if [ -n "$pkgs" ]; then
75 pkgs="$pkgs $virt"
76 elif [ -n "$virt" ]; then
77 pkgs=$virt
78 fi
79 virt=$x
80done
81if [ -z "$pkgs" ]; then
82 echo 'Not enough arguments' >&2
83 usage
84 exit 1
85fi
86virt=$(prepend_dot $virt)
87# exit if database locked (99) or no package found to delete from (1)
88apk info -e "$virt" >/dev/null
89code=$?
90if [ $code -eq 99 -o \( $method = del -a $code -ne 0 \) ]; then
91 exit $code
92fi
93
94# we have validated our args, so let's prepare to run apk: get sudo command if
95# it exists and we are not already root
96# TODO: support doas
97if [ "$(id -u)" != 0 ] && command -v sudo >/dev/null 2>&1; then
98 sudo=$(command -v sudo)
99fi
100
101# handle quick delete shorthand mentioned in help
102if [ $method = del -a '*' = "$pkgs" ]; then
103 $sudo apk del "$virt"
104 exit $?
105fi
106
107currpkgs=$(apk info -R "$virt" | awk 'NR > 1 && NF')
108case $method in
109 # $(echo) construct is to use word splitting to normalize whitespace
110 add) currpkgs="$(echo $currpkgs) $pkgs";;
111 del) # XXX: there's probably a more efficient way to do this
bf0563c2 112 for x in $pkgs; do
c0b3d4b6 113 currpkgs=$(echo "$currpkgs" | grep -vFiw $x)
114 done;;
115esac
116
117if [ -z "$currpkgs" ]; then
118 # virtual package is empty
119 $sudo apk del "$virt"
120else
121 show_reqs "$virt" $currpkgs
122 $sudo apk add -t "$virt" $currpkgs
123fi
124# pass along sudo/apk exit code
125exit $?
126
127# vi:sw=4:sts=4:et
This page took 0.03858 seconds and 4 git commands to generate.