2
0

cleanup of help, info and error messages

This commit is contained in:
Tilman Kranz 2021-11-24 02:13:45 +01:00
parent 3a8054d4d6
commit 35ca6e5142

View File

@ -68,19 +68,39 @@ yes=false
while true ; do
case "$1" in
-h|--help)
# shellcheck disable=SC2016
backticks='```'
cat << EOF
Usage: nft-edit-ruleset [--config [CONFIG_FILE]] [--fail] [--timeout SECONDS] [--yes]
Description:
Interactively edit the current nftables ruleset using the editor specified
by environment variable EDITOR (defaulting to vim). Optionally, revert
changes after a timeout given in seconds.
Options:
- \`-c\`, \`--config [CONFIG_FILE]\`: On successfully applying the
changes, save ruleset to \`CONFIG_FILE\` (default: $default_config_file).
- \`-f\`, \`--fail\`: Exit unsuccessfully if changes fail to apply.
- \`-h\`, \`--help\`: Display this message and exit.
- \`-t NUM\`, \`--timeout NUM\`: Revert changes after NUM seconds.
- \`-y\`, \`--yes\`: No confirmation before applying changes.
# SYNOPSIS
$backticks
nft-edit-ruleset [--config [CONFIG_FILE]] [--fail] [--timeout SECONDS] [--yes]
$backticks
# DESCRIPTION
Interactively edit the current nftables ruleset using the editor specified by
environment variable EDITOR (defaulting to vim). Optionally, revert changes
after a timeout given in seconds.
# OPTIONS
- \`-c\`, \`--config [CONFIG_FILE]\`:
On success, save ruleset to \`CONFIG_FILE\` (default: $default_config_file).
- \`-f\`, \`--fail\`:
Exit unsuccessfully if changes fail to apply.
- \`-h\`, \`--help\`:
Display this message and exit.
- \`-t NUM\`, \`--timeout NUM\`:
Revert changes after NUM seconds.
- \`-y\`, \`--yes\`:
No confirmation before applying changes.
# EXIT CODES
- 0: Success: No changes to apply or changes applied successfully.
- 1: Error: Usage error or failed or aborted changes.
# AUTHOR AND LICENSE
Copyleft 2021 Tilman Kranz <tilt@linuxfoo.de>
This software is distributed on the terms and conditions of the
MIT License [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)
EOF
exit 0
;;
@ -96,7 +116,7 @@ EOF
timeout=true
timeout_secs=$1
else
echo "ERROR: Missing argument for option \`--timeout\`, aborted."
echo "ERROR: Missing argument for option \`--timeout\`, aborted." >&2
exit 1
fi
;;
@ -107,7 +127,10 @@ EOF
shift 1
config_file=$1
elif test -z "$default_config_file" ; then
echo "ERROR: Option \`--config\` was used without an argument, but no default location of a file \"nftables.conf\" could be found (use \`--config CONFIG_FILE\` to specify a location); aborted."
echo \
"ERROR: Option \`--config\` was used without an argument," \
"but no default location of a file \"nftables.conf\" could be found" \
"(use \`--config CONFIG_FILE\` to specify a location); aborted." >&2
exit 1
else
config_file=$default_config_file
@ -117,7 +140,7 @@ EOF
:
;;
*)
echo "ERROR: Unknown or unexpected argument \"$1\"; aborted."
echo "ERROR: Unknown or unexpected argument \"$1\"; aborted." >&2
exit 1
;;
esac
@ -133,7 +156,7 @@ done
# Main Program
if ! test -t ; then
echo "ERROR: Not connected to a terminal; aborted."
echo "ERROR: Not connected to a terminal; aborted." >&2
exit 1
fi
@ -161,7 +184,7 @@ while true ; do
if store_config "$tmp" "$config_file" ; then
echo "Stored unchanged ruleset to config_file=\"$config_file\"."
else
echo "ERROR: Storing unchanged ruleset to config_file=\"$config_file\" failed."
echo "ERROR: Storing unchanged ruleset to config_file=\"$config_file\" failed." >&2
exit 1
fi
fi
@ -205,7 +228,7 @@ while true ; do
if store_config "$tmp" "$config_file" ; then
echo "Stored changed ruleset to config_file=\"$config_file\"."
else
echo "ERROR: Storing changed ruleset to config_file=\"$config_file\" failed."
echo "ERROR: Storing changed ruleset to config_file=\"$config_file\" failed." >&2
exit 1
fi
fi
@ -218,4 +241,4 @@ done
##
# Exit Codes
# - 0: Success: No changes to apply or changes applied successfully.
# - 1: Error: Usage error or failed or aborted changes.
# - 1: Error: Usage error, failed or aborted changes or failureto store configfile.