Skip to content

Commit

Permalink
Allow to use env variables instea of config file
Browse files Browse the repository at this point in the history
  • Loading branch information
pi committed Feb 20, 2021
1 parent e71d2a0 commit e1278bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 23 additions & 9 deletions nc-ddns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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="<ErrCount>0</ErrCount>"
Expand Down Expand Up @@ -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"
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1278bf

Please sign in to comment.