Improved template feature with named options

This commit is contained in:
Héctor Molinero Fernández 2020-10-28 21:15:14 +01:00
commit f1697f0e8d
11 changed files with 55 additions and 42 deletions

13
hblock
View file

@ -198,8 +198,8 @@ showHelp() {
- Environment variable: HBLOCK_WRAP\\n
- Default value: ${wrapDefault?}\\n
-T, --template <TEMPLATE>\\n
POSIX BREs replacement applied to each entry.\\n
Capturing group backreferences: \\1 = <DOMAIN>, \\2 = <REDIRECTION>\\n
Template applied to each entry.\\n
- Variables: %D = <DOMAIN>, %R = <REDIRECTION>\\n
- Environment variable: HBLOCK_TEMPLATE\\n
- Default value: ${templateDefault?}\\n
-C, --comment <COMMENT>\\n
@ -347,8 +347,8 @@ main() {
wrapDefault="${HBLOCK_WRAP-1}"
wrap="${wrapDefault?}"
# POSIX BREs replacement applied to each entry.
templateDefault="${HBLOCK_TEMPLATE-\2 \1}"
# Template applied to each entry.
templateDefault="${HBLOCK_TEMPLATE-%R %D}"
template="${templateDefault?}"
# Character used for comments.
@ -581,9 +581,8 @@ main() {
&& mv -f -- "${blocklistFile:?}.aux.1" "${blocklistFile:?}"
fi
sed -e 's/$/\t'"$(quoteSubst "${redirection?}")"'/' \
-e 's/^\(.*\)\t\(.*\)$/'"${template?}"'/' \
-- "${blocklistFile:?}" > "${blocklistFile:?}.aux.1" \
awk -v T="${template?}" -v R="${redirection?}" '{E=T;D=$0;gsub("%D",D,E);gsub("%R",R,E);print(E)}' \
< "${blocklistFile:?}" > "${blocklistFile:?}.aux.1" \
&& mv -f -- "${blocklistFile:?}.aux.1" "${blocklistFile:?}"
fi

View file

@ -92,13 +92,13 @@ Break blocklist lines after this number of entries.
.HP
\fB\-T\fR, \fB\-\-template\fR <TEMPLATE>
.IP
POSIX BREs replacement applied to each entry.
Template applied to each entry.
.IP
Capturing group backreferences: \e1 = <DOMAIN>, \e2 = <REDIRECTION>
\- Variables: %D = <DOMAIN>, %R = <REDIRECTION>
.IP
\- Environment variable: HBLOCK_TEMPLATE
.IP
\- Default value: \e2 \e1
\- Default value: %R %D
.HP
\fB\-C\fR, \fB\-\-comment\fR <COMMENT>
.IP

View file

@ -100,14 +100,13 @@ OPTIONS
**-T**, **--template** &lt;TEMPLATE&gt;
> POSIX BREs replacement applied to each entry.
> Template applied to each entry.
>
> Capturing group backreferences: \\1 = &lt;DOMAIN&gt;, \\2 =
> &lt;REDIRECTION&gt;
> \- Variables: %D = &lt;DOMAIN&gt;, %R = &lt;REDIRECTION&gt;
>
> \- Environment variable: HBLOCK\_TEMPLATE
>
> \- Default value: \\2 \\1
> \- Default value: %R %D
**-C**, **--comment** &lt;COMMENT&gt;

View file

@ -29,7 +29,7 @@ main() {
export HBLOCK_REDIRECTION=''
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='||\1^'
export HBLOCK_TEMPLATE='||%D^'
export HBLOCK_COMMENT='!'
export HBLOCK_LENIENT='false'

View file

@ -29,7 +29,7 @@ main() {
export HBLOCK_REDIRECTION='0.0.0.0'
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='address=\/\1\/\2'
export HBLOCK_TEMPLATE='address=/%D/%R'
export HBLOCK_COMMENT='#'
export HBLOCK_LENIENT='false'

View file

@ -29,7 +29,7 @@ main() {
export HBLOCK_REDIRECTION=''
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='\1'
export HBLOCK_TEMPLATE='%D'
export HBLOCK_COMMENT=''
export HBLOCK_LENIENT='false'

View file

@ -35,7 +35,7 @@ main() {
export HBLOCK_REDIRECTION=''
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='\1 CNAME .'
export HBLOCK_TEMPLATE='%D CNAME .'
export HBLOCK_COMMENT=';'
export HBLOCK_LENIENT='false'

View file

@ -12,8 +12,6 @@ main() {
target="${2:?}"
hblock="${3:-hblock}"
ENL="$(printf '\\\nx')"; ENL="${ENL%x}"
export HBLOCK_HEADER_FILE='builtin'
export HBLOCK_HEADER=''
@ -31,7 +29,7 @@ main() {
export HBLOCK_REDIRECTION='0.0.0.0'
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='local-zone: "\1" redirect'"${ENL:?}"'local-data: "\1 A \2"'
export HBLOCK_TEMPLATE='local-zone: "%D" redirect\nlocal-data: "%D A %R"'
export HBLOCK_COMMENT='#'
export HBLOCK_LENIENT='false'

View file

@ -32,7 +32,7 @@ main() {
export HBLOCK_REDIRECTION='0.0.0.0'
export HBLOCK_WRAP='1'
export HBLOCK_TEMPLATE='\2 \1'
export HBLOCK_TEMPLATE='%R %D'
export HBLOCK_COMMENT='#'
export HBLOCK_LENIENT='false'

View file

@ -14,21 +14,38 @@ ff02::3 ip6-allhosts
# END HEADER
# BEGIN BLOCKLIST
0.0.0.0 entry-with-comment-000.com
0.0.0.0 entry-with-comment-001.com
0.0.0.0 entry-with-comment-002.com
0.0.0.0 entry-with-comment-003.com
0.0.0.0 entry-with-comment-004.com
0.0.0.0 entry-with-prefix-000.com
0.0.0.0 entry-with-prefix-001.com
0.0.0.0 entry-with-prefix-002.com
0.0.0.0 entry-with-prefix-003.com
0.0.0.0 entry-with-prefix-004.com
0.0.0.0 entry-with-prefix-005.com
0.0.0.0 entry-with-prefix-006.com
0.0.0.0 hblock-check.molinero.dev
0.0.0.0 single-entry-000.com
0.0.0.0 single-entry-001.com
0.0.0.0 single-entry-002.com
0.0.0.0 single-entry-003.com
entry-with-comment-000.com
└─ 0.0.0.0
entry-with-comment-001.com
└─ 0.0.0.0
entry-with-comment-002.com
└─ 0.0.0.0
entry-with-comment-003.com
└─ 0.0.0.0
entry-with-comment-004.com
└─ 0.0.0.0
entry-with-prefix-000.com
└─ 0.0.0.0
entry-with-prefix-001.com
└─ 0.0.0.0
entry-with-prefix-002.com
└─ 0.0.0.0
entry-with-prefix-003.com
└─ 0.0.0.0
entry-with-prefix-004.com
└─ 0.0.0.0
entry-with-prefix-005.com
└─ 0.0.0.0
entry-with-prefix-006.com
└─ 0.0.0.0
hblock-check.molinero.dev
└─ 0.0.0.0
single-entry-000.com
└─ 0.0.0.0
single-entry-001.com
└─ 0.0.0.0
single-entry-002.com
└─ 0.0.0.0
single-entry-003.com
└─ 0.0.0.0
# END BLOCKLIST

View file

@ -15,17 +15,17 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
hblock="${1:-hblock}"
export HBLOCK_TEMPLATE='\2 \1'
export HBLOCK_TEMPLATE='%R %D'
printf 'Test: "-T" short option\n'
actual="$(runInTestShell "${hblock:?}" -qO- -T '\2 \1')"
actual="$(runInTestShell "${hblock:?}" -qO- -T '%D\n\t└─ %R')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-template-opt.out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--template" long option\n'
actual="$(runInTestShell "${hblock:?}" -qO- --template='\2 \1')"
actual="$(runInTestShell "${hblock:?}" -qO- --template='%D\n\t└─ %R')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-template-opt.out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1