-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions
138 lines (108 loc) · 2.58 KB
/
functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
# Version: 0.2.0
# https://github.com/AndiDittrich/bash-functions
# ----------------------------------
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
# ----------------------------------
_print_colorized(){
# extract color
STYLE="$1"
# drop from argument list
shift
# print colorized message
echo -e "${STYLE}$*\e[0m"
}
print_heading(){
# title set ?
[ -z "$1" ] && return
# default style: bold bright blue
STYLE='\x1B[34m\x1B[1m'
# style set ?
[ ! -z "$2" ] && {
STYLE="$2"
}
# std title
TITLE="${1}"
# print heading
_print_colorized "$STYLE" \
"\n---------------------------------------------------------------\n" \
"${TITLE}" \
"\n---------------------------------------------------------------\n"
}
print_subheading(){
# title set ?
[ -z "$1" ] && return
# default style: bold blue
STYLE='\x1B[7m\x1B[34m\x1B[1m'
# style set ?
[ ! -z "$2" ] && {
STYLE="$2"
}
# default title, text only
TITLE="$1"
T=$(date +%T)
# print heading
_print_colorized "$STYLE" \
"[$T] >> ${TITLE} "
}
log_message(){
# green
local T=$(date +%T)
echo "[$T] >> $@"
}
log_success(){
# green
local T=$(date +%T)
_print_colorized '\x1B[1m\x1B[92m' "[$T] >> $@"
}
log_info(){
# blue
local T=$(date +%T)
_print_colorized '\x1B[1m\x1B[34m' "[$T] >> $@"
}
log_warning(){
# yellow
local T=$(date +%T)
_print_colorized '\x1B[1m\x1B[7m\x1B[33m' "[$T] >> $@ "
}
log_error(){
# red bold
local T=$(date +%T)
_print_colorized '\x1B[7m\x1B[91m\x1B[1m' "[$T] >> $@ "
}
panic(){
log_error "$@"
exit 1
}
hook_exec(){
log_info "hook [$1] triggered"
# hook set ?
if [ -z "$1" ]; then
log_error "invalid hook call"
return
fi
local HOOK_FN="hook_$1"
# default hook source
local HOOK_FILE="$BASEDIR/.hooks/$1"
# hook file exists in working dir ?
if [ -f $WORKINGDIR/.hooks/$1 ]; then
local HOOK_FILE="$WORKINGDIR/.hooks/$1"
fi
# exists ?
if [ ! -f $HOOK_FILE ]; then
return 0
fi
# executable ?
if [ ! -x $HOOK_FILE ]; then
panic "hook [$HOOK_FILE] cannot be executed"
fi
# run hook
log_info "running hook [$1].."
# strip hookname
shift
# load and execute
source $HOOK_FILE
$HOOK_FN $@
}