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

honest indentation for flatten mode; updated README

parent b00cdb56
......@@ -5,23 +5,39 @@ Tools for working with `nginx.conf`.
## Usage
```
usage:
nginx-conf-flatten <mode> <source_file> <output_file|output_directory>
modes:
nginx-conf-flatten <mode> <input_file> <output_file|output_directory>
```
Modes are:
flatten:
`flatten`:
flatten an nginx config file by inlining all includes recursively,
save to <output_file>
clean-directory:
`clean-directory`:
generate a cleaned nginx config directory, containing only the files
that are included from the source file, or files included from those,
that are included from the input file, or files included from those,
and so on; 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)
## Operation
The script creates a temporary directory (`/tmp/nginx-conf-flatten.????`) and works in there.
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.
## FAQ
- **why Bash? (Python|Ruby|Rust|OCaml|COBOL|FORTRAN) would have been so much (harder|better|faster|stronger)?**:
- **Why Bash? (Python|Ruby|Rust|OCaml|COBOL|FORTRAN) would have been so much (harder|better|faster|stronger)?**
Mainly because of compatibility. I expect this tool to be useful in weird server/docker deployments where ability to install dependencies is limited. Bash is available by default in a lot of such environments.
- **This is slow!**
And this is not a question!
Seriously though, yes, for large `nginx` configs with deeply nested includes it will take several seconds for the script to finish. There are surely ways to optimize this, and yes, if it was written in C it would be faster.
......@@ -125,13 +125,15 @@ function nginx-flatten {
# get the actual file glob
CI_FILE_GLOB="$( echo "$ci" | sed -r -e "s/^[0-9]+\:\s*include\s*([^;]+).*$/\1/" )"
CI_LINE="$( echo "$ci" | cut -d ':' -f 1 )"
CI_INDENT="$( echo "$ci" | cut -d ':' -f 2 | sed -r -e 's/^(\s*).*$/\1/' )"
DATE_EXT="$( date +%s%N )"
#debug " +-- previous line : $PREVIOUS_CI_LINE"
#debug " ci line : $CI_LINE"
debug " +-- previous line : $PREVIOUS_CI_LINE"
debug " ci line : $CI_LINE"
debug " indent : '$CI_INDENT'"
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############################################################\n### $ci \n############################################################" >> "$NGINX_CONFIG_COPY.$DATE_EXT"
cat $CI_FILE_GLOB >> "$NGINX_CONFIG_COPY.$DATE_EXT"
echo -e "\n############################################################\n### end $ci \n############################################################" >> "$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..."
......@@ -155,7 +157,7 @@ function print-usage {
cat - <<HEREDOC
usage:
$0 <mode> <source_file> <output_file|output_directory>
$0 <mode> <input_file> <output_file|output_directory>
modes:
......@@ -165,7 +167,7 @@ modes:
clean-directory:
generate a cleaned nginx config directory, containing only the files
that are included from the source file, or files included from those,
that are included from the input file, or files included from those,
and so on; output to <output_directory>
HEREDOC
......
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