ansi colors
This commit is contained in:
+1
-32
@@ -7,38 +7,7 @@ if [[ ! -v MIAOU_BASH_LOADED_ONCE ]]; then
|
||||
readonly MIAOU_BASH_LOADED_ONCE=true
|
||||
readonly MIAOU_DEBUG=${MIAOU_DEBUG:-false}
|
||||
readonly MIAOU_ERROR_REGEX='^(.*): line ([0-9]+): miaou_error:'
|
||||
|
||||
declare -A ANSI
|
||||
ANSI[RESET]=0
|
||||
|
||||
ANSI[STYLE_ITALIC]=3
|
||||
ANSI[STYLE_BOLD]=1
|
||||
ANSI[STYLE_UNDERLINE]=4
|
||||
ANSI[STYLE_STRIKETHROUGH]=9
|
||||
|
||||
ANSI[FG_BLACK]=30
|
||||
ANSI[FG_RED]=31
|
||||
ANSI[FG_GREEN]=32
|
||||
ANSI[FG_YELLOW]=33
|
||||
ANSI[FG_BLUE]=34
|
||||
ANSI[FG_PURPLE]=35
|
||||
ANSI[FG_CYAN]=36
|
||||
ANSI[FG_WHITE]=37
|
||||
ANSI[FG_GRAY]=90
|
||||
ANSI[FG_LIGHTBLUE]=94
|
||||
ANSI[FG_MAGENTA]=95
|
||||
|
||||
ANSI[BG_BLACK]=40
|
||||
ANSI[BG_RED]=41
|
||||
ANSI[BG_GREEN]=42
|
||||
ANSI[BG_YELLOW]=43
|
||||
ANSI[BG_BLUE]=44
|
||||
ANSI[BG_PURPLE]=45
|
||||
ANSI[BG_CYAN]=46
|
||||
ANSI[BG_WHITE]=47
|
||||
ANSI[BG_GRAY]=90
|
||||
ANSI[BG_MAGENTA]=95
|
||||
readonly ANSI
|
||||
source "$MIAOU_BASH_DIR"/lib/ansi.bash
|
||||
fi
|
||||
## functions
|
||||
|
||||
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env miaou-bash
|
||||
|
||||
# CONSTANTS
|
||||
|
||||
REFRESH_SLOT='24 hours'
|
||||
DEBIAN_REPO_DIR='/var/lib/apt/lists/partial'
|
||||
ARCH_REPO_DIR='/var/lib/pacman/sync'
|
||||
|
||||
# functions
|
||||
|
||||
function usage {
|
||||
echo "usage: $(basename "$0") <packages...>"
|
||||
echo 'idempotent package installation : install only if not yet done + smart refresh repositories'
|
||||
}
|
||||
|
||||
# $1 -> file to compare with REFRESH_SLOT
|
||||
function needs_refresh {
|
||||
[[ $(date --date="-$REFRESH_SLOT" +%s) -gt $(date -d "$(stat -c %y "$1")" +%s) ]]
|
||||
}
|
||||
|
||||
function debian_update_repo {
|
||||
if needs_refresh "$DEBIAN_REPO_DIR"; then
|
||||
echo "updating repositories..."
|
||||
apt-get update
|
||||
fi
|
||||
}
|
||||
|
||||
function debian_add {
|
||||
declare -a wanted=("$@")
|
||||
declare -a missing=()
|
||||
mapfile -t queries < <(dpkg-query -W -f='${Package}: ${Status}\n' "${wanted[@]}" 2>&1 || true)
|
||||
|
||||
local unrecognized_regex='^dpkg-query: no packages found matching (.*)$'
|
||||
local missing_regex='(.*): .* not-installed$'
|
||||
for query in "${queries[@]}"; do
|
||||
if [[ "$query" =~ $unrecognized_regex ]]; then
|
||||
missing+=("${BASH_REMATCH[1]}")
|
||||
elif [[ "$query" =~ $missing_regex ]]; then
|
||||
missing+=("${BASH_REMATCH[1]}")
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "${#missing[@]}" -gt 0 ]]; then
|
||||
debian_update_repo
|
||||
echo installing packages: "${missing[@]}"
|
||||
apt-get install -y "${missing[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
function arch_update_repo {
|
||||
if needs_refresh "$ARCH_REPO_DIR"; then
|
||||
echo "updating repositories..."
|
||||
pacman -Syyu --noconfirm
|
||||
fi
|
||||
}
|
||||
|
||||
function arch_add {
|
||||
declare -a wanted=("$@")
|
||||
declare -a missing=()
|
||||
for i in "${wanted[@]}"; do
|
||||
if ! pacman -Ql "$i" &>/dev/null; then
|
||||
missing+=("$i")
|
||||
fi
|
||||
done
|
||||
if [[ "${#missing[@]}" -gt 0 ]]; then
|
||||
arch_update_repo
|
||||
echo installing packages: "${missing[@]}"
|
||||
pacman -S --noconfirm "${missing[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
# MAIN
|
||||
|
||||
[ "$UID" -ne 0 ] && echo 'root privilege required' && builtin exit 1
|
||||
[[ $# -lt 1 ]] && usage && builtin exit 2
|
||||
|
||||
source "$MIAOU_BASH_DIR"/lib/functions.bash
|
||||
DISTRO_LIKE=$(get_distro_like)
|
||||
case "$DISTRO_LIKE" in
|
||||
debian) debian_add "$@" ;;
|
||||
arch) arch_add "$@" ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user