summaryrefslogtreecommitdiffstats
path: root/bash-lib.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bash-lib.sh')
-rw-r--r--bash-lib.sh98
1 files changed, 98 insertions, 0 deletions
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}