Last active
January 12, 2021 07:41
-
-
Save undergroundwires/b384e499fd8ba850df173b685b30e32c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# We parse parameter to ensure middleman.sh can be called using: | |
# 1. Double quoted parameter value chains | |
# E.g. "--parameter value" "--parameter2 value" | |
# Designed for GitHub actions as action.yml args is sent this way. | |
# Parameter value chains are parsed to send in unquoted format | |
# 2. Parameter value chain | |
# This is the normal usage, parameters are sent through as they are | |
# E.g. --parameter value --parameter2 value | |
main() { | |
parameters=() | |
for part in "$@" | |
do | |
if is_parameter_name_and_value_in_same_arg "$part"; then # Called by GitHub actions | |
name=${part%% *} # Before first whitespace | |
value=${part#* } # After first whitespace | |
parameters+=("$name" "$value") | |
else # Not by GitHub actions, send the parameters as they are | |
parameters+=("$part") | |
fi | |
done | |
echo "[middleman.sh] Parameters:" "${parameters[@]}" | |
local -r current_directory=$(dirname "$0") | |
bash "$current_directory"/script.sh "${parameters[@]}" | |
} | |
is_parameter_name_and_value_in_same_arg() { | |
local -r value="$1" | |
if starts_with "$value" '--' && \ | |
includes "$value" ' '; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
starts_with() { | |
local -r value="$1" | |
local -r prefix="$2" | |
[[ $value = $prefix* ]] | |
} | |
includes() { | |
local -r value="$1" | |
local -r pattern="$2" | |
[[ $value =~ $pattern ]] | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment