bashlink

Testable, predictable and scoped code in bash.

Usage no npm install needed!

<script type="module">
  import bashlink from 'https://cdn.skypack.dev/bashlink';
</script>

README

Project status

npm version downloads build status dependencies development dependencies peer dependencies documentation website

Use case

A bash framework to fill the gaps to write testable, predictable and scoped code in bash highly inspired by jandob's great tool rebash rebash.

Integrate bashlink into your bash script (only main entry file):

    if [ -f "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh" ]; then
        # shellcheck disable=SC1090
        source "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh"
    elif [ -f "/usr/lib/bashlink/module.sh" ]; then
        # shellcheck disable=SC1091
        source "/usr/lib/bashlink/module.sh"
    else
        echo Needed bashlink library not found 1>&2
        exit 1
    fi
    bl.module.import bashlink.logging
    # Your code comes here.

Integrate bashlink into your standalone bash script:

    declare -gr moduleName_bashlink_path="$(
        mktemp --directory --suffix -module-name-bashlink
    )/bashlink/"
    mkdir "$moduleName_bashlink_path"
    if curl \
        https://raw.githubusercontent.com/thaibault/bashlink/master/module.sh \
            >"${moduleName_bashlink_path}module.sh"
    then
        declare -gr bl_module_retrieve_remote_modules=true
        # shellcheck disable=SC1091
        source "${moduleName_bashlink_path}module.sh"
    else
        echo Needed bashlink library not found 1>&2
        rm --force --recursive "$moduleName_bashlink_path"
        exit 1
    fi
    # Your standalone code comes here

Or combine both to implement a very agnostic script.

    if [ -f "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh" ]; then
        # shellcheck disable=SC1090
        source "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh"
    elif [ -f "/usr/lib/bashlink/module.sh" ]; then
        # shellcheck disable=SC1091
        source "/usr/lib/bashlink/module.sh"
    else
        declare -gr moduleName_bashlink_path="$(
            mktemp --directory --suffix -module-name-bashlink
        )/bashlink/"
        mkdir "$moduleName_bashlink_path"
        if curl \
            https://raw.githubusercontent.com/thaibault/bashlink/master/module.sh \
                >"${moduleName_bashlink_path}module.sh"
        then
            declare -gr bl_module_retrieve_remote_modules=true
            # shellcheck disable=SC1090
            source "${moduleName_bashlink_path}/module.sh"
        else
            echo Needed bashlink library not found 1>&2
            rm --force --recursive "$moduleName_bashlink_path"
            exit 1
        fi
    fi
    # Your portable code comes here.

Best practise (entry) module pattern:

    if [ -f "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh" ]; then
        # shellcheck disable=SC1090
        source "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh"
    elif [ -f "/usr/lib/bashlink/module.sh" ]; then
        # shellcheck disable=SC1091
        source "/usr/lib/bashlink/module.sh"
    else
        declare -gr moduleName_bashlink_path="$(
            mktemp --directory --suffix -module-name-bashlink
        )/bashlink/"
        mkdir "$moduleName_bashlink_path"
        if curl \
            https://raw.githubusercontent.com/thaibault/bashlink/master/module.sh \
                >"${moduleName_bashlink_path}module.sh"
        then
            declare -gr bl_module_retrieve_remote_modules=true
            # shellcheck disable=SC1090
            source "${moduleName_bashlink_path}/module.sh"
        else
            echo Needed bashlink library not found 1>&2
            rm --force --recursive "$moduleName_bashlink_path"
            exit 1
        fi
    fi
    bl.module.import bashlink.exception
    bl.module.import bashlink.logging
    bl.module.import bashlink.tools
    alias moduleName.main=moduleName_main
    moduleName_main() {
        bl.exception.activate
        # Your entry code.
        bl.exception.deactivate
    }
    # Your module functions comes here.
    if bl.tools.is_main; then
        moduleName.main "$@"
        [ -d "$moduleName_bashlink_path" ] && \
            rm --recursive "$moduleName_bashlink_path"
        # shellcheck disable=SC2154
        [ -d "$bl_module_remote_module_cache_path" ] && \
            rm --recursive "$bl_module_remote_module_cache_path"
    fi