Merge branch 'evol/supports_configuration_file'

This commit is contained in:
C-Duv 2017-02-11 15:21:43 +01:00
commit a9efb5606c
2 changed files with 94 additions and 11 deletions

View File

@ -12,21 +12,43 @@ Shell Client
### Possible usages: ### 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 : ### Configuration :
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.
* `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).
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).
* `USER_LOGIN`
* `API_KEY`
* `MESSAGE_HEADER` (Optional)
* `MESSAGE_FOOTER` (Optional)
PHP Client PHP Client
---------- ----------

View File

@ -8,10 +8,37 @@
# #
# Nécessite: sed, sh et wget # Nécessite: sed, sh et wget
# #
# Possible usages: # Possible usages: see usage_help()
# 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 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:"
echo "* ${PROGNAME} [options] [message]"
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"
}
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
## ##
@ -51,6 +78,40 @@ MESSAGE_FOOTER="
Le serveur de la maison" 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 ## Traitement du message
## ##