Skip to content

Commit affa474

Browse files
committed
dx: Add hookah on Bake init
1 parent 099e377 commit affa474

File tree

3 files changed

+120
-45
lines changed

3 files changed

+120
-45
lines changed

.hooks/.hookah/lib.sh

+75-40
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# @brief Hookah: An elegantly minimal solution for Git hooks
66
# @description Hookah streamlines the process of managing Git hooks. This file is a
77
# library of functions that can easily be used by hooks written in Bash. Use it by
8-
# prepending your script with the following
8+
# prepending your hook script with the following
99
#
1010
# ```bash
1111
# #!/usr/bin/env bash
@@ -27,42 +27,66 @@ fi
2727
hookah.init() {
2828
set -Eeo pipefail
2929
shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
30-
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' LC_MONETARY='C' \
31-
LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' LC_TELEPHONE='C' \
32-
LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
30+
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' \
31+
LC_MONETARY='C' LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' \
32+
LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
3333
trap '__hookah_trap_err' 'ERR'
3434

3535
while [ ! -d '.git' ] && [ "$PWD" != / ]; do
3636
if ! cd ..; then
37-
__hookah_die "Failed to cd to nearest Git repository"
37+
__hookah_internal_die "Failed to cd to nearest Git repository"
3838
fi
3939
done
4040
if [ "$PWD" = / ]; then
41-
__hookah_die "Failed to cd to nearest Git repository"
41+
__hookah_internal_die "Failed to cd to nearest Git repository"
4242
fi
4343

4444
# Prevent any possibilities of 'stdin in is not a tty'
4545
if ! exec </dev/tty; then
46-
__hookah_die "Failed to redirect tty to standard input"
46+
__hookah_internal_warn "Failed to redirect tty to standard input"
4747
fi
4848

49-
__hookah_print "Running ${BASH_SOURCE[1]##*/}"
50-
49+
__hookah_internal_info "Running ${BASH_SOURCE[1]##*/}"
5150
}
5251

5352
# @description Prints a command before running it
54-
# #args $@ Command to execute
53+
# @arg $@ Command to execute
5554
hookah.run() {
56-
printf '%s\n' "Hookah: Running command: '$*'"
55+
__hookah_exec "$*"
5756
"$@"
5857
}
5958

6059
# @description Prints a command before running it. But, if the command fails, do not abort execution
61-
# @args $@ Command to execute
60+
# @arg $@ Command to execute
6261
hookah.run_allow_fail() {
6362
if ! hookah.run "$@"; then
64-
printf '%s\n' "Hookah: Command failed"
63+
hookah.die 'Command failed'
64+
fi
65+
}
66+
67+
# @description Prints `$1` formatted as an error and the stacktrace to standard error,
68+
# then exits with code 1
69+
# @arg $1 string Text to print
70+
hookah.die() {
71+
if [ -n "$1" ]; then
72+
__hookah_internal_error "$1. Exiting" 'Hookah'
73+
else
74+
__hookah_internal_error 'Exiting' 'Hookah'
6575
fi
76+
77+
exit 1
78+
}
79+
80+
# @description Prints `$1` formatted as a warning to standard error
81+
# @arg $1 string Text to print
82+
hookah.warn() {
83+
__hookah_internal_warn "$1" 'Hookah'
84+
}
85+
86+
# @description Prints `$1` formatted as information to standard output
87+
# @arg $1 string Text to print
88+
hookah.info() {
89+
__hookah_internal_info "$1" 'Hookah'
6690
}
6791

6892
# @description Scans environment variables to determine if script is in a CI environment
@@ -72,6 +96,7 @@ hookah.run_allow_fail() {
7296
hookah.is_ci() {
7397
unset -v REPLY; REPLY=
7498

99+
# List from 'https://github.com/watson/ci-info/blob/master/vendors.json'
75100
if [[ -v 'APPVEYOR' ]]; then
76101
REPLY='AppVeyor'
77102
elif [[ -v 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' ]]; then
@@ -164,48 +189,58 @@ __hookah_is_color() {
164189
}
165190

166191
# @internal
167-
__hookah_internal_error() {
168-
printf '%s\n' "Internal Error: $1" >&2
192+
__hookah_exec() {
193+
if __hookah_is_color; then
194+
printf "\033[1mHookah \033[1m[exec]:\033[0m %s\n" "$*"
195+
else
196+
printf "Hookah [exec]: %s\n" "$*"
197+
fi
169198
}
170199

171200
# @internal
172-
__hookah_die() {
173-
__hookah_error "$1"
201+
__hookah_internal_die() {
202+
__hookah_internal_error "$1"
174203
exit 1
175204
}
176205

177206
# @internal
178-
__hookah_error() {
179-
printf '%s\n' "Hookah: lib.sh: Error: $1. Exiting"
180-
exit 1
181-
}
207+
__hookah_internal_error() {
208+
local str="${2:-"Hookah (internal)"}"
182209

183-
# @internal
184-
__hookah_print() {
185-
printf '%s\n' "Hookah: $1"
186-
}
210+
if __hookah_is_color; then
211+
printf "\033[1;31m\033[1m$str \033[1m[error]:\033[0m %s\n" "$1"
212+
else
213+
printf "$str [error]: %s\n" "$1"
214+
fi
215+
} >&2
187216

188217
# @internal
189-
__hookah_trap_err() {
190-
local error_code=$?
218+
__hookah_internal_warn() {
219+
local str="${2:-"Hookah (internal)"}"
191220

192-
__hookah_internal_error "Your hook did not exit successfully"
193-
__hookah_print_stacktrace
194-
195-
exit $error_code
221+
if __hookah_is_color; then
222+
printf "\033[1;33m\033[1m$str \033[1m[warn]:\033[0m %s\n" "$1"
223+
else
224+
printf "$str [warn]: %s\n" "$1"
225+
fi
196226
} >&2
197227

198228
# @internal
199-
__hookah_print_stacktrace() {
229+
__hookah_internal_info() {
230+
local str="${2:-"Hookah (internal)"}"
231+
200232
if __hookah_is_color; then
201-
printf '\033[4m%s\033[0m\n' 'Stacktrace:'
233+
printf "\033[0;36m\033[1m$str \033[1m[info]:\033[0m %s\n" "$1"
202234
else
203-
printf '%s\n' 'Stacktrace:'
235+
printf "$str [info]: %s\n" "$1"
204236
fi
237+
}
205238

206-
local i=
207-
for ((i=0; i<${#FUNCNAME[@]}-1; ++i)); do
208-
local __bash_source="${BASH_SOURCE[$i]}"; __bash_source=${__bash_source##*/}
209-
printf '%s\n' " in ${FUNCNAME[$i]} ($__bash_source:${BASH_LINENO[$i-1]})"
210-
done; unset -v i __bash_source
211-
} >&2
239+
# @internal
240+
__hookah_trap_err() {
241+
local error_code=$?
242+
243+
__hookah_internal_error "Your hook did not exit successfully (exit code $error_code)"
244+
245+
exit $error_code
246+
}

Bakefile.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# shellcheck shell=bash
22

3+
task.init() {
4+
hookah refresh
5+
}
6+
37
task.test() {
48
bats tests
59
}

docs/api.md

+41-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ Core functions for any Bash program
1313
* [core.err_set()](#coreerr_set)
1414
* [core.err_clear()](#coreerr_clear)
1515
* [core.err_exists()](#coreerr_exists)
16+
* [core.panic()](#corepanic)
1617
* [core.print_stacktrace()](#coreprint_stacktrace)
18+
* [core.print_error_fn()](#coreprint_error_fn)
19+
* [core.print_warn_fn()](#coreprint_warn_fn)
20+
* [core.print_info_fn()](#coreprint_info_fn)
21+
* [core.print_die()](#coreprint_die)
1722
* [core.print_error()](#coreprint_error)
1823
* [core.print_warn()](#coreprint_warn)
1924
* [core.print_info()](#coreprint_info)
20-
* [core.panic()](#corepanic)
2125
* [core.should_output_color()](#coreshould_output_color)
2226
* [core.get_package_info()](#coreget_package_info)
2327
* [core.init()](#coreinit)
@@ -127,6 +131,10 @@ _does_ exist
127131

128132
_Function has no arguments._
129133

134+
### core.panic()
135+
136+
Use when a serious fault occurs. It will print the current ERR (if it exists)
137+
130138
### core.print_stacktrace()
131139

132140
Prints stacktrace
@@ -144,6 +152,38 @@ core.trap_add 'err_handler' ERR
144152

145153
_Function has no arguments._
146154

155+
### core.print_error_fn()
156+
157+
Print an error message to standard error
158+
159+
#### Arguments
160+
161+
* **$1** (string): message
162+
163+
### core.print_warn_fn()
164+
165+
Print a warning message to standard error
166+
167+
#### Arguments
168+
169+
* **$1** (string): message
170+
171+
### core.print_info_fn()
172+
173+
Print an informative message to standard output
174+
175+
#### Arguments
176+
177+
* **$1** (string): message
178+
179+
### core.print_die()
180+
181+
Print a error message to standard error and die
182+
183+
#### Arguments
184+
185+
* **$1** (string): message
186+
147187
### core.print_error()
148188

149189
Print an error message to standard error
@@ -168,10 +208,6 @@ Print an informative message to standard output
168208

169209
* **$1** (string): message
170210

171-
### core.panic()
172-
173-
Use when a serious fault occurs. It will print the current ERR (if it exists)
174-
175211
### core.should_output_color()
176212

177213
Determine if color should be printed. Note that this doesn't

0 commit comments

Comments
 (0)