From cfc88ae3c534a444e6960121b5f8b97cbd96d5e9 Mon Sep 17 00:00:00 2001 From: ge Date: Thu, 19 May 2022 08:39:44 +0300 Subject: [PATCH] feat: Move is_installed() and is_function_set() to common.sh --- src/lib/common.sh | 26 +++++++++++++++++++++++++- src/lib/source.sh | 12 +----------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/lib/common.sh b/src/lib/common.sh index 0ffc66b..9111fc2 100644 --- a/src/lib/common.sh +++ b/src/lib/common.sh @@ -54,7 +54,7 @@ log() { } err() { - # Handle error. Print errors to STDERR, log, process + # Handle errors. Print errors to STDERR, log, process # and exit from script. # # Usage: err [-eao] MESSAGE @@ -95,6 +95,7 @@ err() { # Run user defined function on_error() if [ "$onerr" ]; then if is_function_set on_error; then + echo -e "Run \e[1mon_error()\e[0m ..." | log -p on_error fi fi @@ -118,3 +119,26 @@ try() { err -aeo "Check $__log_file log for details." fi } + +is_installed() { + # Check if the program is installed. + # See good answer: https://stackoverflow.com/a/677212 + # + # Usage: is_installed COMMAND + + if ! command -v "$1" >/dev/null 2>&1; then + echo "Error: Command $1 not found." \ + "Please install $1 or check your PATH if it's actually installed." >&2 + exit 1 + fi +} + +is_function_set() { + # Test function is set or not. Return exit code. + # Useful with 'if' statement. + # + # Usage: is_function_set FUNCTION + + declare -F -- "$1" > /dev/null + return "$?" +} diff --git a/src/lib/source.sh b/src/lib/source.sh index 9ee6cd3..ea783d9 100644 --- a/src/lib/source.sh +++ b/src/lib/source.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# script.sh - utilitary functions. +# source.sh - source and validate scripts. # Copyright (c) 2022 ge # # This program is free software: you can redistribute it and/or modify @@ -120,13 +120,3 @@ source_script() { echo Error: $__user_script: targets array is not set >&2; exit 1 fi } - -is_function_set() { - # Test function is set or not. Return exit code. - # Useful with 'if' statement. - # - # Usage: is_function_set FUNCTION - - declare -F -- "$1" > /dev/null - return "$?" -}