dotfiles/lib/crates.sh

80 lines
1.7 KiB
Bash
Raw Normal View History

2024-06-04 01:33:44 +02:00
#!/bin/sh
DF_CRATE_STRICT=${DF_CRATE_STRICT:-0}
# crate_validate <crate> <exit_on_invalid>
# example:
# crate_validate $1 1
# crate_validate $1 0 || return 1
crate_validate() {
a=$DF_RUNNER_VERBOSE
DF_RUNNER_VERBOSE=
err=0
[ -d "crates/$1" ] || err=1
[ -f "crates/$1/apply.sh" ] || err=1
[ -f "crates/$1/.invalid" ] && err=1
if [ $err = 1 ]; then
if [ "${2:-$DF_CRATE_STRICT}" = "1" ]; then
echo "$1 is an invalid crate"
exit 1
else
return 1
fi
fi
DF_RUNNER_VERBOSE=$a
}
in_crate() {
crate=$1
crate_validate $1 0 || return 1
shift
DF_RUNNER_WORKDIR="$(pwd)/crates/$crate" ./runner $@
}
crate_applied() {
crate_validate $1 0 || return 1
in_crate "$1" [ -f ".installed" ]
}
crate_has_root_apply() {
crate_validate $1 0 || return 1
in_crate "$1" [ -f "apply_root.sh" ]
}
crate_has_root_remove() {
crate_validate $1 0 || return 1
in_crate "$1" [ -f "remove_root.sh" ]
}
crate_removable() {
crate_validate $1 0 || return 1
in_crate "$1" [ -f "remove.sh" ]
}
crate_info() {
if ! crate_validate $1 0; then
printf "%s " "!"
else
if crate_applied $1; then
if crate_removable $1; then
printf "%s " "+"
else
printf "%s " "*"
fi
else
printf "%s " "-"
fi
fi
printf "%s " $1
crate_removable $1 && printf "[removable] "
crate_has_root_apply $1 && printf "[has root apply] "
crate_has_root_remove $1 && printf "[has root remove] "
printf "\n"
}
crate_apply() {
crate_validate $1 1
echo "applying crates/$1..."
in_crate $1 sh apply.sh
}