refactor(func): Remove unused functions
Remvoe the logging functions replaced by the new structured implementation.
This commit is contained in:
@@ -31,14 +31,29 @@ else
|
||||
width=80
|
||||
fi
|
||||
|
||||
getTimeStamp() {
|
||||
(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
badParam() {
|
||||
if [ "$1" = "null" ]; then
|
||||
printf "\n%sERROR: '%s' cannot have a NULL (empty) value.\n" "$err" "$2"
|
||||
printf "%sPlease use '--help' for assistance.%s\n\n" "$cyan" "$norm"
|
||||
exit 1
|
||||
elif [ "$1" = "dne" ]; then
|
||||
printf "\n%sERROR: '%s %s'\n" "$err" "$2" "$3"
|
||||
printf "file or directory does not exist or is empty.%s\n\n" "$norm"
|
||||
exit 1
|
||||
elif [ "$1" = "errMsg" ]; then
|
||||
printf "\n%sERROR: %s%s\n\n" "$err" "$2" "$norm"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
getHostname() {
|
||||
(hostname -s)
|
||||
}
|
||||
|
||||
getTimeStamp() {
|
||||
(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
}
|
||||
|
||||
lowercase() {
|
||||
echo "$1" | tr '[:upper:]' '[:lower:]'
|
||||
}
|
||||
@@ -47,6 +62,27 @@ uppercase() {
|
||||
echo "$1" | tr '[:lower:]' '[:upper:]'
|
||||
}
|
||||
|
||||
# 1: cloudflare error object, 2: operation identifier
|
||||
listCFErrors() {
|
||||
# extract error codes and messages in separate variables, replace newlines with underscores
|
||||
codes="$(printf "%s" "$1" | jq -r '.errors | .[] | .code' | tr '\n' '_')"
|
||||
messages="$(printf "%s" "$1" | jq -r '.errors | .[] | .message' | tr '\n' '_')"
|
||||
|
||||
# iterate codes and messages, assemble into a coherent log message
|
||||
while [ -n "$codes" ] && [ -n "$messages" ]; do
|
||||
# get the first code and message in respective sets
|
||||
code="${codes%%_*}"
|
||||
message="${messages%%_*}"
|
||||
|
||||
# update sets of codes and messages by removing the first item (above) in each set
|
||||
codes="${codes#*_}"
|
||||
messages="${messages#*_}"
|
||||
|
||||
# output to log
|
||||
writePlainTextLog "${message}" "err" "fail" "$2" "$code"
|
||||
done
|
||||
}
|
||||
|
||||
standardizeLogLevels() {
|
||||
LEVEL_TO_STANDARDIZE="$(lowercase "$1")"
|
||||
case "$LEVEL_TO_STANDARDIZE" in
|
||||
@@ -84,82 +120,6 @@ writePlainTextLog() {
|
||||
>>"$logFile"
|
||||
}
|
||||
|
||||
### functions
|
||||
badParam() {
|
||||
if [ "$1" = "null" ]; then
|
||||
printf "\n%sERROR: '%s' cannot have a NULL (empty) value.\n" "$err" "$2"
|
||||
printf "%sPlease use '--help' for assistance.%s\n\n" "$cyan" "$norm"
|
||||
exit 1
|
||||
elif [ "$1" = "dne" ]; then
|
||||
printf "\n%sERROR: '%s %s'\n" "$err" "$2" "$3"
|
||||
printf "file or directory does not exist or is empty.%s\n\n" "$norm"
|
||||
exit 1
|
||||
elif [ "$1" = "errMsg" ]; then
|
||||
printf "\n%sERROR: %s%s\n\n" "$err" "$2" "$norm"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
exitError() {
|
||||
case "$1" in
|
||||
3)
|
||||
errMsg="Unable to connect to Cloudflare servers. This is probably a temporary networking issue. Please try again later."
|
||||
;;
|
||||
10)
|
||||
errMsg="Unable to auto-detect IP address. Try again later or supply the IP address to be used."
|
||||
;;
|
||||
20)
|
||||
errMsg="Cloudflare authorized email address (cfEmail) is either null or undefined. Please check your Cloudflare credentials file."
|
||||
;;
|
||||
21)
|
||||
errMsg="Cloudflare authorized API key (cfKey) is either null or undefined. Please check your Cloudflare credentials file."
|
||||
;;
|
||||
22)
|
||||
errMsg="Cloudflare zone id (cfZoneId) is either null or undefined. Please check your Cloudflare credentials file."
|
||||
;;
|
||||
25)
|
||||
errMsg="Cloudflare API error. Please review any 'CF-ERR:' lines in this log for details."
|
||||
;;
|
||||
26)
|
||||
errMsg="${failedHostCount} host update(s) failed. Any 'CF-ERR:' lines noted in this log may help determine what went wrong."
|
||||
;;
|
||||
*)
|
||||
writeLog error "An unspecified error occurred. (code: 99)"
|
||||
printf "%s[%s] -- Cloudflare DDNS update-script: completed with error(s) --%s\n" "$err" "$(stamp)" "$norm" >>"$logFile"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
writeLog error "$errMsg" "$1"
|
||||
printf "%s[%s] -- Cloudflare DDNS update-script: completed with error(s) --%s\n" "$err" "$(stamp)" "$norm" >>"$logFile"
|
||||
exit "$1"
|
||||
}
|
||||
|
||||
exitOK() {
|
||||
printf "%s[%s] -- Cloudflare DDNS update-script: completed successfully --%s\n" "$ok" "$(stamp)" "$norm" >>"$logFile"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# 1: cloudflare error object, 2: operation identifier
|
||||
listCFErrors() {
|
||||
# extract error codes and messages in separate variables, replace newlines with underscores
|
||||
codes="$(printf "%s" "$1" | jq -r '.errors | .[] | .code' | tr '\n' '_')"
|
||||
messages="$(printf "%s" "$1" | jq -r '.errors | .[] | .message' | tr '\n' '_')"
|
||||
|
||||
# iterate codes and messages, assemble into a coherent log message
|
||||
while [ -n "$codes" ] && [ -n "$messages" ]; do
|
||||
# get the first code and message in respective sets
|
||||
code="${codes%%_*}"
|
||||
message="${messages%%_*}"
|
||||
|
||||
# update sets of codes and messages by removing the first item (above) in each set
|
||||
codes="${codes#*_}"
|
||||
messages="${messages#*_}"
|
||||
|
||||
# output to log
|
||||
writePlainTextLog "${message}" "err" "fail" "$2" "$code"
|
||||
done
|
||||
}
|
||||
|
||||
scriptExamples() {
|
||||
newline
|
||||
printf "Update Cloudflare DNS host A/AAAA records with current IP address.\n"
|
||||
@@ -246,10 +206,6 @@ scriptHelp() {
|
||||
exit 0
|
||||
}
|
||||
|
||||
stamp() {
|
||||
(date +%F" "%T)
|
||||
}
|
||||
|
||||
newline() {
|
||||
printf "\n"
|
||||
}
|
||||
@@ -266,47 +222,6 @@ textBlockSwitches() {
|
||||
printf "%s%s%s\n" "$cyan" "$1" "$norm"
|
||||
}
|
||||
|
||||
writeLog() {
|
||||
case "$1" in
|
||||
cf)
|
||||
printf "[%s] CF-ERR: %s (code: %s)\n" "$(stamp)" "$2" "$3" >>"$logFile"
|
||||
;;
|
||||
err)
|
||||
printf "%s[%s] ERR: %s%s\n" "$err" "$(stamp)" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
error)
|
||||
printf "%s[%s] ERROR: %s (code: %s)%s\n" "$err" "$(stamp)" "$2" "$3" "$norm" >>"$logFile"
|
||||
;;
|
||||
process)
|
||||
printf "%s[%s] %s... %s" "$cyan" "$(stamp)" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
process-done)
|
||||
printf "%s%s%s\n" "$cyan" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
process-error)
|
||||
printf "%sERROR%s\n" "$err" "$norm" >>"$logFile"
|
||||
;;
|
||||
process-warning)
|
||||
printf "%s%s%s\n" "$warn" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
stamped)
|
||||
printf "[%s] %s\n" "$(stamp)" "$2" >>"$logFile"
|
||||
;;
|
||||
success)
|
||||
printf "%s[%s] SUCCESS: %s%s\n" "$ok" "$(stamp)" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
warn)
|
||||
printf "%s[%s] WARN: %s%s\n" "$warn" "$(stamp)" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
warning)
|
||||
printf "%s[%s] WARNING: %s%s\n" "$warn" "$(stamp)" "$2" "$norm" >>"$logFile"
|
||||
;;
|
||||
*)
|
||||
printf "%s\n" "$2" >>"$logFile"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
### default variable values
|
||||
scriptPath="$(CDPATH='' \cd -- "$(dirname -- "$0")" && pwd -P)"
|
||||
scriptName="$(basename "$0")"
|
||||
|
||||
Reference in New Issue
Block a user