cleanup according to styleguide / ShellCheck

This commit is contained in:
der-berni 2020-04-09 12:17:08 +02:00 committed by GitHub
parent 5fac282ee0
commit da7b1fb014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,10 @@
# Created: 2019-02-17 # Created: 2019-02-17
# Fixed by: @der-berni # Fixed by: @der-berni
# Modified: 2020-04-07 # Modified: 2020-04-07
# #
# Use ONECOM_KeepCnameProxy to keep the CNAME DNS record
# export ONECOM_KeepCnameProxy="1"
#
# export ONECOM_User="username" # export ONECOM_User="username"
# export ONECOM_Password="password" # export ONECOM_Password="password"
# #
@ -29,41 +32,41 @@ dns_one_add() {
_err "root domain not found" _err "root domain not found"
return 1 return 1
fi fi
subdomain="${_sub_domain}" subdomain="${_sub_domain}"
maindomain=${_domain} maindomain=${_domain}
useProxy=0 useProxy=0
if [ "${_sub_domain}" = "_acme-challenge" ]; then if [ "${_sub_domain}" = "_acme-challenge" ]; then
subdomain="proxy${_sub_domain}" subdomain="proxy${_sub_domain}"
useProxy=1 useProxy=1
fi fi
_debug subdomain "$subdomain" _debug subdomain "$subdomain"
_debug maindomain "$maindomain" _debug maindomain "$maindomain"
if [ $useProxy -eq 1 ]; then if [ $useProxy -eq 1 ]; then
#Check if the CNAME exists #Check if the CNAME exists
_dns_one_getrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain" _dns_one_getrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain"
if [ -z "$id" ]; then if [ -z "$id" ]; then
_info "$(__red "Add CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")" _info "$(__red "Add CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")"
_dns_one_addrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain" _dns_one_addrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain"
_info "Not valid yet, let's wait 1 hour to take effect." _info "Not valid yet, let's wait 1 hour to take effect."
_sleep 3600 _sleep 3600
fi fi
fi fi
#Check if the TXT exists #Check if the TXT exists
_dns_one_getrecord "TXT" "$subdomain" "$txtvalue" _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
if [ ! -z "$id" ]; then if [ -n "$id" ]; then
_info "$(__green "Txt record with the same value found. Skip adding.")" _info "$(__green "Txt record with the same value found. Skip adding.")"
return 0 return 0
fi fi
_dns_one_addrecord "TXT" "$subdomain" "$txtvalue" _dns_one_addrecord "TXT" "$subdomain" "$txtvalue"
if [ -z "$id" ]; then if [ -z "$id" ]; then
_err "Add CNAME record error." _err "Add TXT record error."
return 1 return 1
else else
_info "$(__green "Added, OK ($id)")" _info "$(__green "Added, OK ($id)")"
@ -88,43 +91,42 @@ dns_one_rm() {
subdomain="${_sub_domain}" subdomain="${_sub_domain}"
maindomain=${_domain} maindomain=${_domain}
useProxy=0 useProxy=0
if [ "${_sub_domain}" = "_acme-challenge" ]; then if [ "${_sub_domain}" = "_acme-challenge" ]; then
subdomain="proxy${_sub_domain}" subdomain="proxy${_sub_domain}"
useProxy=1 useProxy=1
fi fi
_debug subdomain "$subdomain" _debug subdomain "$subdomain"
_debug maindomain "$maindomain" _debug maindomain "$maindomain"
if [ $useProxy -eq 1 ]; then if [ $useProxy -eq 1 ]; then
if [ "$ONECOM_KeepCnameProxy" = "1" ]; then if [ "$ONECOM_KeepCnameProxy" = "1" ]; then
_info "$(__red "Keeping CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")" _info "$(__red "Keeping CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")"
else else
#Check if the CNAME exists #Check if the CNAME exists
_dns_one_getrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain" _dns_one_getrecord "CNAME" "$_sub_domain" "$subdomain.$maindomain"
if [ ! -z "$id" ]; then if [ -n "$id" ]; then
_info "$(__red "Removing CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")" _info "$(__red "Removing CNAME Proxy record: '$(__green "\"$_sub_domain\" => \"$subdomain.$maindomain\"")'")"
_dns_one_delrecord "$id" _dns_one_delrecord "$id"
fi
fi fi
fi
fi fi
#Check if the TXT exists #Check if the TXT exists
_dns_one_getrecord "TXT" "$subdomain" "$txtvalue" _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
if [ -z "$id" ]; then if [ -z "$id" ]; then
_err "Txt record not found." _err "Txt record not found."
return 1 return 1
fi fi
# delete entry # delete entry
if _dns_one_delrecord "$id"; then if _dns_one_delrecord "$id"; then
_info "$(__green Removed, OK)" _info "$(__green Removed, OK)"
return 0 return 0
else else
_err "Removing txt record error." _err "Removing txt record error."
return 1 return 1
fi fi
} }
@ -162,6 +164,7 @@ _dns_one_login() {
# get credentials # get credentials
ONECOM_KeepCnameProxy="${ONECOM_KeepCnameProxy:-$(_readaccountconf_mutable ONECOM_KeepCnameProxy)}" ONECOM_KeepCnameProxy="${ONECOM_KeepCnameProxy:-$(_readaccountconf_mutable ONECOM_KeepCnameProxy)}"
ONECOM_KeepCnameProxy="${ONECOM_KeepCnameProxy:-0}"
ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}" ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}" ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
@ -173,6 +176,7 @@ _dns_one_login() {
fi fi
#save the api key and email to the account conf file. #save the api key and email to the account conf file.
_saveaccountconf_mutable ONECOM_KeepCnameProxy "$ONECOM_KeepCnameProxy"
_saveaccountconf_mutable ONECOM_User "$ONECOM_User" _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
_saveaccountconf_mutable ONECOM_Password "$ONECOM_Password" _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
@ -207,17 +211,17 @@ _dns_one_getrecord() {
name="$2" name="$2"
value="$3" value="$3"
if [ -z "$type" ]; then if [ -z "$type" ]; then
type="TXT" type="TXT"
fi fi
if [ -z "$name" ]; then if [ -z "$name" ]; then
_err "Record name is empty." _err "Record name is empty."
return 1 return 1
fi fi
response="$(_get "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records")" response="$(_get "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records")"
response="$(echo "$response" | _normalizeJson)" response="$(echo "$response" | _normalizeJson)"
_debug response "$response" _debug response "$response"
if [ -z "${value}" ]; then if [ -z "${value}" ]; then
id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"[^\"]*\",\"priority\":0,\"ttl\":600}.*/\1/p") id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"[^\"]*\",\"priority\":0,\"ttl\":600}.*/\1/p")
response=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"[^\"]*\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"\([^\"]*\)\",\"priority\":0,\"ttl\":600}.*/\1/p") response=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"[^\"]*\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"\([^\"]*\)\",\"priority\":0,\"ttl\":600}.*/\1/p")
@ -225,7 +229,6 @@ _dns_one_getrecord() {
id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"${value}\",\"priority\":0,\"ttl\":600}.*/\1/p") id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"${value}\",\"priority\":0,\"ttl\":600}.*/\1/p")
fi fi
if [ -z "$id" ]; then if [ -z "$id" ]; then
_err "Record not found."
return 1 return 1
fi fi
return 0 return 0
@ -236,13 +239,13 @@ _dns_one_addrecord() {
name="$2" name="$2"
value="$3" value="$3"
if [ -z "$type" ]; then if [ -z "$type" ]; then
type="TXT" type="TXT"
fi fi
if [ -z "$name" ]; then if [ -z "$name" ]; then
_err "Record name is empty." _err "Record name is empty."
return 1 return 1
fi fi
postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"${type}\",\"prefix\":\"${name}\",\"content\":\"${value}\"}}" postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"${type}\",\"prefix\":\"${name}\",\"content\":\"${value}\"}}"
_debug postdata "$postdata" _debug postdata "$postdata"
response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records" "" "POST" "application/json")" response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records" "" "POST" "application/json")"
@ -261,16 +264,16 @@ _dns_one_addrecord() {
_dns_one_delrecord() { _dns_one_delrecord() {
id="$1" id="$1"
if [ -z "$id" ]; then if [ -z "$id" ]; then
return 1 return 1
fi fi
response="$(_post "" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records/$id" "" "DELETE" "application/json")" response="$(_post "" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records/$id" "" "DELETE" "application/json")"
response="$(echo "$response" | _normalizeJson)" response="$(echo "$response" | _normalizeJson)"
_debug response "$response" _debug response "$response"
if [ "$response" = '{"result":null,"metadata":null}' ]; then if [ "$response" = '{"result":null,"metadata":null}' ]; then
return 0 return 0
else else
return 1 return 1
fi fi
} }