diff options
author | Minijackson <minijackson@riseup.net> | 2021-11-09 11:01:52 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2021-11-09 11:01:52 +0100 |
commit | becafeba45c3bf51de8ee3c523d19ae68dd6754a (patch) | |
tree | 17ecabea012b1bec94172eb921e71a15ecd1aaee | |
download | bash-lib-becafeba45c3bf51de8ee3c523d19ae68dd6754a.tar.gz bash-lib-becafeba45c3bf51de8ee3c523d19ae68dd6754a.zip |
initial commit with basic logging
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | bash-lib.sh | 98 | ||||
-rw-r--r-- | demo.sh | 47 | ||||
-rw-r--r-- | flake.lock | 26 | ||||
-rw-r--r-- | flake.nix | 27 |
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 @@ | |||
1 | result | ||
2 | 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 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | |||
3 | set -euo pipefail | ||
4 | IFS=$'\n\t' | ||
5 | |||
6 | shopt -s nullglob | ||
7 | |||
8 | tput() { | ||
9 | # If tput fails, it just means less colors | ||
10 | command tput "$@" 2> /dev/null || true | ||
11 | } | ||
12 | |||
13 | NORMAL="$(tput sgr0)" | ||
14 | readonly NORMAL | ||
15 | BOLD="$(tput bold)" | ||
16 | readonly BOLD | ||
17 | |||
18 | RED="$(tput setaf 1)" | ||
19 | readonly RED | ||
20 | GREEN="$(tput setaf 2)" | ||
21 | readonly GREEN | ||
22 | YELLOW="$(tput setaf 3)" | ||
23 | readonly YELLOW | ||
24 | BLUE="$(tput setaf 4)" | ||
25 | readonly BLUE | ||
26 | PURPLE="$(tput setaf 5)" | ||
27 | readonly PURPLE | ||
28 | CYAN="$(tput setaf 6)" | ||
29 | readonly CYAN | ||
30 | WHITE="$(tput setaf 7)" | ||
31 | readonly WHITE | ||
32 | |||
33 | readonly BASH_LIB_DEBUG_VAR="${BASH_LIB_NAME:-BASH}_DEBUG" | ||
34 | |||
35 | is_debug() { | ||
36 | [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 1 ] | ||
37 | } | ||
38 | |||
39 | is_trace() { | ||
40 | [ "${!BASH_LIB_DEBUG_VAR:-0}" -ge 2 ] | ||
41 | } | ||
42 | |||
43 | declare -a VERBOSE_ARG | ||
44 | if is_debug; then | ||
45 | VERBOSE_ARG=("--verbose") | ||
46 | else | ||
47 | VERBOSE_ARG=() | ||
48 | fi | ||
49 | readonly VERBOSE_ARG | ||
50 | |||
51 | echoe() { | ||
52 | IFS=" " echo "$@" >&2 | ||
53 | } | ||
54 | |||
55 | log() { | ||
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 | |||
65 | trace() { | ||
66 | if is_trace; then | ||
67 | log "Trace: " "${PURPLE}" "$@" | ||
68 | fi | ||
69 | } | ||
70 | |||
71 | debug() { | ||
72 | if is_debug; then | ||
73 | log "Debug: " "${GREEN}" "$@" | ||
74 | fi | ||
75 | } | ||
76 | |||
77 | info() { | ||
78 | log "Info: " "${CYAN}" "$@" | ||
79 | } | ||
80 | |||
81 | warn() { | ||
82 | log "Warning:" "${YELLOW}" "$@" | ||
83 | } | ||
84 | |||
85 | error() { | ||
86 | log "Error: " "${RED}" "$@" | ||
87 | } | ||
88 | |||
89 | critical() { | ||
90 | error "$@" | ||
91 | # Useful for triggering the error trap system | ||
92 | false | ||
93 | } | ||
94 | |||
95 | fatal() { | ||
96 | error "$@" | ||
97 | exit 1 | ||
98 | } | ||
@@ -0,0 +1,47 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | |||
3 | readonly BASH_LIB_NAME=DEMO | ||
4 | source ./bash-lib.sh | ||
5 | |||
6 | # Set `DEMO_DEBUG=1` to enable debug messages | ||
7 | |||
8 | trace "this is a trace message" "with additional data" | ||
9 | debug "this is a debugging message" "with additional data" | ||
10 | info "this is an informational message" "with additional data" | ||
11 | warn "this is a warning message" "with additional data" | ||
12 | error "this is an error message" "with additional data" | ||
13 | |||
14 | # Like echo, but to stderr | ||
15 | echoe '------------------------------' | ||
16 | |||
17 | if is_debug; then | ||
18 | info "Debugging messages:" on | ||
19 | else | ||
20 | info "Debugging messages:" off | ||
21 | fi | ||
22 | |||
23 | if is_trace; then | ||
24 | info "Trace messages:" on | ||
25 | else | ||
26 | info "Trace messages:" off | ||
27 | fi | ||
28 | |||
29 | echoe '------------------------------' | ||
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 | |||
44 | echoe '------------------------------' | ||
45 | |||
46 | info "use 'fatal()' in either case if you want to quit with an error message" | ||
47 | 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 @@ | |||
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 | } | ||