summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--bash-lib.sh98
-rw-r--r--demo.sh47
-rw-r--r--flake.lock26
-rw-r--r--flake.nix27
5 files changed, 200 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..750baeb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
1result
2result-*
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 @@
1#!/usr/bin/env bash
2
3set -euo pipefail
4IFS=$'\n\t'
5
6shopt -s nullglob
7
8tput() {
9 # If tput fails, it just means less colors
10 command tput "$@" 2> /dev/null || true
11}
12
13NORMAL="$(tput sgr0)"
14readonly NORMAL
15BOLD="$(tput bold)"
16readonly BOLD
17
18RED="$(tput setaf 1)"
19readonly RED
20GREEN="$(tput setaf 2)"
21readonly GREEN
22YELLOW="$(tput setaf 3)"
23readonly YELLOW
24BLUE="$(tput setaf 4)"
25readonly BLUE
26PURPLE="$(tput setaf 5)"
27readonly PURPLE
28CYAN="$(tput setaf 6)"
29readonly CYAN
30WHITE="$(tput setaf 7)"
31readonly WHITE
32
33readonly BASH_LIB_DEBUG_VAR="${BASH_LIB_NAME:-BASH}_DEBUG"
34
35is_debug() {
36 [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 1 ]
37}
38
39is_trace() {
40 [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 2 ]
41}
42
43declare -a VERBOSE_ARG
44if is_debug; then
45 VERBOSE_ARG=("--verbose")
46else
47 VERBOSE_ARG=()
48fi
49readonly VERBOSE_ARG
50
51echoe() {
52 IFS=" " echo "$@" >&2
53}
54
55log() {
56 local level="$1"
57 local color="$2"
58 local message="$3"
59 shift 3
60 local rest="$@"
61
62 echoe "${BOLD}${color}${level} ${WHITE}${message}${NORMAL}" "${rest[@]}"
63}
64
65trace() {
66 if is_trace; then
67 log "Trace: " "${PURPLE}" "$@"
68 fi
69}
70
71debug() {
72 if is_debug; then
73 log "Debug: " "${GREEN}" "$@"
74 fi
75}
76
77info() {
78 log "Info: " "${CYAN}" "$@"
79}
80
81warn() {
82 log "Warning:" "${YELLOW}" "$@"
83}
84
85error() {
86 log "Error: " "${RED}" "$@"
87}
88
89critical() {
90 error "$@"
91 # Useful for triggering the error trap system
92 false
93}
94
95fatal() {
96 error "$@"
97 exit 1
98}
diff --git a/demo.sh b/demo.sh
new file mode 100644
index 0000000..256d091
--- /dev/null
+++ b/demo.sh
@@ -0,0 +1,47 @@
1#!/usr/bin/env bash
2
3readonly BASH_LIB_NAME=DEMO
4source ./bash-lib.sh
5
6# Set `DEMO_DEBUG=1` to enable debug messages
7
8trace "this is a trace message" "with additional data"
9debug "this is a debugging message" "with additional data"
10info "this is an informational message" "with additional data"
11warn "this is a warning message" "with additional data"
12error "this is an error message" "with additional data"
13
14# Like echo, but to stderr
15echoe '------------------------------'
16
17if is_debug; then
18 info "Debugging messages:" on
19else
20 info "Debugging messages:" off
21fi
22
23if is_trace; then
24 info "Trace messages:" on
25else
26 info "Trace messages:" off
27fi
28
29echoe '------------------------------'
30
31# Use critical with `set +e` if you want to trap errors
32(
33 function handle_error() {
34 info "handling error..."
35 }
36
37 set +e
38 trap "handle_error" ERR
39
40 critical "this is a critical error:" "this will allow some error recovery"
41 info "but with 'set +e', the program continues its course"
42)
43
44echoe '------------------------------'
45
46info "use 'fatal()' in either case if you want to quit with an error message"
47fatal "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 @@
1{
2 "nodes": {
3 "flake-utils": {
4 "locked": {
5 "lastModified": 1634851050,
6 "narHash": "sha256-N83GlSGPJJdcqhUxSCS/WwW5pksYf3VP1M13cDRTSVA=",
7 "owner": "numtide",
8 "repo": "flake-utils",
9 "rev": "c91f3de5adaf1de973b797ef7485e441a65b8935",
10 "type": "github"
11 },
12 "original": {
13 "owner": "numtide",
14 "repo": "flake-utils",
15 "type": "github"
16 }
17 },
18 "root": {
19 "inputs": {
20 "flake-utils": "flake-utils"
21 }
22 }
23 },
24 "root": "root",
25 "version": 7
26}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..ad3d540
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,27 @@
1{
2 description = "A simple bash library for your scripting needs";
3
4 inputs.flake-utils.url = "github:numtide/flake-utils";
5
6 outputs = { self, flake-utils }:
7 with flake-utils.lib;
8 eachSystem allSystems (system: {
9
10 # Convert a path to a derivation
11 # This is done so we don't depend on nixpkgs
12 packages.bash-lib = with builtins; derivation {
13 name = "bash-lib.sh";
14 src = readFile ./bash-lib.sh;
15 inherit system;
16
17 builder = "/bin/sh";
18 args = [
19 (toFile "builder.sh" ''
20 echo "$src" > $out
21 '')
22 ];
23 };
24
25 defaultPackage = self.packages.${system}.bash-lib;
26 });
27}