-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-config
executable file
·267 lines (224 loc) · 6.47 KB
/
package-config
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/sh
myname=$(basename $0)
configbase="${WTFOS_PACKAGE_CONFIG_BASE:-/opt/etc/package-config/}"
if [ ! -z $DEBUG ]; then
set -x
fi
if [ ! -z DINIT_BINARY ]; then
alias dinitctl=echo
fi
#detect entware jq, use it
if [ -f /opt/bin/jq ]; then
jqbin=/opt/bin/jq
else
jqbin=jq
fi
dinitbin="${DINITCTL_BINARY:-dinitctl}"
set -e
sub_help(){
echo "Usage: $myname <subcommand> [options]\n"
echo "Subcommands:"
echo " get <package> show all keys for package"
echo " get <package> <key> get value for key in package"
echo " getsaved <package> <key> get on disk (non-pending) value"
echo " set <package> <key> <value> set value for key in package"
echo " reset <package> forget unapplied changes"
echo " apply <package> apply changes and restart package units"
echo ""
}
init_schema(){
configfile=$configbase/$1/config.json
schemafile=$configbase/$1/schema.json
newconfigfile="/tmp/package-config/$1/config.json.new"
if [ ! -f $configfile ]; then
echo "package $1 configuration not found"
exit 1
fi
if [ ! -f $schemafile ]; then
echo "package $1 schema not found"
exit 1
fi
if [ "$#" -ne 1 ]; then
##lets keep things sane
echo "$2" | grep -Eq "^[a-zA-Z0-9\\_\\-]*$"
result=$?
if [ $result -ne 0 ]; then
echo "key may only contain alphanumeric characters and _"
exit 1
fi
settingkey=$(echo -n "$2" | $jqbin -R -s '.')
settingtype=$($jqbin -r ".settings.$settingkey.widget" $schemafile)
result=$?
if [ $result -ne 0 ]; then
echo "could not parse schema.json, see errors above"
exit 1
fi
if [ -z "$settingtype" ] || [ "$settingtype" = "null" ]; then
echo "invalid key for $1"
exit 1
fi
fi
}
sub_getsaved() {
showsavedonly=true
sub_get "$@"
}
sub_get(){
if [ "$#" -lt 1 ]; then
echo "must specify config package"
exit 1
fi
init_schema $1
key=$(echo -n "$2" | $jqbin -R -s '.')
if [ -z $showsavedonly ] && [ -f "$newconfigfile" ]; then
configfile="$newconfigfile"
fi
if [ "$#" -eq 1 ]; then
#dump all settings
$jqbin -r 'to_entries|map("\(.key)=" + @sh "\(.value|tostring)")|.[]' $configfile
else
#get specific settings
$jqbin -r ".$key" $configfile
fi
}
validate_checkbox(){
if [ $1 != "false" ] && [ $1 != "true" ]; then
echo "value must be 'true' or 'false'"
exit 1
fi
return 0
}
validate_range(){
if [ "$pattern" != "null" ]; then
echo "$1" | grep -Eq "^[0-9]*$"
result=$?
if [ $result -ne 0 ]; then
echo "$settingkey must be an integer number"
exit 1
fi
fi
minValue=$($jqbin -r .settings.$settingkey.min $schemafile)
if [ "$minValue" != "null" ]; then
if [ $1 -lt $minValue ]; then
echo "$settingkey must be at least $minValue"
exit 1
fi
fi
maxValue=$($jqbin -r .settings.$settingkey.max $schemafile)
if [ "$maxValue" != "null" ]; then
if [ $1 -gt $maxValue ]; then
echo "$settingkey must be no more than $maxValue"
exit 1
fi
fi
return 0
}
validate_select(){
safevalue=$(echo -n $1 | $jqbin -R -s '.')
if [ $($jqbin "any(.settings.$settingkey.options[].key == $safevalue; .)" $schemafile) != "true" ]; then
echo "$1 is not a valid option for $settingkey"
exit 1
fi
}
validate_text(){
pattern=$($jqbin -r .settings.$settingkey.pattern $schemafile)
if [ "$pattern" != "null" ]; then
echo "$1" | grep -Eq "^$pattern$"
result=$?
if [ $result -ne 0 ]; then
echo "$settingkey does not match $pattern"
exit 1
fi
fi
minLength=$($jqbin -r .settings.$settingkey.minLength $schemafile)
if [ "$minLength" != "null" ]; then
if [ ${#1} -lt $minLength ]; then
echo "$settingkey must be at least $minLength characters"
exit 1
fi
fi
maxLength=$($jqbin -r .settings.$settingkey.maxLength $schemafile)
if [ "$maxLength" != "null" ]; then
if [ ${#1} -gt $maxLength ]; then
echo "$settingkey must be no more than $maxLength characters"
exit 1
fi
fi
return 0
}
sub_set(){
if [ "$#" -ne 3 ]; then
echo "must specify package, key and value"
exit 1
fi
init_schema "$1" "$2"
validate_${settingtype} "$3"
if [ $? = 127 ]; then
echo "Error: '$settingtype' is not a known setting type." >&2
exit 1
fi
key=$(echo -n "$2" | $jqbin -R -s '.')
if [ "$settingtype" = "text" ] || [ "$settingtype" = "select" ]; then
value=$(echo -n $3 | $jqbin -R -s '.')
else
value="$3"
fi
mkdir -p "/tmp/package-config/"
mkdir -p "/tmp/package-config/$1/"
if [ -f $newconfigfile ]; then
$jqbin ".$key = $value" $newconfigfile > $configfile.tmp
mv $configfile.tmp $newconfigfile
else
$jqbin ".$key = $value" $configfile > $newconfigfile
fi
}
sub_reset() {
if [ "$#" -ne 1 ]; then
echo "must specify package"
exit 1
fi
init_schema "$1"
if [ ! -f $newconfigfile ]; then
echo "no changes to reset"
exit 1
else
rm -f $newconfigfile
fi
}
sub_apply(){
if [ "$#" -ne 1 ]; then
echo "must specify package"
exit 1
fi
init_schema "$1"
if [ ! -f $newconfigfile ]; then
echo "no changes to apply"
fi
#validate the new file for being paresable
if $jqbin empty $newconfigfile 2>/dev/null; then
mv $newconfigfile $configfile
else
echo "resulting config file is not valid json, please check your input"
exit 1
fi
echo "changes applied"
for unit in $(cat $schemafile | $jqbin -r '.units[]'); do
echo "restarting $unit"
$dinitbin restart $unit || true
done
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} "$@"
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$myname help' for a list of known subcommands." >&2
exit 1
fi
;;
esac