feat: Move is_installed() and is_function_set() to common.sh
This commit is contained in:
parent
9ed6528911
commit
cfc88ae3c5
@ -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 "$?"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# script.sh - utilitary functions.
|
||||
# source.sh - source and validate scripts.
|
||||
# Copyright (c) 2022 ge <https://nixhacks.net/>
|
||||
#
|
||||
# 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 "$?"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user