From 3a363c648641314ed3d20d4c9f363cf01072c301 Mon Sep 17 00:00:00 2001 From: Galen Guyer Date: Tue, 9 Aug 2022 18:23:15 -0400 Subject: add logging module --- dots | 1 - lib/log.sh | 38 ++++++++++++++++++++++++++++++++++++++ modules/40-git/install.sh | 3 ++- modules/40-neofetch/install.sh | 3 ++- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 lib/log.sh diff --git a/dots b/dots index 72d9484..9a4d9fa 100755 --- a/dots +++ b/dots @@ -21,7 +21,6 @@ EOF function main() { intro - echo "Running as $(id -un) on $(os::distro)" MODULES=modules/**/install.sh diff --git a/lib/log.sh b/lib/log.sh new file mode 100644 index 0000000..f204d7e --- /dev/null +++ b/lib/log.sh @@ -0,0 +1,38 @@ +function log() { + local log_level=0 + local log_level_string="" + + case $1 in + debug) + log_level=0 + log_level_string="[DBG]" + ;; + verbose) + log_level=1 + log_level_string="[VRB]" + ;; + info) + log_level=2 + log_level_string="[INF]" + ;; + warn) + log_level=3 + log_level_string="[WRN]" + ;; + error) + log_level=4 + log_level_string="[ERR]" + ;; + esac + shift + + # set the log level to info if unset + [[ -z "DOT_LOG_LEVEL" ]] && DOT_LOG_LEVEL=2 + + # if this is too verbose, stop now + [[ "$DOT_LOG_LEVEL" -gt "$level" ]] && return + + [[ -n "$_modulename" ]] && module="[$_modulename]: " || module="" + + echo "$log_level_string $module$@" +} diff --git a/modules/40-git/install.sh b/modules/40-git/install.sh index 890db77..384f539 100644 --- a/modules/40-git/install.sh +++ b/modules/40-git/install.sh @@ -12,10 +12,11 @@ function preinstall() { function install() { # backup old file if it exists and we haven't backed it up before if [[ -f "$HOME/.gitconfig" ]] && ! [[ -f "$HOME/.gitconfig.bak" ]] && ! [[ -f "$_scriptdir/.firstrun" ]]; then - echo "making backup" + log verbose "making backup" cp "$HOME/.gitconfig" "$HOME/.gitconfig.bak" touch "$_scriptdir/.firstrun" fi + log info "linking gitconfig" ln -f "$_scriptdir/gitconfig" "$HOME/.gitconfig" } diff --git a/modules/40-neofetch/install.sh b/modules/40-neofetch/install.sh index 65a90d3..c432a71 100644 --- a/modules/40-neofetch/install.sh +++ b/modules/40-neofetch/install.sh @@ -12,10 +12,11 @@ function preinstall() { function install() { # backup old file if it exists and we haven't backed it up before if [[ -f "$HOME/.config/neofetch/config.conf" ]] && ! [[ -f "$HOME/.config/neofetch/config.conf.bak" ]] && ! [[ -f "$_scriptdir/.firstrun" ]]; then - echo "making backup" + log verbose "making backup" cp "$HOME/.config/neofetch/config.conf" "$HOME/.config/neofetch/config.conf.bak" touch "$_scriptdir/.firstrun" fi + log info "linking neofetch config" ln -f "$_scriptdir/config.conf" "$HOME/.config/neofetch/config.conf" } -- cgit v1.2.3