forked from AAFC-BICoE/matiri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.sh
155 lines (135 loc) · 2.84 KB
/
util.sh
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#
# utiliy library for matiri
#
# Author: GNewton 2013.10.09 [email protected]
# Copyright 2013 Government of Canada and Glen Newton
# Apache v2 License
#
# Except for code from http://google-styleguide.googlecode.com/svn/trunk/shell.xml?showone=STDOUT_vs_STDERR#STDOUT_vs_STDERR v1.26
# - function err()
function command_exists {
local commandName
commandName="$1"
local fullPath
fullPath=$(command -v $commandName)
if [[ $? == 0 ]] && [[ -x $fullPath ]]; then
return 0
fi
return 42
}
function thisFunc {
echo ${FUNCNAME[ 1 ]}
}
function parent {
echo ${FUNCNAME[ 2 ]}
}
# $1=expected; $2=actual
function expects {
local readonly EXPECTED
EXPECTED=$1
local readonly ACTUAL
ACTUAL=$2
if [ $EXPECTED -eq $ACTUAL ]; then
return 0
fi
echo "Error: function $(parent) expects: $1 arguments; passed $2 arguments"
exit 1
}
function test {
"$@"
local status
status=$?
if [ $status -ne 0 ]
then
echo "error with $1"
fi
return $status
}
function deleteIfExists {
local filename
filename="$1"
if [ -e $filename ]
then
rm -f $filename
fi
}
# readonly LOG_INFO="INFO: "
# readonly LOG_ERROR="ERROR: "
function log {
if [ $LOG == true ]; then
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] ($(parent)) $@" >&2
fi
}
# From http://google-styleguide.googlecode.com/svn/trunk/shell.xml?showone=STDOUT_vs_STDERR#STDOUT_vs_STDERR v1.26
function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
# $1=#columns
function get_sha {
expects 2 $#
if [ ! -e $2 ]; then
echo "File does not exist: $2" >&2
fi
echo $(colrm $1 < $2)
}
function onBackupList {
if [ $# -eq 1 ]; then
echo "2"
return
fi
local DATABASE
DATABASE=$1
shift
local WANTED_DBS
WANTED_DBS=("${@}")
for WANTED_DB in "${WANTED_DBS[@]}" # or simply "for i; do"
do
if [ "$WANTED_DB" == "$DATABASE" ]; then
echo "0"
return
fi
done
echo "1"
return
}
function onDoNotBackupList {
if [ $# -eq 1 ]; then
echo "2"
return
fi
local DATABASE
DATABASE=$1
shift
local UNWANTED_DBS
UNWANTED_DBS=("${@}")
for UNWANTED_DB in "${UNWANTED_DBS[@]}" # or simply "for i; do"
do
if [ "$UNWANTED_DB" == "$DATABASE" ]; then
echo "0"
return
fi
done
echo "1"
return
}
function should_backup {
if [ $1 == "0" ] || [ $1 == "2" -a $2 == "2" ] || [ $1 == "2" -a $2 == 1 ]; then
echo "true"
else
echo "false"
fi
}
function check_dependencies {
local DEPS
DEPS=("${@}")
local hasAllDeps
hasAllDeps=true
for cmd in ${DEPS[@]}; do
hash $cmd 2>/dev/null || { echo >&2 "ERROR: dependency \"$cmd\" is NOT in PATH."; hasAllDeps=false; }
done
if ! $hasAllDeps ; then
echo >&2 "ERROR: Missing dependencies. Aborting"
return 42
fi
return 0
}