Skip to content

Commit

Permalink
make versioning possible and return early on error
Browse files Browse the repository at this point in the history
  • Loading branch information
georglauterbach committed Apr 20, 2024
1 parent f94ed7a commit 6912c92
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 56 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ You may also use `libbash` without cloning the repository. To do so, run:
source <(curl -qsSfL https://raw.githubusercontent.com/georglauterbach/libbash/main/load) --online 'log'
```
---
You may optionally specify a version to acquire modules from a specific release. The version follows the [Semantic Versioning](https://semver.org/lang/de/) pattern:
```bash
source <(curl -qsSfL https://raw.githubusercontent.com/georglauterbach/libbash/main/load) --online --version '6.1.0' 'log'
```
The [Releases](https://github.com/georglauterbach/libbash/releases) page, you can see which versions you can use. Make sure the module exists for your version.
### Environment Variables
#### `__LIBBASH__*`
Expand Down
134 changes: 78 additions & 56 deletions load
Original file line number Diff line number Diff line change
Expand Up @@ -117,67 +117,80 @@ fi # if ! ${__LIBBASH__IS_LOADED:-false}
# This function calls the initialization routines for `libbash`
# in proper order.
function __libbash__main() {
# ### Load a `libbash` Module
#
# Checks if the given modules is present (exits unsuccessfully)
# otherwise. Then loads the module by sourcing it.
function libbash__load_module() {
[[ -z ${1+set} ]] && __libbash__exit_with_error_and_callstack 'no module provided' 2

local MODULE_PATH
MODULE_PATH="${LIBBASH_DIRECTORY}/modules/${1}.sh"

if [[ -e "${MODULE_PATH}" ]]
then
if [[ ${LIBBASH_LOADED_MODULES[*]} =~ ${1} ]]
then
__libbash__exit_with_error_and_callstack "module '${1}' loaded more than once" 3
fi

# shellcheck source=/dev/null
source "${MODULE_PATH}"
LIBBASH_LOADED_MODULES+=("${1}")
else
__libbash__exit_with_error_and_callstack "module '${1:-}' not found" 4
fi
function parse_arguments() {
while [[ ${#} -gt 0 ]]; do
case "${1:-}" in
( '--version' )
LIBBASH_VERSION="v${2:?Version is required when specifying \'--version\'}"
shift 2
;;

( '--online' )
LIBBASH_ONLINE=1
shift 1
;;

( * )
MODULES+=("${1}")
shift 1
;;
esac
done
}

# ### Go Through Arguments
#
# Iterates through arguments and calls `libbash__load_module` to
# load the corresponding module. If the `--online` flag was supplied,
# this function `source`s the files from the GitHub repository via
# `curl`.
function libbash__source_files() {
local MODULE
if [[ ${1:-} == --online ]]
then
shift 1
for MODULE in "${@}"
do
# Iterates through arguments to load the corresponding module.
# If the `--online` flag was supplied, this function `source`s
# the files from the GitHub repository via `curl`.
function source_files() {
local MODULE TMP_DIR

TMP_DIR=$(mktemp --directory)
trap 'rm -rf "${TMP_DIR}"' ERR

for MODULE in "${MODULES[@]}"; do
if [[ ${LIBBASH_LOADED_MODULES[*]} =~ ${MODULE} ]]; then
__libbash__exit_with_error_and_callstack "module '${MODULE}' loaded more than once" 3
fi

if [[ ${LIBBASH_ONLINE} -eq 1 ]]; then
if ! curl --silent --show-error --fail --location -o "${TMP_DIR}/${MODULE}" \
"https://raw.githubusercontent.com/georglauterbach/libbash/${LIBBASH_VERSION}/modules/${MODULE}.sh"; then
__libbash__show_error "Curl did not succeed. Wrong version? Does the module exist?"
return 5
fi

# shellcheck source=/dev/null
source <(curl -qsSfL "https://raw.githubusercontent.com/georglauterbach/libbash/main/modules/${MODULE}.sh")
done
else
for MODULE in "${@}"
do
libbash__load_module "${MODULE}"
done
fi
source "${TMP_DIR}/${MODULE}"
else
local MODULE_PATH
MODULE_PATH="${LIBBASH_DIRECTORY}/modules/${MODULE}.sh"

if [[ -e "${MODULE_PATH}" ]]; then
# shellcheck source=/dev/null
source "${MODULE_PATH}"
else
__libbash__exit_with_error_and_callstack "module '${MODULE}' not found" 4
fi
fi

LIBBASH_LOADED_MODULES+=("${MODULE}")
done

rm -rf "${TMP_DIR}"
}

# ### Fallback `log`
#
# If the `log` module was not sourced, provide a fallback
# `log` implementation, but only for error messages.
function libbash__setup_default_notify_error() {
function setup_default_notify_error() {
LIBBASH_LOG_ONLY_ERROR_FALLBACK=1
function log() {
if [[ ${1:-} != 'error' ]]
then
__libbash__exit_with_error_and_callstack \
"log module was not loaded but 'log' was called" \
"with log level not 'error'" \
"(arguments were: ${*})"
if [[ ${1:-} != 'error' ]]; then
__libbash__exit_with_error_and_callstack \
"log module was not loaded but 'log' was called with log level not 'error' (arguments were: ${*})"
return 1
fi

Expand All @@ -190,13 +203,20 @@ function __libbash__main() {
# ! all modules are sourced to satisfy shellcheck
# ! in applications that use `libbash`
unset LIBBASH_LOADED_MODULES
export CRI LOG_LEVEL LIBBASH_DIRECTORY SCRIPT LIBBASH_LOADED_MODULES
export SCRIPT LOG_LEVEL CRI
export LIBBASH_DIRECTORY LIBBASH_VERSION LIBBASH_LOG_ONLY_ERROR_FALLBACK LIBBASH_LOADED_MODULES

SCRIPT='libbash init'
LOG_LEVEL=${LOG_LEVEL:-info}

LIBBASH_DIRECTORY="$(realpath -eL "$(dirname "${BASH_SOURCE[0]}")")"
SCRIPT='libbash init'
LIBBASH_LOG_ONLY_ERROR_FALLBACK=0
LIBBASH_VERSION='main'

libbash__source_files "${@}"
local LIBBASH_ONLINE=0 MODULES=()

parse_arguments "${@}" || return ${?}
source_files || return ${?}

# this has to be set up last
if [[ ${LIBBASH_LOADED_MODULES[*]} != *log* ]]; then
Expand All @@ -207,7 +227,9 @@ function __libbash__main() {
log 'trace' "Finished 'libbash' initialization"
}

__libbash__main "${@}"
unset __libbash__main

SCRIPT='prompt or not inside a function'
if __libbash__main "${@}"; then
unset __libbash__main
SCRIPT='prompt or not inside a function'
else
( exit ${?} ; )
fi

0 comments on commit 6912c92

Please sign in to comment.