From becafeba45c3bf51de8ee3c523d19ae68dd6754a Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 9 Nov 2021 11:01:52 +0100 Subject: initial commit with basic logging --- .gitignore | 2 ++ bash-lib.sh | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ demo.sh | 47 +++++++++++++++++++++++++++++ flake.lock | 26 ++++++++++++++++ flake.nix | 27 +++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 bash-lib.sh create mode 100644 demo.sh create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..750baeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +result-* diff --git a/bash-lib.sh b/bash-lib.sh new file mode 100644 index 0000000..83819df --- /dev/null +++ b/bash-lib.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash + +set -euo pipefail +IFS=$'\n\t' + +shopt -s nullglob + +tput() { + # If tput fails, it just means less colors + command tput "$@" 2> /dev/null || true +} + +NORMAL="$(tput sgr0)" +readonly NORMAL +BOLD="$(tput bold)" +readonly BOLD + +RED="$(tput setaf 1)" +readonly RED +GREEN="$(tput setaf 2)" +readonly GREEN +YELLOW="$(tput setaf 3)" +readonly YELLOW +BLUE="$(tput setaf 4)" +readonly BLUE +PURPLE="$(tput setaf 5)" +readonly PURPLE +CYAN="$(tput setaf 6)" +readonly CYAN +WHITE="$(tput setaf 7)" +readonly WHITE + +readonly BASH_LIB_DEBUG_VAR="${BASH_LIB_NAME:-BASH}_DEBUG" + +is_debug() { + [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 1 ] +} + +is_trace() { + [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 2 ] +} + +declare -a VERBOSE_ARG +if is_debug; then + VERBOSE_ARG=("--verbose") +else + VERBOSE_ARG=() +fi +readonly VERBOSE_ARG + +echoe() { + IFS=" " echo "$@" >&2 +} + +log() { + local level="$1" + local color="$2" + local message="$3" + shift 3 + local rest="$@" + + echoe "${BOLD}${color}${level} ${WHITE}${message}${NORMAL}" "${rest[@]}" +} + +trace() { + if is_trace; then + log "Trace: " "${PURPLE}" "$@" + fi +} + +debug() { + if is_debug; then + log "Debug: " "${GREEN}" "$@" + fi +} + +info() { + log "Info: " "${CYAN}" "$@" +} + +warn() { + log "Warning:" "${YELLOW}" "$@" +} + +error() { + log "Error: " "${RED}" "$@" +} + +critical() { + error "$@" + # Useful for triggering the error trap system + false +} + +fatal() { + error "$@" + exit 1 +} diff --git a/demo.sh b/demo.sh new file mode 100644 index 0000000..256d091 --- /dev/null +++ b/demo.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +readonly BASH_LIB_NAME=DEMO +source ./bash-lib.sh + +# Set `DEMO_DEBUG=1` to enable debug messages + +trace "this is a trace message" "with additional data" +debug "this is a debugging message" "with additional data" +info "this is an informational message" "with additional data" +warn "this is a warning message" "with additional data" +error "this is an error message" "with additional data" + +# Like echo, but to stderr +echoe '------------------------------' + +if is_debug; then + info "Debugging messages:" on +else + info "Debugging messages:" off +fi + +if is_trace; then + info "Trace messages:" on +else + info "Trace messages:" off +fi + +echoe '------------------------------' + +# Use critical with `set +e` if you want to trap errors +( + function handle_error() { + info "handling error..." + } + + set +e + trap "handle_error" ERR + + critical "this is a critical error:" "this will allow some error recovery" + info "but with 'set +e', the program continues its course" +) + +echoe '------------------------------' + +info "use 'fatal()' in either case if you want to quit with an error message" +fatal "some unrecoverable error occurred, exiting" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e03bfad --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1634851050, + "narHash": "sha256-N83GlSGPJJdcqhUxSCS/WwW5pksYf3VP1M13cDRTSVA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c91f3de5adaf1de973b797ef7485e441a65b8935", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..ad3d540 --- /dev/null +++ b/flake.nix @@ -0,0 +1,27 @@ +{ + description = "A simple bash library for your scripting needs"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, flake-utils }: + with flake-utils.lib; + eachSystem allSystems (system: { + + # Convert a path to a derivation + # This is done so we don't depend on nixpkgs + packages.bash-lib = with builtins; derivation { + name = "bash-lib.sh"; + src = readFile ./bash-lib.sh; + inherit system; + + builder = "/bin/sh"; + args = [ + (toFile "builder.sh" '' + echo "$src" > $out + '') + ]; + }; + + defaultPackage = self.packages.${system}.bash-lib; + }); +} -- cgit v1.2.3