summary refs log tree commit diff
diff options
context:
space:
mode:
authorSingustromo <singustromo@disroot.org>2023-07-03 12:50:55 +0200
committerSingustromo <singustromo@disroot.org>2023-07-03 12:50:55 +0200
commit6fb9df04f4891aa76a1d120650f9169cf97ad48f (patch)
tree0ccce1fcf11c5befe5ea571b0d4afe011a7b404b
parentd0b41fb5266b188f6642db1af49b5ed8fe564e57 (diff)
downloadbash-framework-6fb9df04f4891aa76a1d120650f9169cf97ad48f.tar.gz
bash-framework-6fb9df04f4891aa76a1d120650f9169cf97ad48f.zip
Added System::die
  - primarily used by Framework
  - Added util/log
  - rearranged other modules
-rwxr-xr-xlib/bootstrap.sh32
-rw-r--r--lib/string/GetSpaces.sh8
-rw-r--r--lib/string/IsNumber.sh6
-rw-r--r--lib/string/SanitizeForVariable.sh4
-rw-r--r--lib/string/SlashReplacement.sh17
-rw-r--r--lib/string/UUID.sh24
-rw-r--r--lib/util/bash4.sh1
-rw-r--r--lib/util/command.sh27
-rw-r--r--lib/util/exit_codes.sh37
-rwxr-xr-xyour-script.sh6
10 files changed, 44 insertions, 118 deletions
diff --git a/lib/bootstrap.sh b/lib/bootstrap.sh
index 10593c3..1dc9559 100755
--- a/lib/bootstrap.sh
+++ b/lib/bootstrap.sh
@@ -4,9 +4,19 @@
 ### BOOTSTRAP FUNCTIONS ###
 ###########################
 
-# $1 : relative filename
+# Kills the script with previous exit code and optional error message
+System::die() {
+	local return_value="$?"
+	
+	if [ $# -gt 0 ]; then
+		(( return_value )) && printf "FATAL ERROR: %b" "$@"
+		(( ! return_value )) && printf "%b" "$@"
+	fi
+	exit "$return_value"
+}
+
 File::absolute_path() {
-	local file="$1"
+	local file="${1:?no filepath}"
 	if [ "${file::1}" == "/" ]; then
 		[ -r "$file" ] || return
 		printf "%s\n" "$file"
@@ -34,7 +44,7 @@ System::SourceFile() {
 		true; return
 	fi
 	
-	# Check syntax before
+	# Check syntax
 	/usr/bin/env bash -n "$libPath" \
 		&& builtin source "$libPath" "$@" \
 		&& _VOID_IMPORTS+=( "$libPath" )
@@ -59,8 +69,8 @@ System::ImportOne() {
 	local requestedPath="$libPath"
 
 	shift
-	[[ "$requestedPath" == './'* ]] && requestedPath="${requestedPath:2}"
-	requestedPath="${_VOID_PATH}/${requestedPath}"
+	[ "${requestedPath::2}" == './' ] && requestedPath="${requestedPath:2}"
+	requestedPath="${_VOID_ROOT}/${requestedPath}"
 
 	# try relative to parent script
 	# try with parent
@@ -76,13 +86,14 @@ System::ImportOne() {
 	System::SourcePath "${requestedPath}" "$@" \
 		|| System::SourcePath "${libPath}" "$@" \
 		|| System::SourcePath "${_VOID_LIBPATH}/${libPath}" "$@" \
-		|| System::SourcePath "${_VOID_PATH}/${libPath}" "$@"
+		|| System::SourcePath "${_VOID_ROOT}/${libPath}" "$@"
 }
 
 System::Import() {
 	local libPath
 	for libPath in "$@"; do
-		System::ImportOne "$libPath"
+		System::ImportOne "$libPath" \
+			|| System::die "Unable to import '$libPath'\n"
 	done
 }
 
@@ -90,9 +101,8 @@ System::Import() {
 ## they were initialized AFTER they were created
 ## this is the reason why we have to load files in a specific order
 System::Bootstrap() {
-	System::Import array/contains && return
-	printf "FATAL ERROR: Unable to bootstrap (missing lib directory?)\n" 1>&2
-	false; exit
+	System::Import array/contains \
+		|| System::die "Unable to bootstrap (missing lib directory?)\n"
 }
 
 ######################
@@ -107,7 +117,7 @@ set -o pipefail
 
 _VOID_LIBPATH="$(realpath "${BASH_SOURCE[0]}")"
 declare -g -r _VOID_LIBPATH="${_VOID_LIBPATH%/*}"
-declare -g -r _VOID_PATH="${_VOID_LIBPATH%/*}"
+declare -g -r _VOID_ROOT="${_VOID_LIBPATH%/*}"
 
 declare -g -a _VOID_IMPORTS
 
diff --git a/lib/string/GetSpaces.sh b/lib/string/GetSpaces.sh
deleted file mode 100644
index 77bdf7c..0000000
--- a/lib/string/GetSpaces.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-String::GetSpaces() {
-  local howMany="$1"
-
-  if [[ "$howMany" -gt 0 ]]
-  then
-    ( printf "%*s" "$howMany" )
-  fi
-}
diff --git a/lib/string/IsNumber.sh b/lib/string/IsNumber.sh
deleted file mode 100644
index 7537c71..0000000
--- a/lib/string/IsNumber.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-String::IsNumber() {
-  local input="$1"
-  [ -n "$input" ] || return
-  local regex='^-?[0-9]+([.][0-9]+)?$'
-  [[ "$input" =~ $regex ]] || return
-}
diff --git a/lib/string/SanitizeForVariable.sh b/lib/string/SanitizeForVariable.sh
deleted file mode 100644
index 044b9d4..0000000
--- a/lib/string/SanitizeForVariable.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-String::SanitizeForVariableName() {
-  local type="$1"
-  echo "${type//[^a-zA-Z0-9]/_}"
-}
diff --git a/lib/string/SlashReplacement.sh b/lib/string/SlashReplacement.sh
deleted file mode 100644
index 7922d7a..0000000
--- a/lib/string/SlashReplacement.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-String::ReplaceSlashes() {
-  local stringToMark="$1"
-
-  # Workaround for a Bash bug that causes string replacement to fail when a \ is in the string
-  local slash="\\"
-  local slashReplacement='_%SLASH%_'
-  echo "${stringToMark/$slash$slash/$slashReplacement}"
-}
-
-String::RestoreSlashes() {
-  local stringToMark="$1"
-
-  # Workaround for a Bash bug that causes string replacement to fail when a \ is in the string
-  local slash="\\"
-  local slashReplacement='_%SLASH%_'
-  echo "${stringToMark/$slashReplacement/$slash}"
-}
diff --git a/lib/string/UUID.sh b/lib/string/UUID.sh
deleted file mode 100644
index 29cbee0..0000000
--- a/lib/string/UUID.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-String::GenerateUUID() {
-  ## https://gist.github.com/markusfisch/6110640
-  local N B C='89ab'
-
-  for (( N=0; N < 16; ++N ))
-  do
-    B=$(( $RANDOM%256 ))
-
-    case $N in
-      6)
-        printf '4%x' $(( B%16 ))
-      ;;
-      8)
-        printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%16 ))
-      ;;
-      3 | 5 | 7 | 9)
-        printf '%02x-' $B
-      ;;
-      *)
-        printf '%02x' $B
-      ;;
-    esac
-  done
-}
diff --git a/lib/util/bash4.sh b/lib/util/bash4.sh
deleted file mode 100644
index 75f33b7..0000000
--- a/lib/util/bash4.sh
+++ /dev/null
@@ -1 +0,0 @@
-[[ "${BASH_VERSINFO[0]}" -lt 4 ]] && echo "The module you are trying to load requires bash >= 4" && exit 1 || true
diff --git a/lib/util/command.sh b/lib/util/command.sh
index e59afb8..1f5a3ae 100644
--- a/lib/util/command.sh
+++ b/lib/util/command.sh
@@ -1,40 +1,19 @@
 # no dependencies
 
-Command::GetType() {
+Command::get_type() {
   local name="$1"
   local typeMatch=$(type -t "$name" 2> /dev/null || true)
   echo "$typeMatch"
 }
 
-Command::Exists(){
+Command::exists(){
   local name="$1"
   local typeMatch=$(Command::GetType "$name")
   [[ "$typeMatch" == "alias" || "$typeMatch" == "function" || "$typeMatch" == "builtin" ]]
 }
 
-Alias::Exists(){
+Alias::exists(){
   local name="$1"
   local typeMatch=$(Command::GetType "$name")
   [[ "$typeMatch" == "alias" ]]
 }
-
-Function::Exists(){
-  local name="$1"
-  declare -f "$name" &> /dev/null
-}
-
-Function::GetAllStartingWith() {
-  local startsWith="$1"
-  compgen -A 'function' "$startsWith" || true
-}
-
-Function::InjectCode() {
-  local functionName="$1"
-  local injectBefore="$2"
-  local injectAfter="$3"
-  local body=$(declare -f "$functionName")
-  body="${body#*{}" # trim start
-  body="${body%\}}" # trim end
-  local enter=$'\n'
-  eval "${functionName}() { ${enter}${injectBefore}${body}${injectAfter}${enter} }"
-}
diff --git a/lib/util/exit_codes.sh b/lib/util/exit_codes.sh
index e9f322a..2ee1895 100644
--- a/lib/util/exit_codes.sh
+++ b/lib/util/exit_codes.sh
@@ -1,24 +1,21 @@
 #!/usr/bin/env bash
-#
-# exits
-#
+# exit codes
 # Those values are come from /usr/include/sysexits.h
 #
 
-# successful termination
-Util_ExitCode_OK=0
-Util_ExitCode_USAGE=64  # command line usage error
-Util_ExitCode_DATAERR=65  # data format error
-Util_ExitCode_NOINPUT=66  # cannot open input
-Util_ExitCode_NOUSER=67  # addressee unknown
-Util_ExitCode_NOHOST=68  # host name unknown
-Util_ExitCode_UNAVAILABLE=69  # service unavailable
-Util_ExitCode_SOFTWARE=70  # internal software error
-Util_ExitCode_OSERR=71  # system error (e.g., can't fork)
-Util_ExitCode_OSFILE=72  # critical OS file missing
-Util_ExitCode_CANTCREAT=73  # can't create (user) output file
-Util_ExitCode_IOERR=74  # input/output error
-Util_ExitCode_TEMPFAIL=75  # temp failure; user is invited to retry
-Util_ExitCode_PROTOCOL=76  # remote error in protocol
-Util_ExitCode_NOPERM=77  # permission denied
-Util_ExitCode_CONFIG=78  # configuration error
+EX_OK=0 # successful termination
+EX_USAGE=64  # command line usage error
+EX_DATAERR=65  # data format error
+EX_NOINPUT=66  # cannot open input
+EX_NOUSER=67  # addressee unknown
+EX_NOHOST=68  # host name unknown
+EX_UNAVAILABLE=69  # service unavailable
+EX_SOFTWARE=70  # internal software error
+EX_OSERR=71  # system error (e.g., can't fork)
+EX_OSFILE=72  # critical OS file missing
+EX_CANTCREAT=73  # can't create (user) output file
+EX_IOERR=74  # input/output error
+EX_TEMPFAIL=75  # temp failure; user is invited to retry
+EX_PROTOCOL=76  # remote error in protocol
+EX_NOPERM=77  # permission denied
+EX_CONFIG=78  # configuration error
diff --git a/your-script.sh b/your-script.sh
index 82c3ccd..2ba458d 100755
--- a/your-script.sh
+++ b/your-script.sh
@@ -6,11 +6,11 @@ readonly workdir="${workdir%/*}"
 . "$workdir"/lib/bootstrap.sh
 
 ## MAIN EXAMPLES ##
-import string/IsNumber
-# Files not in dir/ can also be imported:
+import util/log
+# Files not in lib/ can also be imported:
 #  import my_file.sh
 #  import my_dir/my_file.sh
 
-printf "imported: %s\n" "${_VOID_IMPORTS[@]}"
+Util::log "imports:\n" "${_VOID_IMPORTS[@]}"
 
 ## YOUR CODE GOES HERE ##