Commit 50e92b8b authored by Michał Woźniak's avatar Michał Woźniak
Browse files

enabled output to stdout; started working on nginx-tree

parent 80f9f070
...@@ -12,7 +12,7 @@ Modes are: ...@@ -12,7 +12,7 @@ Modes are:
- `flatten`: - `flatten`:
flatten an nginx config file by inlining all includes recursively, flatten an nginx config file by inlining all includes recursively,
save to `<output_file>` save to `<output_file>` (or to standard output if <output_file> not given)
- `clean-directory`: - `clean-directory`:
generate a cleaned nginx config directory, containing only the files that generate a cleaned nginx config directory, containing only the files that
...@@ -24,7 +24,9 @@ Imporant caveats: ...@@ -24,7 +24,9 @@ Imporant caveats:
- `<input_file>` *must* exist (if it doesn't the script will exit with an error) - `<input_file>` *must* exist (if it doesn't the script will exit with an error)
- `<output_file>`/`<output_directory>` *must not* exist (if it does, the script will exit with an error) - `<output_file>`/`<output_directory>` *must not* exist (if it does, the script will exit with an error)
Debug output (a lot of it!) can be enabled by setting the `NGINX_CONF_FLATTEN_DEBUG` environment variable to any non-empty string before running `nginx-conf-flatten`. For example: Debug output (a lot of it!) can be enabled by setting the `NGINX_CONF_FLATTEN_DEBUG` environment variable to any non-empty string before running `nginx-conf-flatten`. If enabled, it is printed to `stderr`.
For example:
``` ```
NGINX_CONF_FLATTEN_DEBUG='yes-please' nginx-conf-flatten flatten /etc/nginx.conf ./flat.conf NGINX_CONF_FLATTEN_DEBUG='yes-please' nginx-conf-flatten flatten /etc/nginx.conf ./flat.conf
......
...@@ -155,11 +155,25 @@ function nginx-flatten { ...@@ -155,11 +155,25 @@ function nginx-flatten {
debug "+-- moving the temporary output file to the output destination" debug "+-- moving the temporary output file to the output destination"
debug " src: $NGINX_CONFIG_COPY" debug " src: $NGINX_CONFIG_COPY"
debug " dst: $OUTPUT_DEST" debug " dst: $OUTPUT_DEST"
if [ "$OUTPUT_DEST" == '-' ]; then
cat "$NGINX_CONFIG_COPY"
else
mv "$NGINX_CONFIG_COPY" "$OUTPUT_DEST" mv "$NGINX_CONFIG_COPY" "$OUTPUT_DEST"
fi
debug "+-- cleaning up the temporary output directory" debug "+-- cleaning up the temporary output directory"
rm -rf "$TMP_WORKDIR" rm -rf "$TMP_WORKDIR"
} }
#
# print the include tree, or dump it to a file
function nginx-tree {
echo "NOT IMPLEMENTED YET"
}
#
# print the usage
function print-usage { function print-usage {
cat - <<HEREDOC cat - <<HEREDOC
...@@ -168,9 +182,13 @@ usage: ...@@ -168,9 +182,13 @@ usage:
modes: modes:
tree:
generate a tree of includes and save it to <output_file>
(or to standard output if <output_file> not given)
flatten: flatten:
flatten an nginx config file by inlining all includes recursively, flatten an nginx config file by inlining all includes recursively,
save to <output_file> save to <output_file> (or to standard output if <output_file> not given)
clean-directory: clean-directory:
generate a cleaned nginx config directory, containing only the files generate a cleaned nginx config directory, containing only the files
...@@ -188,7 +206,7 @@ ORIG_CWD="$PWD" ...@@ -188,7 +206,7 @@ ORIG_CWD="$PWD"
# $2 - source file # $2 - source file
# $3 - destination file/directory # $3 - destination file/directory
if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ]; then if [ "$1" == "" ] || [ "$2" == "" ]; then
print-usage print-usage
exit 1 exit 1
fi fi
...@@ -201,16 +219,36 @@ if [ ! -f "$2" ] || [ ! -r "$2" ]; then ...@@ -201,16 +219,36 @@ if [ ! -f "$2" ] || [ ! -r "$2" ]; then
exit 2 exit 2
fi fi
# destination file/directory must not exist if [ "$1" == "flatten" ] || [ "$1" == "tree" ]; then
OUTPUT_DEST="$( get-path-relative-to "$3" "$ORIG_CWD" )" if [ "$3" == "" ]; then
if [ -e "$OUTPUT_DEST" ]; then OUTPUT_DEST="-"
debug "+-- output to stdout."
else
# destination file/directory must not exist
OUTPUT_DEST="$( get-path-relative-to "$3" "$ORIG_CWD" )"
if [ -e "$OUTPUT_DEST" ]; then
echo
echo "WARNING: output file/directory exists; quitting!"
echo
exit 3
fi
fi
# for clean-director we always have to have $3
else
if [ "$3" == "" ]; then
print-usage
exit 1
fi
# destination file/directory must not exist
OUTPUT_DEST="$( get-path-relative-to "$3" "$ORIG_CWD" )"
if [ -e "$OUTPUT_DEST" ]; then
echo echo
echo "WARNING: output file/directory exists; quitting!" echo "WARNING: output file/directory exists; quitting!"
echo echo
exit 3 exit 3
fi
fi fi
NGINX_CONFIG="$( realpath -eL "$2" )" NGINX_CONFIG="$( realpath -eL "$2" )"
debug "+-- config file: '$NGINX_CONFIG'" debug "+-- config file: '$NGINX_CONFIG'"
...@@ -228,9 +266,11 @@ NGINX_CONFIG_ELEMENTS=("$NGINX_CONFIG") ...@@ -228,9 +266,11 @@ NGINX_CONFIG_ELEMENTS=("$NGINX_CONFIG")
# what are we doing, eh? # what are we doing, eh?
if [ "$1" == "flatten" ]; then if [ "$1" == "flatten" ]; then
nginx-flatten "$NGINX_CONFIG" "$3" nginx-flatten
elif [ "$1" == "clean-directory" ]; then elif [ "$1" == "clean-directory" ]; then
nginx-clean-directory "$NGINX_CONFIG" "$3" nginx-clean-directory
elif [ "$1" == "tree" ]; then
nginx-tree
else else
print-usage print-usage
exit 1 exit 1
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment