]> git.wincent.com - wincent.git/blob - bin/common
refactor(fig): use Node v13 so that we can ES modules natively
[wincent.git] / bin / common
1 #!/bin/bash
2
3 # https://stackoverflow.com/a/2684300
4 if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
5   echo "[error] script \"$0\" is intended to be sourced, not executed directly"
6   exit 1
7 fi
8
9 # Get directory of current script:
10 #
11 #     https://stackoverflow.com/a/12694189/2103996
12
13 # and convert to absolute path:
14 #
15 #     https://stackoverflow.com/a/4045350/2103996
16 #
17 REPO_ROOT=$(cd "${BASH_SOURCE%/*}/.." && pwd)
18
19 BIN_DIR="$REPO_ROOT/bin"
20
21 export PATH="$BIN_DIR":$PATH
22
23 # https://stackoverflow.com/a/5947802/2103996
24 BOLD='\033[0;1m'
25 GREEN='\033[1;32m'
26 PURPLE='\033[1;35m'
27 RED='\033[1;31m'
28 RESET='\033[0m'
29 YELLOW='\033[1;33m'
30
31 # No associative arrays in Bash < v4, so can't do:
32 #
33 #   declare -A LOG_LEVEL
34 #
35 LOG_LEVEL_EMERGENCY=0
36 LOG_LEVEL_ALERT=1
37 LOG_LEVEL_CRITICAL=2
38 LOG_LEVEL_ERROR=3
39 LOG_LEVEL_WARNING=4
40 LOG_LEVEL_NOTICE=5
41 LOG_LEVEL_INFO=6
42 LOG_LEVEL_DEBUG=7
43
44 LOG_LEVEL=$LOG_LEVEL_INFO
45
46 for VAR in "$@"
47 do
48   if [[ $VAR = "--debug" ]]; then
49     LOG_LEVEL=$LOG_LEVEL_DEBUG
50   elif [[ $VAR = "--quiet" || $VAR = "-q" ]]; then
51     LOG_LEVEL=$LOG_LEVEL_ERROR
52   fi
53 done
54
55 log_debug() {
56   if [[ $LOG_LEVEL -ge $LOG_LEVEL_DEBUG ]]; then
57     echo -e "${PURPLE}[debug]  ${RESET} $*" > /dev/stderr
58   fi
59 }
60
61 log_error() {
62   if [[ $LOG_LEVEL -ge $LOG_LEVEL_ERROR ]]; then
63     echo -e "${RED}[error]  ${RESET} $*" > /dev/stderr
64   fi
65 }
66
67 log_info() {
68   if [[ $LOG_LEVEL -ge $LOG_LEVEL_INFO ]]; then
69     echo -e "${BOLD}[info]   ${RESET} $*" > /dev/stderr
70   fi
71 }
72
73 log_notice() {
74   if [[ $LOG_LEVEL -ge $LOG_LEVEL_NOTICE ]]; then
75     echo -e "${GREEN}[notice] ${RESET} $*" > /dev/stderr
76   fi
77 }
78
79 log_warn() {
80   if [[ $LOG_LEVEL -ge $LOG_LEVEL_WARNING ]]; then
81     echo -e "${YELLOW}[warning]${RESET} $*" > /dev/stderr
82   fi
83 }