Skip to content

Commit

Permalink
Added more useful functions
Browse files Browse the repository at this point in the history
  • Loading branch information
drivera-armedia committed Nov 12, 2024
1 parent d7860ed commit fd1c7e4
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,19 @@ is_valid_hostname()
return 0
}

#
# usage: is_valid_hostname_part HOSTNAME_PART
#
# Check that the given hostname part is valid as per RFC-1123,
# and return 0 if it is, or a non-0 status if it is not.
#
is_valid_hostname_part()
{
local __PART="${1}"
[[ "${__PART,,}" =~ ^([a-z0-9][-a-z0-9]*)?[a-z0-9]$ ]] || return 1
return 0
}

#
# usage: is_valid_port PORT
#
Expand Down Expand Up @@ -1351,3 +1364,62 @@ poll_url_insecure()
{
__poll_url true "${@}"
}

#
# usage: auth_params USER:PASSWORD
#
# Generate a CURL authentication configuration that can
# be used securely via --config
#
auth_params()
{
local USER=""
local PASS=""

case ${#} in
0 ) ;;
1 ) USER="${1}"
[[ "${USER}" =~ ^([^:]*)(:(.*))?$ ]] || true
USER="${BASH_REMATCH[1]}"
PASS="${BASH_REMATCH[3]}"
;;
* ) USER="${1}"
PASS="${2}"
;;
esac

cat <<-EOF
user "${USER}:${PASS}"
EOF
}

#
# usage: execute_unless VAR_NAME COMMAND [ARGS ...]
#
# Execute the given command conditionally, in case the given variable has
# a boolean-false value (or is not set).
#
execute_unless()
{
[ ${#} -ge 2 ] || fail "execute_unless requires the name of the variable AND the command to execute"

local __VAR="${1}"
shift

[[ "${__VAR}" =~ ^[a-zA-Z0-9_]+$ ]] || fail "Invalid variable name [${__VAR}]"

#
# Show the command about to be executed
#
running "${@@Q}"
if as_boolean "${!__VAR}" ; then
warn "${__VAR} is set to true, Skipping the command execution"
return 0
fi

#
# Proceed with the execution
#
( exec "${@}" )
return ${?}
}

0 comments on commit fd1c7e4

Please sign in to comment.