-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconf.lib.sh
More file actions
208 lines (175 loc) · 6.64 KB
/
conf.lib.sh
File metadata and controls
208 lines (175 loc) · 6.64 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#
# Copyright (c) 2010-2011 Linagora
# Patrick Guiran <pguiran@linagora.com>
# http://github.com/Tauop/ScriptHelper
#
# ScriptHelper is free software, you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# ScriptHelper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# README ---------------------------------------------------------------------
# This library helps to deal with configuration files
#
# Global variables ===========================================================
# IMPORTANT: Please to write to those variables
# __LIB_CONF__ : 'Loaded' when the lib is 'source'd
# __CONF_FILE__ : configuration file to deal with when not given with --file
# ----------------------------------------------------------------------------
# don't load several times this file
if [ "${__LIB_CONF__:-}" != 'Loaded' ]; then
__LIB_CONF__='Loaded'
__CONF_FILE__=
# load dependencies
load() {
local var= value= file=
var="$1"; file="$2"
value=$( eval "printf '%s' \"\${${var}:-}\"" )
[ -n "${value}" ] && return 1;
if [ -f "${file}" ]; then
. "${file}"
else
printf "ERROR: Unable to load ${file}\n"
exit 2
fi
return 0;
}
# Load configuration file
load SCRIPT_HELPER_DIRECTORY /etc/ScriptHelper.conf
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY:-}"
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY%%/}"
load __LIB_MESSAGE__ "${SCRIPT_HELPER_DIRECTORY}/message.lib.sh"
load __LIB_RANDOM__ "${SCRIPT_HELPER_DIRECTORY}/random.lib.sh"
# ----------------------------------------------------------------------------
# usage: SET_CONF_FILE <file>
# desc: Save a file path where you will read/write data
# note: if the directory doesn't exist, it will be created
# note: if the configuration file doesn't exist, it will be created
CONF_SET_FILE () {
local dir= file=
[ $# -ne 1 ] && FATAL 'SET_CONF_FILE: bad arguments'
__CONF_FILE__="$1"
if [ -z "${__CONF_FILE__}" ]; then
LOG "Configuration file reset"
return 0
fi
file="${__CONF_FILE__##*/}"
dir="${__CONF_FILE__%${file}}"
# create the configuration file if it doesn't exist
if [ ! -e "${__CONF_FILE__}" ]; then
[ -d "${dir:-./}" ] || CMD mkdir -p "${dir}"
touch "${__CONF_FILE__}"
chmod u+wr "${__CONF_FILE__}"
chmod a-x "${__CONF_FILE__}"
LOG "Configuration file ${__CONF_FILE__} created"
fi
}
# usage: private_SED_SEPARATOR <string>
# desc: determine a good sed separator
private_SED_SEPARATOR () {
for s in '/' '@' ',' '|'; do
printf '%s' "$1" | grep "$s" >/dev/null
if [ $? -ne 0 ]; then
printf '%s' "$s"; return 0;
fi
done
return 1;
}
# usage: CONF_SAVE [ <option> ] <conf_var> [ <value> ]
# desc: save a variable into the configuration file
# options: --conf-file <file> : the configuration file to use
CONF_SAVE () {
local var= value= sep= conf_file="${__CONF_FILE__}" s='[[:space:]]'
while true ; do
[ $# -eq 0 ] && break;
case "$1" in
--conf-file) shift; conf_file="$1"; shift ;;
*) break;
esac
done
[ $# -eq 0 -o $# -gt 2 ] && FATAL 'CONF_SAVE: Bad number of arguments'
[ ! -w "${conf_file}" ] && FATAL "CONF_SAVE: Can't write into configuration file '${conf_file}'"
var="$1";
[ -z "${var}" ] && FATAL "CONF_SAVE: variable name is empty"
[ $# -eq 2 ] && value="$2"
[ $# -eq 1 ] && eval "value=\"\${${1}}\""
sep=$( private_SED_SEPARATOR "${var}${value}" )
# save data into the configuration file
tmp_file="/tmp/conf.$(RANDOM)"
grep "^$s*${var}$s*=" < "${conf_file}" >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then # found -> replace
sed -e "s${sep}^\($s*${var}\)$s*=.*\$${sep}\1=\"${value}\"${sep}" < "${conf_file}" > "${tmp_file}"
mv "${tmp_file}" "${conf_file}"
else # append
printf '%s\n' "${var}=\"${value}\"" >> "${conf_file}"
fi
LOG "CONF_SAVE: ${var}=\"${value}\""
}
# usage: CONF_GET [ <options> ] <conf_var> [ <result_var> ]
# desc: read a variable from the configuration file
# options: --conf-file <file> : the configuration file to use
CONF_GET () {
local result= resultvar= confvar= sep= conf_file="${__CONF_FILE__}" s='[[:space:]]'
while true ; do
[ $# -eq 0 ] && break
case "$1" in
--conf-file) shift; conf_file="$1"; shift ;;
*) break;
esac
done
[ $# -eq 0 -o $# -gt 2 ] && FATAL 'CONF_GET: bad number of arguments'
[ ! -r "${conf_file}" ] && FATAL "CONF_GET: Can't read from '${conf_file}'"
confvar="$1"
resultvar="${2:-$1}"
[ -z "${confvar}" ] && FATAL "CONF_GET: variable name is empty"
sep=$( private_SED_SEPARATOR "${confvar}${resultvar}" )
result=$( sed -n -e "s${sep}^$s*${confvar}$s*=$s*\(.*\)$s*\$${sep}\1${sep}p" "${conf_file}" )
eval "${resultvar}=${result}"
LOG "CONF_GET: ${resultvar}=${result}"
}
# usage: CONF_DEL [ <options> ] <conf_var>
# desc: remove the <conf_var> from the configuration file
# options: --conf-file <file> : the configuration file to use
CONF_DEL () {
local confvar= tmp_file= conf_file="${__CONF_FILE__}" s='[[:space:]]'
while true ; do
[ $# -eq 0 ] && break
case "$1" in
--conf-file) shift; conf_file="$1"; shift ;;
*) break;
esac
done
[ $# -ne 1 ] && FATAL 'CONF_DEL: bad number of arguments'
confvar="$1"
[ ! -w "${conf_file}" ] && FATAL "CONF_DEL: Can't write to '${conf_file}'"
[ -z "${confvar}" ] && FATAL 'CONF_DEL: variable name is empty'
tmp_file="/tmp/conf.$(RANDOM)"
grep -v "^$s*${confvar}$s*=.*\$" < "${conf_file}" > "${tmp_file}"
mv "${tmp_file}" "${conf_file}"
LOG "CONF_DEL: remove '${confvar}'"
if [ ! -s "${conf_file}" ]; then
rm -f "${conf_file}"
LOG "CONF_DEL: '${conf_file}' deleted"
fi
return 0;
}
# usage: CONF_LOAD [ <file> ]
# desc: load a configuration file.
# note: if called without argument, use the file set by CONF_SET_FILE
CONF_LOAD () {
local file=${1:-${__CONF_FILE__}}
if [ -n "${file}" ]; then
[ ! -r "${file}" ] && FATAL "CONF_LOAD: Can't read from '${file}'"
. "${file}"
LOG "CONF_LOAD: ${file}"
fi
}
fi # end of: if [ "${__LIB_CONF__}" != 'Loaded' ]; then