2024-10-26 22:54:26 +02:00
|
|
|
#!/usr/bin/env bash
|
2024-10-26 23:23:16 +02:00
|
|
|
# STAROPENSOURCE BASHUTILS SOURCE FILE
|
|
|
|
# Copyright (c) 2024 The StarOpenSource bashutils Authors
|
|
|
|
# Licensed under the GNU Affero General Public License v3
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# The copyright only protects the code from this line on until
|
|
|
|
# the "BASHUTILS CODE ENDS HERE" line. This notice is provided
|
|
|
|
# in case the bashutils script is embedded into a script file.
|
|
|
|
# The copyright notice above this notice does not apply the code
|
|
|
|
# above this notice or below the "BASHUTILS CODE ENDS HERE" line
|
|
|
|
# unless it licensed under the same license without modifications.
|
2024-10-26 22:54:26 +02:00
|
|
|
|
|
|
|
|
2024-11-17 16:14:49 +01:00
|
|
|
# Logging
|
|
|
|
## => Handles log calls and prints the specified message correctly.
|
|
|
|
## => Arguments: <int:level_id> <str:level_name> <str...:message>
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function _bashutils_log() {
|
|
|
|
# Cancel if disallowed
|
|
|
|
[ ! "${BASHUTILS_LOGLEVEL}" -lt "${1}" ] && return
|
|
|
|
|
|
|
|
# Set variables
|
|
|
|
# -> Level
|
|
|
|
#LEVEL_ID=${1}
|
|
|
|
LEVEL_NAME=${2}
|
|
|
|
# -> Message
|
|
|
|
shift 2
|
|
|
|
MESSAGE=${*}
|
|
|
|
# -> Indentation
|
|
|
|
unset INDENTATION
|
|
|
|
for ((i=1; i <= BASHUTILS_SUBSHELL; i++))
|
|
|
|
do
|
|
|
|
INDENTATION="${INDENTATION}${BASHUTILS_LOGINDENTATION}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Add shadow
|
|
|
|
MESSAGE=${MESSAGE//\\n/\\n }
|
|
|
|
|
|
|
|
# Print
|
|
|
|
echo -e "${LEVEL_NAME} ${INDENTATION} ${MESSAGE}";
|
2024-10-27 13:26:32 +01:00
|
|
|
}
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints a diagnostic message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function diag() { _bashutils_log "1" "DIAG" "${*}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints a verbose message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function verb() { _bashutils_log "2" "VERB" "${*}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints a silent warning message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function sarn() { _bashutils_log "3" "SARN" "${*}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints an informational message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function info() { _bashutils_log "4" "INFO" "${*}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints a warning message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function warn() { _bashutils_log "5" "WARN" "${*}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Prints an error message.
|
|
|
|
## => Arguments: <str...:message>
|
|
|
|
## => Returns: 0
|
2024-11-17 16:14:49 +01:00
|
|
|
function error() { _bashutils_log "6" "ERR!" "${*}" &> /dev/stderr; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Handles crashes.
|
|
|
|
## => Arguments: <bool:terminate?> <str...:message>
|
|
|
|
## => Returns: 0
|
2024-10-26 22:54:26 +02:00
|
|
|
function crash() {
|
|
|
|
[ "${1}" == "true" ] && CRASH_TERMINATE=true || CRASH_TERMINATE=false
|
|
|
|
shift
|
|
|
|
# shellcheck disable=SC2002
|
|
|
|
cat << EOF &> /dev/stderr
|
|
|
|
$([ "${CRASH_TERMINATE}" == "false" ] && echo "!!! This crash will not terminate the shell !!!")
|
|
|
|
-------------------------
|
|
|
|
sos!bashutils crash
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
Issuer
|
|
|
|
-> Line: $(caller)
|
|
|
|
-> Message: ${*}
|
|
|
|
|
|
|
|
Operating system
|
|
|
|
$(if [ -f "/etc/os-release" ]; then cat "/etc/os-release" | while read -r line; do echo " -> ${line}"; done; else echo "/etc/os-release does not exist"; fi)
|
|
|
|
|
|
|
|
Environment
|
|
|
|
$(for line in $(env); do echo " -> ${line}"; done)
|
|
|
|
|
|
|
|
Stacktrace
|
|
|
|
$(for index in $(seq 0 1000); do if [ -z "$(caller "${index}")" ]; then break; else echo " -> $(caller "${index}")"; fi; done)
|
|
|
|
|
|
|
|
-------------------------
|
|
|
|
sos!bashutils crash
|
|
|
|
-------------------------
|
|
|
|
$([ "${CRASH_TERMINATE}" == "false" ] && echo "!!! This crash will not terminate the shell !!!")
|
|
|
|
EOF
|
|
|
|
[ "${CRASH_TERMINATE}" == "true" ] && exit 69
|
|
|
|
}
|
|
|
|
|
2024-10-27 13:26:32 +01:00
|
|
|
# Checks
|
2024-10-27 21:52:26 +01:00
|
|
|
## Command type
|
|
|
|
## => Checks whether the provided command name is an alias.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_alias() { [[ "$(type -t "${1}")" == alias ]]; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Checks whether the provided command name is a keyword.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_keyword() { [[ "$(type -t "${1}")" == keyword ]]; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Checks whether the provided command name is a function.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_function() { [[ "$(type -t "${1}")" == function ]]; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Checks whether the provided command name is a builtin.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_builtin() { [[ "$(type -t "${1}")" == builtin ]]; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Checks whether the provided command name is a file.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_file() { [[ "$(type -t "${1}")" == file ]]; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Checks whether the provided command name is defined.
|
|
|
|
## => Arguments: <str:command name>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_command_defined() { [[ "$(type -t "${1}")" == "" ]]; }
|
2024-10-27 22:04:37 +01:00
|
|
|
## Input type
|
|
|
|
## => Checks whether the provided input is a bool.
|
|
|
|
## => Arguments: <str:input>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_input_bool() { [ "${1}" == "true" ] || [ "${1}" == "false" ]; }
|
2024-10-27 22:04:37 +01:00
|
|
|
## => Checks whether the provided input is a byte.
|
|
|
|
## => Arguments: <str:input>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_input_byte() { [[ "${1}" =~ ^[0-9]+$ ]] && [[ "${1}" -gt -1 ]] && [[ "${1}" -lt 256 ]]; }
|
2024-10-27 22:04:37 +01:00
|
|
|
## => Checks whether the provided input is a integer/number.
|
|
|
|
## => Arguments: <str:input>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_input_int() { [[ "${1}" =~ ^[0-9]+$ ]]; }
|
2024-10-27 22:04:37 +01:00
|
|
|
## => Checks whether the provided input is a char.
|
|
|
|
## => Arguments: <str:input>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function is_input_char() { [[ "${1}" =~ ^.$ ]]; }
|
2024-10-27 13:26:32 +01:00
|
|
|
|
|
|
|
# Variable definition
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Sets the specified variable to the specified value if it is undefined.
|
|
|
|
## => Arguments: <str:variable> <?:value>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function set_undefined() { [ -z "${!1}" ] && export "${1}=${2}"; }
|
2024-10-27 21:52:26 +01:00
|
|
|
## => Sets the specified variable to the specified value if it is defined.
|
|
|
|
## => Arguments: <str:variable> <?:value>
|
|
|
|
## => Returns: 0 if it is, 1 otherwise
|
2024-11-17 16:14:49 +01:00
|
|
|
function set_defined() { [ -n "${!1}" ] && export "${1}=${2}"; }
|
2024-10-27 13:26:32 +01:00
|
|
|
|
2024-10-26 22:54:26 +02:00
|
|
|
|
|
|
|
# Initialize variables
|
2024-10-27 13:26:32 +01:00
|
|
|
set_undefined "BASHUTILS_LOGLEVEL" "3"
|
2024-11-17 16:14:49 +01:00
|
|
|
set_undefined "BASHUTILS_LOGINDENTATION" ">"
|
|
|
|
set_undefined "BASHUTILS_SUBSHELL" "1"
|
2024-10-26 22:54:26 +02:00
|
|
|
|
|
|
|
|
2024-10-26 23:23:16 +02:00
|
|
|
################################
|
|
|
|
### BASHUTILS CODE ENDS HERE ###
|
|
|
|
################################
|
2024-10-27 22:04:37 +01:00
|
|
|
|