forked from Wizcorp/libbash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.sh
executable file
·142 lines (124 loc) · 4.3 KB
/
validation.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
#!/bin/bash
##
# title : validation.sh
# description : This script provides helper functions for data, input and
# setup validation.
# author : Almir Kadric
# created on : 2012-12-12
# version : 0.1
##
# Get script root path, symlink resistant
scriptRoot=$(cd "$(dirname ${BASH_SOURCE})" && pwd);
if [ -h ${BASH_SOURCE} ]
then
scriptRoot=$(cd "$(dirname $(readlink ${BASH_SOURCE}))" && pwd);
fi
# Include required libraries
. ${scriptRoot}/bashr.sh
##
# Takes in a space separated list of command which should be expected. If anyone
# one of the listed commands are found to not exist, an error will thrown for
# that command. And once all commands are processed, if any were found missing
# function will exit with status code of 1 and return those missing command names
# in a predefined array called notFoundCommands.
#
# Commands can be scripts, system binaries etc. found in $PATH or functions
#
# Usage: EnsureCommandsAvailable command1 [command2] [...]
#
# Arguments:
# commands: space separated list of commands which to test for
#
# Environment Variables:
# notFoundCommands: Array list of commands which could not be found.
#
# SILENT_ERROR: (BOOLEAN) Whether or not an error should be written to
# STDERR when a command is not found. This is useful if you
# want to display a custom error message, or handle it in a
# different fashion.
##
function EnsureCommandsAvailable() {
# Define returned environment variables
eval notFoundCommands=();
# Define required variables
commands="$@";
errors=false;
# Iterate through commands
for command in ${commands}
do
# Check command type
type $command 2>/dev/null 1>/dev/null;
status=$?;
# Check if type casting of command was successful
if [ $status -ne 0 ]
then
# If not silent, output error if command was not found
if [ "${SILENT_ERROR:-}" = "" ] || ! $SILENT_ERROR
then
echoError "Could not find command ${command}";
fi
eval notFoundCommands+=(${command});
errors=true;
fi
done
# Exit if any commands were not found
if $errors
then
return 1;
fi
}
##
# Function which takes in a list of arguments and allowed arguments and then
# determines if there are any foreign arguments. If none are found it will then
# store the argument value pairs into variables with argument label and values.
#
# Usage: ParseArguments <allowed> <arguments>
#
# Arguments:
# allowed: space or comma separated list of allowed arguments. Anything not
# in this list will be considered foreign and will cause the function
# to exit with error code 1.
#
# arguments: list of arguments passed to your application (usually will be $@)
# which will be tested against allowed values passed. Also these
# will then be pulled a part and stored into actual variables.
#
# Environment Variables:
# invalidArguments: Array list of arguments which were invalid.
#
# SILENT_ERROR: (BOOLEAN) Whether or not an error should be written to
# STDERR when a command is not found. This is useful if you
# want to display a custom error message, or handle it in a
# different fashion.
##
function ParseArguments() {
# Define returned environment variables
eval invalidArguments=();
# Define required variables
allow="${1:-}";
errors=false;
# Remove the first element from arguments
shift 1
# Iterate through remaining arguments
for argument in $@
do
label="$(echo $argument | awk -F '=' '{print $1}' | sed 's/^--//')";
value="$(echo $argument | awk -F '=' '{print $2}')";
if [ "$(echo ${allow} | egrep "(^|[,\ ])${label}([,\ ]|$)")" = "" ]
then
if [ "$SILENT_ERROR" = "" ] || ! $SILENT_ERROR
then
echoError "Unrecognized argument --${label}";
fi
eval invalidArguments+=(${label});
errors=true;
else
eval "export ${label}='${value}'";
fi
done
# Exit if any commands were not found
if $errors
then
return 1;
fi
}