From 0a78be8aeb835f903222b7fe8372b432700d5919 Mon Sep 17 00:00:00 2001 From: KwadFan Date: Tue, 19 Oct 2021 21:22:24 +0200 Subject: [PATCH 1/2] Added two 'QoL' funtions to common.sh * check_install_pkgs - check if pkgs is installed if not installs it. * apt_update_skip - Skips 'apt update' if cache not older than one Hour --- src/common.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/common.sh b/src/common.sh index 870de36e..03f9cb0e 100755 --- a/src/common.sh +++ b/src/common.sh @@ -398,6 +398,20 @@ function minimize_ext() { fi } +# Skip apt update if Cache not older than 1 Hour. +function apt_update_skip() { + if [ -f "/var/cache/apt/pkgcache.bin" ] && \ + [ "$(($(date +%s)-$(stat -c %Y /var/cache/apt/pkgcache.bin)))" -lt "3600" ]; + then + echo_green "APT Cache needs no update! [SKIPPED]" + else + # force update + echo_red "APT Cache needs to be updated!" + echo_green "Running 'apt update' ..." + apt update + fi +} + function is_installed(){ # checks if a package is installed, returns 1 if installed and 0 if not. # usage: is_installed @@ -414,6 +428,30 @@ function is_in_apt(){ fi } +### Only install Packages if not installed. +## check_install_pkgs $MODULNAME_PKGS_VAR (Replace with your Variable set in config file) +function check_install_pkgs() { + ## Build Array from Var + local missing_pkgs + for dep in "$@"; do + # if in apt cache and not installed add to array + if [ $(is_in_apt ${dep}) -eq 1 ] && [ $(is_installed ${dep}) -ne 1 ]; then + missing_pkgs+="${dep}" + else + # if not in apt cache + echo_red "Missing Package ${dep} not found in Apt Repository. [SKIPPED]" + fi + done + # if missing pkgs install missing else skip that. + if [ "${#missing_pkgs[@]}" -ne 0 ]; then + echo_red "${#missing_pkgs[@]} missing Packages..." + echo_green "Installing ${missing_pkgs[@]}" + apt install --yes "${missing_pkgs[@]}" + else + echo_green "No Dependencies missing... [SKIPPED]" + fi +} + function remove_if_installed(){ remove_extra_list="" for package in "$1" From 8ef84eba299f9522da35f3b34e2890ba08bb9766 Mon Sep 17 00:00:00 2001 From: KwadFan Date: Wed, 20 Oct 2021 11:52:14 +0200 Subject: [PATCH 2/2] Fixed Typo in common.sh Forgot to set missing_pkgs Variable to an Array --- src/common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common.sh b/src/common.sh index 03f9cb0e..c31de1a1 100755 --- a/src/common.sh +++ b/src/common.sh @@ -436,7 +436,7 @@ function check_install_pkgs() { for dep in "$@"; do # if in apt cache and not installed add to array if [ $(is_in_apt ${dep}) -eq 1 ] && [ $(is_installed ${dep}) -ne 1 ]; then - missing_pkgs+="${dep}" + missing_pkgs+=("${dep}") else # if not in apt cache echo_red "Missing Package ${dep} not found in Apt Repository. [SKIPPED]"