From 3304fde7a309c28031bc3e41671ff30468de4cc1 Mon Sep 17 00:00:00 2001 From: C-Duv Date: Sat, 11 Feb 2017 14:58:03 +0100 Subject: [PATCH 1/3] Adds "-h" option to display usage guide --- README.md | 5 ++++- send-notification.sh | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8d4a0a6..bd66e6b 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,14 @@ send-notification.sh "All your base are belong to us" ``` ``` - uptime | send-notification.sh +uptime | send-notification.sh ``` +See `send-notification.sh -h`. + ### Configuration : +Script can be configured Edit `send-notification.sh` and set the following variables: * `USER_LOGIN` diff --git a/send-notification.sh b/send-notification.sh index f22de58..cda4383 100644 --- a/send-notification.sh +++ b/send-notification.sh @@ -8,10 +8,25 @@ # # Nécessite: sed, sh et wget # -# Possible usages: -# send-notification.sh "All your base are belong to us" -# echo "All your base are belong to us" | send-notification.sh -# uptime | send-notification.sh +# Possible usages: see usage_help() + + +readonly PROGNAME=$(basename $0) + +usage_help () { + echo "Possible usages:" + echo "* ${PROGNAME} [options] [message]" + echo "* echo \"All your base are belong to us\" | ${PROGNAME} [options]" + echo "" + echo "Options:" + echo "* -h display this help" +} + +while getopts "h" option; do + case "$option" in + h) usage_help ; exit 0 ;; + esac +done ## From 0105907e2061c067001a52ab8b20fa3b4057a205 Mon Sep 17 00:00:00 2001 From: C-Duv Date: Sat, 11 Feb 2017 15:18:18 +0100 Subject: [PATCH 2/3] Adds meaning about configuration variables in README This commit explains, in the README, what each configuration variable do. --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bd66e6b..b640ae3 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,13 @@ See `send-notification.sh -h`. Script can be configured Edit `send-notification.sh` and set the following variables: +* `USER_LOGIN`: The login to use the API. +* `API_KEY`: The secret key associated to `USER_LOGIN`. +* `MESSAGE_HEADER` (Optional): Will be prepended to all the messages. +* `MESSAGE_FOOTER` (Optional): Will be appended to all the messages. +* `NEWLINE_CHAR` (Optional): Char to use to create a new line (it depends on + receiving terminal). -* `USER_LOGIN` -* `API_KEY` -* `MESSAGE_HEADER` (Optional) -* `MESSAGE_FOOTER` (Optional) PHP Client ---------- From 29abae8a06cf365aaee85625e782a64aeffd4969 Mon Sep 17 00:00:00 2001 From: C-Duv Date: Sat, 11 Feb 2017 15:20:55 +0100 Subject: [PATCH 3/3] Adds support for configuration file The script can now be configured using configuration files. If a file named ".freemobile-smsapi" is located next to the script or in user's home directory, such file will be read to load Alternatively, a file path can be specified using `-c` option at runtime. This commit makes the script accept option to specify configuration filepath and also look for configuration files in specific places. --- README.md | 25 +++++++++++++++++++---- send-notification.sh | 48 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b640ae3..96e14d8 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,19 @@ Shell Client ### Possible usages: ``` -send-notification.sh "All your base are belong to us" +send-notification.sh [options] "All your base are belong to us" ``` ``` -uptime | send-notification.sh +uptime | send-notification.sh [options] ``` See `send-notification.sh -h`. ### Configuration : -Script can be configured -Edit `send-notification.sh` and set the following variables: +Script can be configured using the following variables: + * `USER_LOGIN`: The login to use the API. * `API_KEY`: The secret key associated to `USER_LOGIN`. * `MESSAGE_HEADER` (Optional): Will be prepended to all the messages. @@ -32,6 +32,23 @@ Edit `send-notification.sh` and set the following variables: * `NEWLINE_CHAR` (Optional): Char to use to create a new line (it depends on receiving terminal). +Theses can be directly changed in the `send-notification.sh` file or set in a +separate configuration file named `.freemobile-smsapi` next to the script or in +the user's home directory. A given filepath can also be specify using the `-c` +script at runtime (`send-notification.sh -c foobar`). + +Configuration load process is as follows: + +# Uses what's in `send-notification.sh` file. +# If `-c` option is set, test given file existence and uses what's inside. + Will not load any other configuration file (stops load process here). +# Checks if a `.freemobile-smsapi` file exists next to the script and uses + what's inside. Will not load any other configuration file (stops load process + here). +# Checks if a `.freemobile-smsapi` file exists in user's home directory + (`${HOME}`) and uses what's inside. Will not load any other configuration file + (stops load process here). + PHP Client ---------- diff --git a/send-notification.sh b/send-notification.sh index cda4383..d4ef30c 100644 --- a/send-notification.sh +++ b/send-notification.sh @@ -12,6 +12,14 @@ readonly PROGNAME=$(basename $0) +readonly PROGDIR=$(readlink -m $(dirname $0)) + +usage_error () { + echo "ERROR: ${1}" >&2 + echo "" + usage_help + exit 1 +} usage_help () { echo "Possible usages:" @@ -19,11 +27,15 @@ usage_help () { echo "* echo \"All your base are belong to us\" | ${PROGNAME} [options]" echo "" echo "Options:" + echo "* -c file specify configuration file" echo "* -h display this help" } -while getopts "h" option; do +CONFIG_FILE="" +while getopts "c:h" option; do case "$option" in + c) CONFIG_FILE=${OPTARG} ;; + :) usage_error "Invalid arguments" ;; h) usage_help ; exit 0 ;; esac done @@ -66,6 +78,40 @@ MESSAGE_FOOTER=" Le serveur de la maison" +## +## Fichier de configuration +## + +if [ -n "${CONFIG_FILE}" ]; then + if [ -e "${CONFIG_FILE}" ]; then + . "./${CONFIG_FILE}" + else + echo "ERROR: Configuration file \"${CONFIG_FILE}\" does not exists." >&2 + exit 2 + fi +else + if [ -e "${PROGDIR}/.freemobile-smsapi" ]; then + . "${PROGDIR}/.freemobile-smsapi" + elif [ -e "${HOME}/.freemobile-smsapi" ]; then + . "${HOME}/.freemobile-smsapi" + fi +fi + +## +## Vérifications des paramètres requis +## + +if [ -z "${USER_LOGIN}" ] \ + || [ -z "${API_KEY}" ] \ + || [ -z "${SMSAPI_BASEURL}" ] \ + || [ -z "${SMSAPI_SEND_ACTION}" ] \ +; then + echo "ERROR: Either USER_LOGIN, API_KEY, SMSAPI_BASEURL or " \ + "SMSAPI_SEND_ACTION is not set" >&2 + exit 2 +fi + + ## ## Traitement du message ##