-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-require.sh
56 lines (49 loc) · 1.35 KB
/
14-require.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Simple system to track and install dependencies of the various bashrc.d modules.
# Create an associative array (dict). We use the key for package name and value for
# comments, such as what's needed from the package.
declare -A REQ_ARR
export REQ_ARR
# Check if packages are present and, if they are not, add them to a list of missing
# requirements. If all the packages in the list are present, exit with 0 (true), else 1
# (false).
require() {
missing=0
for pkg in "$@"; do
pkg_is_installed "$pkg"
[[ $? -ne 0 ]] && {
echo "Missing package: $pkg"
missing=1
}
done
return $missing
}
require_cmd() {
arg=("$@") req=('name of command required to be in PATH') opt=()
usage arg req opt && return 1
cmd_str="$1"
abort
cmd_is_installed cmd_str || {
printf >&2 "Missing a required command: %s\n" "$cmd_str"
}
}
list_deps() {
for pkg in "${!REQ_ARR[@]}"; do
echo "$pkg (${REQ_ARR[$pkg]})"
done
}
# Install any missing packages.
pkg_install_missing() {
[[ -n "${REQ_ARR[*]}" ]] || {
pkg_install "${REQ_ARR[@]}"
}
}
# Install a list of packages.
pkg_install() {
sudo apt-get install --no-upgrade --yes "$@"
}
# Exit with status 0 (exists) or 1 (does not exist) for a given package name.
pkg_is_installed() {
q="$(dpkg-query -W -f '${Status}\n' "${1}" 2>&1)"
[[ "$q" =~ is\ installed ]] && return 1
return 0
}