forked from MycroftAI/mycroft-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycroft-config
executable file
·244 lines (208 loc) · 6.6 KB
/
mycroft-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
#!/usr/bin/env bash
# Copyright 2019 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SOURCE="${BASH_SOURCE[0]}"
cd -P "$( dirname "$SOURCE" )"
DIR="$( pwd )"
script=${0}
script=${script##*/}
function help() {
echo "${script}: Mycroft configuration manager"
echo "usage: ${script} [COMMAND] [params]"
echo
echo "COMMANDs:"
echo " edit (system|user) edit and validate config file"
echo " reload instruct services to reload config"
echo " show (default|remote|system|user) display the specified setting file"
echo " set <var> set the variable (under USER)"
echo " get [var] display a particular variable"
echo " or all if no 'var' specified"
echo "Note: Use jq format for specifying <var>"
echo
echo "Examples:"
echo " ${script} edit user"
echo " sudo ${script} edit system"
echo " ${script} show remote"
echo " ${script} get"
echo " ${script} get enclosure.platform"
echo " ${script} set test.subvalue \"foo\" "
exit 1
}
################################################################
# Setup stuff based on the environment
VIEWER="nano --syntax=json --view"
if [ -z "$EDITOR" ] ; then
if [ $( which sensible-editor ) ] ; then
EDITOR="sensible-editor"
else
EDITOR="nano --syntax=json --tempfile"
fi
fi
if [ -z "$TEMP" ] ; then
TEMP="/tmp"
fi
function found_exe() {
hash "$1" 2>/dev/null
}
if found_exe tput ; then
GREEN="$(tput setaf 2)"
BLUE="$(tput setaf 4)"
CYAN="$(tput setaf 6)"
YELLOW="$(tput setaf 3)"
RESET="$(tput sgr0)"
HIGHLIGHT=${YELLOW}
fi
################################################################
# Utilities
function validate_config_file() {
if [ ! -f "$1" ] ; then
# A missing config file is valid
return 0
fi
echo -n ${BLUE}
# Remove any comments (lines starting with # or //) found in the file and
# Use jq to validate and output errors
sed 's/^\s*[#\/].*$//g' "$1" | sed '/^$/d' | jq -e "." > /dev/null
result=$?
echo -n ${RESET}
#xxx echo "RESULT=$result for $1"
return $result
}
_conf_file="~/.mycroft/mycroft.conf"
function name_to_path() {
case ${1} in
"system") _conf_file="/etc/mycroft/mycroft.conf" ;;
"user") _conf_file=$(readlink -f ~/.mycroft/mycroft.conf) ;;
"default") _conf_file="$DIR/../mycroft/configuration/mycroft.conf" ;;
"remote") _conf_file="/var/tmp/mycroft_web_cache.json" ;;
*)
echo "ERROR: Unknown name '${1}'."
echo " Must be one of: default, remote, system, or user"
exit 1
esac
}
################################################################
function edit_config() {
name_to_path $1
validate_config_file $_conf_file
rc=$?
if [ $rc -ne 0 ] ; then
echo "${YELLOW}WARNING: ${RESET}Configuration file did not pass validation before edits."
read -p "Review errors above and press ENTER to continue with editing."
fi
if [ -f "${_conf_file}" ] ; then
cp "${_conf_file}" "${TEMP}/mycroft.json"
else
echo "{" > "${TEMP}/mycroft.json"
echo "}" >> "${TEMP}/mycroft.json"
fi
while [ 1 ] ; do
case $1 in
system | user)
# Allow user to edit
$EDITOR $TEMP/mycroft.json
;;
default | remote)
# View-only
echo "The default config shouldn't be changed, opening in View mode"
sleep 2
$VIEWER $TEMP/mycroft.json
;;
esac
cmp --quiet "${_conf_file}" "${TEMP}/mycroft.json"
rc=$?
if [ $rc -eq 0 ] ; then
echo "Configuration unchanged."
break
fi
# file was changed, validate changes
validate_config_file $TEMP/mycroft.json
if [ $? -ne 0 ] ; then
echo "${YELLOW}WARNING: ${RESET}Configuration file does not pass validation, see errors above."
echo "Press X to abandon changes, S to force save, any other key to edit again."
read -N1 -s key
else
key="S"
fi
case $key in
[Ss])
echo "Saving..."
mv $TEMP/mycroft.json $_conf_file
signal_reload_config
break
;;
[Xx])
# abandoning
break
;;
esac
done
}
function signal_reload_config() {
# Enter the Mycroft venv
source "$DIR/../venv-activate.sh" -q
# Post a messagebus notification to reload the config file
output=$(python -m mycroft.messagebus.send "configuration.updated" "{}")
}
function show_config() {
name_to_path $1
# Use jq to display formatted nicely (after stripping out comments)
sed 's/^\s*[#\/].*$//g' "${_conf_file}" | sed '/^$/d' | jq "."
}
function get_config() {
value=$1
if [[ ! $value =~ ^\..* ]] ; then
# Add the leading period if not included
value=".${value}"
fi
# Load all the configuration(s)
json_config=$( source "$DIR/../venv-activate.sh" -q && python -c "import json; from mycroft.configuration import Configuration; print(json.dumps(Configuration.get()))" )
# Read the given variable from the mix
echo ${json_config} | jq -r "${value}"
}
function set_config() {
# Set all overrides under the user configuration
value=$1
if [[ ! $value =~ ^\..* ]] ; then
# Add the leading period if not included
value=".${value}"
fi
jq "${value} = \"$2\"" ~/.mycroft/mycroft.conf > "${TEMP}/~mycroft.conf"
if [ $? -eq 0 ] ; then
# Successful update, replace the config file
mv "${TEMP}/~mycroft.conf" ~/.mycroft/mycroft.conf
signal_reload_config
fi
}
_opt=$1
case ${_opt} in
"edit")
edit_config $2
;;
"reload")
signal_reload_config
;;
"show")
show_config $2
;;
"get")
get_config $2
;;
"set")
set_config "$2" "$3"
;;
*)
help
;;
esac