diff --git a/README.md b/README.md index 351a144..39ea035 100644 --- a/README.md +++ b/README.md @@ -13,23 +13,24 @@ because of cache TTL, etc ... --> ## Configuration -You need to create a `ddns-info.txt` file with the Namecheap domain details. +Provide a configuration via a set of environment variables, or using a configuration +file (with the same variables defined). -E.g., if your DDNS domain name is `ddns.example.com`, the content of that file -should be: +E.g., if your DDNS domain name is `ddns.example.com`, the content of the env variables +or of the configuration files should be: ``` -declare -r ddnsHost="ddns" -declare -r ddnsDomain="example.com" -declare -r ddnsPassword="namecheap-ddns-password" +NC_DDNS_HOST=myhost +NC_DDNS_DOMAIN=example.com +NC_DDNS_PASSWORD=namecheap-ddns-password ``` -The `ddnsPassword` shall be retrieved from the Namecheap web control panel. +The `NC_DDNS_PASSWORD` shall be retrieved from the Namecheap web control panel. ## Usage ``` Usage: nc-ddns [-c config-file] [-d] [−n] [-h] [−f] Options: - -c : Configuration file path (default path: '$HOME/ddns-info.txt') + -c : Configuration file path (default: get config via env variables) -d : print debug info (default: false) -f : Force update (invalidate the cached IP) -h : help diff --git a/nc-ddns.sh b/nc-ddns.sh index 9a21f20..72eafbf 100755 --- a/nc-ddns.sh +++ b/nc-ddns.sh @@ -8,7 +8,7 @@ set -euo pipefail declare -r myName="nc-ddns" -declare configFile="$HOME/ddns-info.txt" +declare configFile= declare -r cachedIpFile="${XDG_RUNTIME_DIR:-/tmp}/ddns-ip" declare -r urlPrefix="https://dynamicdns.park-your-domain.com/update" declare -r respSuccessPattern="0" @@ -63,12 +63,23 @@ read_config() { local ip="$1" [ -n "$ip" ] || { echo "ERR: need an IP"; exit -1; } - [ -f "$configFile" ] || { echo "ERR: missing DDNS info file \"$configFile\""; exit -1; } - . "$configFile" + if [ -f "$configFile" ]; then + . "$configFile" + else + echo "WARN: missing DDNS info file. Using config from env variables" - urlParams[host]="$ddnsHost" - urlParams[domain]="$ddnsDomain" - urlParams[password]="$ddnsPassword" + if [ -z ${NC_DDNS_HOST:-} ] || + [ -z ${NC_DDNS_DOMAIN:-} ] || + [ -z ${NC_DDNS_PASSWORD:-} ]; then + echo "ERR: could not get DDNS confg from env variables" + exit -1 + fi + + fi + + urlParams[host]="$NC_DDNS_HOST" + urlParams[domain]="$NC_DDNS_DOMAIN" + urlParams[password]="$NC_DDNS_PASSWORD" urlParams[ip]="$1" } @@ -83,8 +94,11 @@ ip_is_cached() { # read return > 0 when EOF is met... read -r cachedIp < "$cachedIpFile" || true - [ -z "$cachedIp" ] && { echo "ERR: cached IP file content is invalid"; exit -1; } - info "cached IP: $cachedIp" + if [ -z "$cachedIp" ]; then + echo "WARN: cached IP file content is invalid" + else + info "cached IP: $cachedIp" + fi else echo "WARN: missing DDNS cached IP file: \"$cachedIpFile\"" fi @@ -159,7 +173,7 @@ parse_args() { printf "\nUsage: %s [-c config-file] [-d] [−n] [-h] [−f]\n" "$myName" printf " Options: - -c : Configuration file path (default path: '$HOME/ddns-info.txt') + -c : Configuration file path (default: get config via env variables) -d : print debug info (default: false) -f : Force update (invalidate the cached IP) -h : help