Browse Source

Add '--debug' and '--speedtest' arguments

Adding switches by using 'argbash' (ref: https://argbash.io/):
* '--debug' & '-d' switch to activate debug mode
* '--speedtest' & '-s' switch to pass arguments to speedtest

Ex:
./docker-entrypoint.sh --debug
./docker-entrypoint.sh --speedtest "-v -s 12345"

Signed-off-by: Jeremy MAURO <jeremy.mauro@gmail.com>
Jeremy MAURO 3 years ago
parent
commit
da123e43eb
1 changed files with 137 additions and 8 deletions
  1. 137 8
      scripts/docker-entrypoint.sh

+ 137 - 8
scripts/docker-entrypoint.sh

@@ -1,11 +1,138 @@
 #!/usr/bin/env bash
+#
+# This is a rather minimal example Argbash potential
+# Example taken from http://argbash.readthedocs.io/en/stable/example.html
+#
+# ARG_OPTIONAL_SINGLE([speedtest],[s],[optional argument extra arguments to speedtest])
+# ARG_OPTIONAL_BOOLEAN([debug],[d],[boolean optional argument activate debug mode])
+# ARG_HELP([The general script's help msg])
+# ARGBASH_GO()
+# needed because of Argbash --> m4_ignore([
+### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
+# Argbash is a bash code generator used to get arguments parsing right.
+# Argbash is FREE SOFTWARE, see https://argbash.io for more info
+# Generated online by https://argbash.io/generate
 
-DEBUG=0
-if [ "$1" == "debug" ]; then
-       DEBUG=1
-       set -x
-       shift
-fi
+
+# # When called, the process ends.
+# Args:
+# 	$1: The exit message (print to stderr)
+# 	$2: The exit code (default is 1)
+# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
+# Example:
+# 	test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
+die()
+{
+	local _ret="${2:-1}"
+	test "${_PRINT_HELP:-no}" = yes && print_help >&2
+	echo "$1" >&2
+	exit "${_ret}"
+}
+
+
+# Function that evaluates whether a value passed to it begins by a character
+# that is a short option of an argument the script knows about.
+# This is required in order to support getopts-like short options grouping.
+begins_with_short_option()
+{
+	local first_option all_short_options='sdh'
+	first_option="${1:0:1}"
+	test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
+}
+
+# THE DEFAULTS INITIALIZATION - OPTIONALS
+_arg_speedtest=
+_arg_debug="off"
+
+
+# Function that prints general usage of the script.
+# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
+# and it makes sense to remind the user how the script is supposed to be called.
+print_help()
+{
+	printf '%s\n' "The general script's help msg"
+	printf 'Usage: %s [-s|--speedtest <arg>] [-d|--(no-)debug] [-h|--help]\n' "$0"
+	printf '\t%s\n' "-s, --speedtest: optional argument extra arguments to speedtest (no default)"
+	printf '\t%s\n' "-d, --debug, --no-debug: boolean optional argument activate debug mode (off by default)"
+	printf '\t%s\n' "-h, --help: Prints help"
+}
+
+
+# The parsing of the command-line
+parse_commandline()
+{
+	while test $# -gt 0
+	do
+		_key="$1"
+		case "$_key" in
+			# We support whitespace as a delimiter between option argument and its value.
+			# Therefore, we expect the --speedtest or -s value.
+			# so we watch for --speedtest and -s.
+			# Since we know that we got the long or short option,
+			# we just reach out for the next argument to get the value.
+			-s|--speedtest)
+				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
+				_arg_speedtest="$2"
+				shift
+				;;
+			# We support the = as a delimiter between option argument and its value.
+			# Therefore, we expect --speedtest=value, so we watch for --speedtest=*
+			# For whatever we get, we strip '--speedtest=' using the ${var##--speedtest=} notation
+			# to get the argument value
+			--speedtest=*)
+				_arg_speedtest="${_key##--speedtest=}"
+				;;
+			# We support getopts-style short arguments grouping,
+			# so as -s accepts value, we allow it to be appended to it, so we watch for -s*
+			# and we strip the leading -s from the argument string using the ${var##-s} notation.
+			-s*)
+				_arg_speedtest="${_key##-s}"
+				;;
+			# The debug argurment doesn't accept a value,
+			# we expect the --debug or -d, so we watch for them.
+			-d|--no-debug|--debug)
+				_arg_debug="on"
+				test "${1:0:5}" = "--no-" && _arg_debug="off"
+				;;
+			# We support getopts-style short arguments clustering,
+			# so as -d doesn't accept value, other short options may be appended to it, so we watch for -d*.
+			# After stripping the leading -d from the argument, we have to make sure
+			# that the first character that follows coresponds to a short option.
+			-d*)
+				_arg_debug="on"
+				_next="${_key##-d}"
+				if test -n "$_next" -a "$_next" != "$_key"
+				then
+					{ begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
+				fi
+				;;
+			# See the comment of option '--debug' to see what's going on here - principle is the same.
+			-h|--help)
+				print_help
+				exit 0
+				;;
+			# See the comment of option '-d' to see what's going on here - principle is the same.
+			-h*)
+				print_help
+				exit 0
+				;;
+			*)
+				_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
+				;;
+		esac
+		shift
+	done
+}
+
+# Now call all the functions defined above that are needed to get the job done
+parse_commandline "$@"
+
+# OTHER STUFF GENERATED BY Argbash
+
+### END OF CODE GENERATED BY Argbash (sortof) ### ])
+# [ <-- needed because of Argbash
+[ "${_arg_debug}" == 'on' ] && _arg_debug=1 || _arg_debug=0
+[ ${_arg_debug} -eq 1 ] && set -x
 
 # InfluxDB variables
 influxdb_proto=${INFLUXDB_PROTO:-http}
@@ -16,7 +143,7 @@ influxdb_db=${INFLUXDB_DB:-speedtest}
 influxdb_url="${influxdb_proto}://${influxdb_host}:${influxdb_port}"
 
 # run speedtest & store result
-json_result=$(speedtest -f json --accept-license --accept-gdpr)
+json_result=$(speedtest -f json --accept-license --accept-gdpr ${_arg_speedtest})
 
 declare -A results
 # Extract data from speedtest result
@@ -41,7 +168,7 @@ do
 done
 INFLUXDB_APPEND="speedtest,result_id=${result_id} ${INFLUXDB_APPEND}"
 
-[ ${DEBUG} -eq 1  ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
+[ ${_arg_debug} -eq 1  ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
 
 # Ensure InfluxDB database exists
 curl \
@@ -51,3 +178,5 @@ curl \
 curl \
     -d "${INFLUXDB_APPEND}" \
     "${influxdb_url}/write?db=${influxdb_db}"
+
+# ] <-- needed because of Argbash