-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.lib.sh
executable file
·66 lines (59 loc) · 1.95 KB
/
validation.lib.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
#!/bin/bash
################################################################################
# validation.lib.sh - Bash library functions related to validation
################################################################################
#
# Copyright (C) 2014 - 2015 stepping stone GmbH
# Bern, Switzerland
# http://www.stepping-stone.ch
#
# Authors:
# Christian Affolter <[email protected]>
#
# Licensed under the EUPL, Version 1.1.
#
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://www.osor.eu/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
#
################################################################################
# The path to the lib directory.
# The default value only works if not sourced or executed from within $PATH
LIB_DIR=${LIB_DIR:="$(readlink -f ${0%/*})"}
source "${LIB_DIR}/input-output.lib.sh"
function validationDieIfCommandMissing ()
{
local cmdPath="${1}"
local cmdName="$( basename "${cmdPath}" )"
debug "Checking ${cmdPath}"
test -x "${cmdPath}" || die "Missing ${cmdName} command at '${cmdPath}'"
}
# Checks if a given value matches a given regular expression.
#
# Returns 0 on match, otherwise 1
# Prints an error message if passed.
#
# validationRegex <VALUE> <REGEX> [<ERROR-MESSAGE>]
function validationRegex() {
local value="${1}"
local regex="${2}"
local errorMessage="${3}"
if ! [[ "${value}" =~ $regex ]]; then
if test -n "$errorMessage"; then
error "$errorMessage"
fi
return 1
fi
return 0
}