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 while true ; do
case "$1" in case "$1" in
-h|--help) -h|--help)
# shellcheck disable=SC2016
backticks='```'
cat << EOF cat << EOF
Usage: nft-edit-ruleset [--config [CONFIG_FILE]] [--fail] [--timeout SECONDS] [--yes] # SYNOPSIS
Description: $backticks
Interactively edit the current nftables ruleset using the editor specified nft-edit-ruleset [--config [CONFIG_FILE]] [--fail] [--timeout SECONDS] [--yes]
by environment variable EDITOR (defaulting to vim). Optionally, revert $backticks
changes after a timeout given in seconds.
Options: # DESCRIPTION
- \`-c\`, \`--config [CONFIG_FILE]\`: On successfully applying the Interactively edit the current nftables ruleset using the editor specified by
changes, save ruleset to \`CONFIG_FILE\` (default: $default_config_file). environment variable EDITOR (defaulting to vim). Optionally, revert changes
- \`-f\`, \`--fail\`: Exit unsuccessfully if changes fail to apply. after a timeout given in seconds.
- \`-h\`, \`--help\`: Display this message and exit.
- \`-t NUM\`, \`--timeout NUM\`: Revert changes after NUM seconds. # OPTIONS
- \`-y\`, \`--yes\`: No confirmation before applying changes. - \`-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 EOF
exit 0 exit 0
;; ;;
@ -96,7 +116,7 @@ EOF
timeout=true timeout=true
timeout_secs=$1 timeout_secs=$1
else else
echo "ERROR: Missing argument for option \`--timeout\`, aborted." echo "ERROR: Missing argument for option \`--timeout\`, aborted." >&2
exit 1 exit 1
fi fi
;; ;;
@ -107,7 +127,10 @@ EOF
shift 1 shift 1
config_file=$1 config_file=$1
elif test -z "$default_config_file" ; then 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 exit 1
else else
config_file=$default_config_file 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 exit 1
;; ;;
esac esac
@ -133,7 +156,7 @@ done
# Main Program # Main Program
if ! test -t ; then if ! test -t ; then
echo "ERROR: Not connected to a terminal; aborted." echo "ERROR: Not connected to a terminal; aborted." >&2
exit 1 exit 1
fi fi
@ -161,7 +184,7 @@ while true ; do
if store_config "$tmp" "$config_file" ; then if store_config "$tmp" "$config_file" ; then
echo "Stored unchanged ruleset to config_file=\"$config_file\"." echo "Stored unchanged ruleset to config_file=\"$config_file\"."
else 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 exit 1
fi fi
fi fi
@ -205,7 +228,7 @@ while true ; do
if store_config "$tmp" "$config_file" ; then if store_config "$tmp" "$config_file" ; then
echo "Stored changed ruleset to config_file=\"$config_file\"." echo "Stored changed ruleset to config_file=\"$config_file\"."
else 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 exit 1
fi fi
fi fi
@ -218,4 +241,4 @@ done
## ##
# Exit Codes # Exit Codes
# - 0: Success: No changes to apply or changes applied successfully. # - 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.