Add "version" and extract "help" options

This commit is contained in:
Jérémy Lecour 2020-10-07 20:50:17 +02:00 committed by Jérémy Lecour
parent 6fe81c7077
commit c1fbe6394f
23 changed files with 94 additions and 47 deletions

View File

@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* bkctld: add a [-V|--version] option to display release number
* bkctld: add a [-h|--help|-?] option to display help message
### Changed
### Deprecated

33
bkctld
View File

@ -34,9 +34,30 @@ fi
# shellcheck source=lib/includes
. "${LIBDIR}/includes"
# Parse options
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
while :; do
case $1 in
-h|-\?|--help)
show_help
exit 0
;;
-V|--version)
show_version
exit 0
;;
*)
# Default case: If no more options then break out of the loop.
break
;;
esac
shift
done
subcommand="${1:-}"
case "${subcommand}" in
"inc" | "rm" | "check-jails" | "check-setup" | "stats" | "help" | "list")
"inc" | "rm" | "check-jails" | "check-setup" | "stats" | "list")
"${LIBDIR}/bkctld-${subcommand}"
;;
"check")
@ -50,14 +71,14 @@ case "${subcommand}" in
elif [ "${option}" = "last" ]; then
"${LIBDIR}/bkctld-check-last-incs"
else
"${LIBDIR}/bkctld-help"
show_help
exit 1
fi
;;
"init" | "is-on")
jail_name="${2:-}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help"
show_help
exit 1
else
"${LIBDIR}/bkctld-${subcommand}" "${jail_name}"
@ -67,7 +88,7 @@ case "${subcommand}" in
jail_name="${2:-}"
option="${3:-}"
if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help"
show_help
exit 1
else
"${LIBDIR}/bkctld-${subcommand}" "${jail_name}" "${option}"
@ -80,7 +101,7 @@ case "${subcommand}" in
"${LIBDIR}/bkctld-${subcommand}" "${jail}"
done
elif [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help"
show_help
exit 1
else
"${LIBDIR}/bkctld-${subcommand}" "${jail_name}"
@ -97,7 +118,7 @@ case "${subcommand}" in
fi
;;
*)
"${LIBDIR}/bkctld-help"
show_help
exit 1
;;
esac

View File

@ -1,4 +1,8 @@
#!/bin/sh
#
# Run check on all expected incs of all jails
# Usage: check-incs all
#
set -u

View File

@ -1,7 +1,7 @@
#!/bin/sh
#
# Run check on jails (NRPE output)
# Usage: check
# Usage: check-jails
#
# shellcheck source=./includes

View File

@ -1,4 +1,8 @@
#!/bin/sh
#
# Run check on the last inc of all jails
# Usage: check-incs last
#
set -u

View File

@ -1,7 +1,7 @@
#!/bin/sh
#
# Run check on jails (NRPE output)
# Usage: check
# Usage: check-setup
#
# shellcheck source=./includes

View File

@ -10,7 +10,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -1,22 +0,0 @@
#!/bin/sh
#
# Print this help
# Usage: help
#
# shellcheck source=./includes
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
cat <<EOF
Usage: bkctld <subcommand> [options]
Subcommands:
EOF
for subcommand in ${LIBDIR}/bkctld-*; do
name=$(basename "${subcommand}"|sed 's/^bkctld-//')
desc=$(grep -E "^#" "${subcommand}"|sed -n '3p'|sed "s/^# //")
usage=$(grep -E "^# Usage: ${name}" "${subcommand}"|sed "s/^# Usage: ${name}//")
printf " %- 10s %- 30s %- 40s\n" "${name}" "${usage}" "${desc}"
done
printf "\n"

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -11,7 +11,7 @@ jail_name="${1:?}"
ip="${2:-}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -10,7 +10,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -11,7 +11,7 @@ jail_name="${1:?}"
keyfile="${2:-}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -11,7 +11,7 @@ jail_name="${1:?}"
port="${2:-}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")
incs_path=$(incs_path "${jail_name}")

View File

@ -11,7 +11,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
[ -d "${JAILDIR}/${jail_name}" ] || error "${jail_name} : jail is missing.\nUse '$0 status [all]' to get the status of all jails."

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")
jail_config_dir=$(jail_config_dir "${jail_name}")

View File

@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -1,7 +1,7 @@
#!/bin/sh
#
# Update jail <jailname> or all
# Usage: update <jailname>|all
# Upgrade chroot components for jail <jailname> or all
# Usage: upgrade-config <jailname>|all
#
# shellcheck source=./includes
@ -9,7 +9,7 @@ LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ ! -n "${jail_name}" ]; then
"${LIBDIR}/bkctld-help" && exit 1
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")

View File

@ -4,6 +4,10 @@
#
[ -f /etc/default/bkctld ] && . /etc/default/bkctld
# shellcheck disable=SC2034
VERSION="2.5.1"
LIBDIR=${LIBDIR:-/usr/lib/bkctld}
CONFDIR="${CONFDIR:-/etc/evobackup}"
BACKUP_DISK="${BACKUP_DISK:-}"
@ -24,6 +28,39 @@ CRITICAL="${CRITICAL:-48}"
WARNING="${WARNING:-24}"
DUC=$(command -v duc-nox || command -v duc)
show_version() {
cat <<END
bkctld version ${VERSION}
Copyright 2004-2020 Evolix <info@evolix.fr>,
Victor Laborie <vlaborie@evolix.fr>,
Jérémy Lecour <jlecour@evolix.fr>
and others.
bkctld comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
See the GNU Affero General Public License v3.0 for details.
END
}
show_help() {
cat <<EOF
Usage: bkctld [options] <subcommand> [arguments]
Options
-h|--help|-? Display help
-V|--version Display version, authors and license
Subcommands:
EOF
for subcommand in ${LIBDIR}/bkctld-*; do
name=$(basename "${subcommand}"|sed 's/^bkctld-//')
desc=$(grep -E "^#" "${subcommand}"|sed -n '3p'|sed "s/^# //")
usage=$(grep -E "^# Usage: ${name}" "${subcommand}"|sed "s/^# Usage: ${name}//")
printf " %- 15s %- 30s %- 40s\n" "${name}" "${usage}" "${desc}"
done
printf "\n"
}
log_date() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")]"
}