Skip to content

Commit 599b035

Browse files
committed
Tools: unitc YAML mode.
Added --format option to manage configuration in other formats. Initially, YAML is the only supported conversion format. JSON/YAML conversion is performed with yq(1). Suggested by: Torstein Krause Johansen <https://github.com/skybert> Closes: #958 <#958>
1 parent 9c8b9a4 commit 599b035

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

tools/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ web page with NGINX Unit.
3737
| _HTTP method_ | It is usually not required to specify a HTTP method. `GET` is used to read the configuration. `PUT` is used when making configuration changes unless a specific method is provided.
3838
| `edit` | Opens **URI** in the default editor for interactive configuration. The [jq](https://stedolan.github.io/jq/) tool is required for this option.
3939
| `INSERT` | A _virtual_ HTTP method that prepends data when the URI specifies an existing array. The [jq](https://stedolan.github.io/jq/) tool is required for this option.
40+
| `-f` \| `--format YAML` | Convert configuration data to/from YAML format. The [yq](https://github.com/mikefarah/yq) tool is required for this option.
4041
| `-q` \| `--quiet` | No output to stdout.
4142

4243
Options are case insensitive and can appear in any order. For example, a
4344
redundant part of the configuration can be identified by its URI, and
4445
followed by `delete` in a subsequent command.
4546

47+
Options may be combined. For example, `edit -f yaml` will open the
48+
configuration URI in a text editor, in YAML format.
49+
4650
### Local Configuration
4751
For local instances of Unit, the control socket is automatically detected.
4852
The error log is monitored; when changes occur, new log entries are shown.

tools/unitc

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ REMOTE=0
1010
SHOW_LOG=1
1111
NOLOG=0
1212
QUIET=0
13+
CONVERT=0
1314
URI=""
1415
SSH_CMD=""
1516
METHOD=PUT
@@ -18,6 +19,30 @@ CONF_FILES=()
1819
while [ $# -gt 0 ]; do
1920
OPTION=$(echo $1 | tr '[a-z]' '[A-Z]')
2021
case $OPTION in
22+
"-F" | "--FORMAT")
23+
case $(echo $2 | tr '[a-z]' '[A-Z]') in
24+
"YAML")
25+
CONVERT=1
26+
if hash yq 2> /dev/null; then
27+
CONVERT_TO_JSON="yq eval -P --output-format=json"
28+
CONVERT_FROM_JSON="yq eval -P --output-format=yaml"
29+
else
30+
echo "${0##*/}: ERROR: yq(1) is required to use YAML format; install at <https://github.com/mikefarah/yq#install>"
31+
exit 1
32+
fi
33+
;;
34+
"")
35+
echo "${0##*/}: ERROR: Must specify configuration format"
36+
exit 1
37+
;;
38+
*)
39+
echo "${0##*/}: ERROR: Invalid format ($2)"
40+
exit 1
41+
;;
42+
esac
43+
shift; shift
44+
;;
45+
2146
"-H" | "--HELP")
2247
shift
2348
;;
@@ -45,15 +70,22 @@ while [ $# -gt 0 ]; do
4570
*)
4671
if [ -f $1 ] && [ -r $1 ]; then
4772
CONF_FILES+=($1)
73+
if [ "${1##*.}" = "yaml" ]; then
74+
echo "${0##*/}: INFO: converting $1 to JSON"
75+
shift; set -- "--format" "yaml" "$@" # Apply the command line option
76+
else
77+
shift
78+
fi
4879
elif [ "${1:0:1}" = "/" ] || [ "${1:0:4}" = "http" ] && [ "$URI" = "" ]; then
4980
URI=$1
81+
shift
5082
elif [ "${1:0:6}" = "ssh://" ]; then
5183
UNIT_CTRL=$1
84+
shift
5285
else
5386
echo "${0##*/}: ERROR: Invalid option ($1)"
5487
exit 1
5588
fi
56-
shift
5789
;;
5890
esac
5991
done
@@ -67,16 +99,18 @@ USAGE: ${0##*/} [options] URI
6799
• URI is for Unit's control API target, e.g. /config
68100
• A local Unit control socket is detected unless a remote one is specified.
69101
• Configuration data is read from stdin.
102+
• All options are case-insensitive (excluding filenames and URIs).
70103
71104
General options
72-
filename … # Read configuration data from files instead of stdin
73-
HTTP method # Default=GET, or PUT with config data (case-insensitive)
74-
EDIT # Opens the URI contents in \$EDITOR
75-
INSERT # Virtual HTTP method to prepend data to an existing array
76-
-q | --quiet # No output to stdout
105+
filename … # Read configuration data from files instead of stdin
106+
HTTP method # Default=GET, or PUT when config data is present
107+
EDIT # Opens the URI contents in \$EDITOR
108+
INSERT # Virtual HTTP method; prepend data to an array
109+
-f | --format YAML # Convert configuration data to/from YAML format
110+
-q | --quiet # No output to stdout
77111
78112
Local options
79-
-l | --nolog # Do not monitor the error log after applying config changes
113+
-l | --nolog # Do not monitor the Unit log file after config changes
80114
81115
Remote options
82116
ssh://[user@]remote_host[:port]/path/to/control.socket # Remote Unix socket
@@ -187,6 +221,8 @@ fi
187221
#
188222
if [ $QUIET -eq 1 ]; then
189223
OUTPUT="tail -c 0" # Equivalent to >/dev/null
224+
elif [ $CONVERT -eq 1 ]; then
225+
OUTPUT=$CONVERT_FROM_JSON
190226
elif hash jq 2> /dev/null; then
191227
OUTPUT="jq"
192228
else
@@ -224,6 +260,10 @@ if [ -t 0 ] && [ ${#CONF_FILES[@]} -eq 0 ]; then
224260
$SSH_CMD curl -fsSX DELETE $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ && \
225261
printf "%s" "$(< $EDIT_FILENAME.js)" | $SSH_CMD curl -fX PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ && \
226262
$SSH_CMD curl -X PUT --data-binary @/tmp/${0##*/}.$$_js_module $UNIT_CTRL/config/settings/js_module 2> /tmp/${0##*/}.$$
263+
elif [ $CONVERT -eq 1 ]; then
264+
$CONVERT_FROM_JSON < $EDIT_FILENAME > $EDIT_FILENAME.yaml
265+
$EDITOR $EDIT_FILENAME.yaml || exit 2
266+
$CONVERT_TO_JSON < $EDIT_FILENAME.yaml | $SSH_CMD curl -X PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
227267
else
228268
tr -d '\r' < $EDIT_FILENAME > $EDIT_FILENAME.json # Remove carriage-return from newlines
229269
$EDITOR $EDIT_FILENAME.json || exit 2
@@ -249,6 +289,10 @@ else
249289
exit 3
250290
fi
251291
else
292+
if [ $CONVERT -eq 1 ]; then
293+
cat ${CONF_FILES[@]} | $CONVERT_TO_JSON > /tmp/${0##*/}.$$_json
294+
CONF_FILES=(/tmp/${0##*/}.$$_json)
295+
fi
252296
cat ${CONF_FILES[@]} | $SSH_CMD curl -X $METHOD --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
253297
fi
254298
fi

0 commit comments

Comments
 (0)