Commit 3541677d authored by Michał Woźniak's avatar Michał Woźniak
Browse files

bugfix

parent 1e853f0f
*.kate-swp
......@@ -12,17 +12,17 @@ Modes are:
- `flatten`:
flatten an nginx config file by inlining all includes recursively,
save to <output_file>
save to `<output_file>`
- `clean-directory`:
generate a cleaned nginx config directory, containing only the files that
are included from the input file, or files included from those, and so on;
output to <output_directory>
output to `<output_directory>`
Imporant caveats:
- `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)
- `<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)
## Operation
......@@ -31,7 +31,7 @@ The script creates a temporary directory (`/tmp/nginx-conf-flatten.????`) and wo
Once output is ready (either in the form of a single flattened `nginx` config file if mode was `flatten`, or a directory structure if it was `clean-directory`), it is moved to the correct location and the temporary directory is removed.
The `flatten` mode makes an honest attempt at keeping indentation sane. This means looking at an `include` line, and indenting the text included from the relevant files by whatever indent was there in the `include` line. This seems to work reasonably well, but if you want the config to be linted and nicely formatted, use a linter and a formatter.
The `flatten` mode makes an honest attempt at keeping indentation sane. This means looking at an `include` line, and indenting the text included from the relevant files by whatever indent was there in the `include` line. This seems to work reasonably well, but if you want the config to be linted and nicely formatted, use a linter and [a formatter](https://github.com/1connect/nginx-config-formatter).
## FAQ
......
......@@ -12,8 +12,14 @@
# - no files are included from above the directory where the nginx config file
# being processed resides in
# set debug verbosity to none
[ -z "${NGINX_CONFIG_FLATTEN_DEBUG+x}" ] && NGINX_CONFIG_FLATTEN_DEBUG=""
function debug {
echo "$@" >&2
if [ "$NGINX_CONFIG_FLATTEN_DEBUG" != "" ]; then
echo "$@" >&2
fi
}
......@@ -130,18 +136,19 @@ function nginx-flatten {
debug " +-- previous line : $PREVIOUS_CI_LINE"
debug " ci line : $CI_LINE"
debug " indent : '$CI_INDENT'"
debug " glob : '$CI_FILE_GLOB'"
tail -n "+$(( $PREVIOUS_CI_LINE + 1 ))" "$NGINX_CONFIG_COPY" | head -n $(( $CI_LINE - 1 - $PREVIOUS_CI_LINE )) > "$NGINX_CONFIG_COPY.$DATE_EXT"
echo -e "\n$CI_INDENT############################################################\n$CI_INDENT### $CI_FILE_GLOB \n$CI_INDENT############################################################" >> "$NGINX_CONFIG_COPY.$DATE_EXT"
sed -r -e "s/^/$CI_INDENT/" $CI_FILE_GLOB >> "$NGINX_CONFIG_COPY.$DATE_EXT"
echo -e "\n$CI_INDENT############################################################\n$CI_INDENT### end $CI_FILE_GLOB \n$CI_INDENT############################################################" >> "$NGINX_CONFIG_COPY.$DATE_EXT"
PREVIOUS_CI_LINE="$CI_LINE"
done
#debug "pre-cat waiting..."
#read
debug " +-- previous line : $PREVIOUS_CI_LINE"
debug " finishing this run off."
DATE_EXT="$( date +%s%N )"
tail -n "+$(( $PREVIOUS_CI_LINE + 1 ))" "$NGINX_CONFIG_COPY" > "$NGINX_CONFIG_COPY.$DATE_EXT"
cat $NGINX_CONFIG_COPY.* > "$NGINX_CONFIG_COPY"
rm $NGINX_CONFIG_COPY.*
#debug "post-rm waiting..."
#read
done
IFS="$OLDIFS"
......
Markdown is supported
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