diff options
author | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2022-08-08 13:43:23 +0000 |
---|---|---|
committer | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2022-08-08 13:56:32 +0000 |
commit | 2460b3594356409f0eab5cdbea28e4c6700abb71 (patch) | |
tree | 19df0bb438923ca177e09739f77037830d9afe20 | |
parent | 27ec3d01e125f6f5e29c33c685fd79050d57ec98 (diff) |
jenkins-helpers.sh (convert_args_to_variables): Rework
... to make more structured.
Change-Id: I00f82f6b079e021f546408676848e6d557097d42
-rw-r--r-- | jenkins-helpers.sh | 121 |
1 files changed, 73 insertions, 48 deletions
diff --git a/jenkins-helpers.sh b/jenkins-helpers.sh index b04703e1..89ce07e5 100644 --- a/jenkins-helpers.sh +++ b/jenkins-helpers.sh @@ -913,6 +913,52 @@ manifest_out () cat >> "${__manifest_filename[0]}" } +convert_arg_var () +{ + declare -g "$1=$2" + cat <<EOF | manifest_out +declare -g "$1=$2" +EOF +} + +convert_arg_arr () +{ + if ! test_array $1; then + declare -ag $1 + cat <<EOF | manifest_out +declare -ga $1 +EOF + fi + eval "$1+=(\"$2\")" + cat <<EOF | manifest_out +$1+=("$2") +EOF +} + +convert_arg_assarr () +{ + local arr="${1%\[*\]}" + if ! test_array $arr; then + declare -Ag $arr + cat <<EOF | manifest_out +declare -gA $arr +EOF + fi + eval "$1=\"$2\"" + cat <<EOF | manifest_out +$1="$2" +EOF +} + +convert_arg_source () +{ + # shellcheck disable=SC1090 + source "$1" + echo "# Start of include $1" | manifest_out + cat "$1" | manifest_out + echo "# End of include $1" | manifest_out +} + # Process "--var value" and "++arr elem" arguments and define corresponding # variables and arrays. # "--var value" defines shell variable "$var" to "value". @@ -945,78 +991,57 @@ convert_args_to_variables () break ;; "--"*) - name="${1#--}" - declare -g "$name=$2" - cat <<EOF | manifest_out -declare -g "$name=$2" -EOF + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] + convert_arg_var "${1#--}" "$2" num=2 ;; "__"*) + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] name="${1#__}" declare -g "$name=$2" num=2 ;; "++"*) - name="${1#++}" - if ! test_array $name; then - declare -ag $name - cat <<EOF | manifest_out -declare -ga $name -EOF - fi - eval "$name+=(\"$2\")" - cat <<EOF | manifest_out -$name+=("$2") -EOF + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] + convert_arg_arr "${1#++}" "$2" num=2 ;; "=="*) - name="${1#==}" - arr="${name%\[*\]}" - if ! test_array $arr; then - declare -Ag $arr - cat <<EOF | manifest_out -declare -gA $arr -EOF - fi - if [ $# -lt 2 ]; then - echo "ERROR: Parameter value not provided for $1." - exit 1 - fi - eval "$name=\"$2\"" - cat <<EOF | manifest_out -$name="$2" -EOF + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] + convert_arg_assarr "${1#==}" "$2" num=2 ;; "@@") - # shellcheck disable=SC1090 - source "$2" - echo "# Start of include $2" | manifest_out - cat "$2" | manifest_out - echo "# End of include $2" | manifest_out + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] + convert_arg_source "$2" num=2 ;; "%%") + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 2 ] manifest_push "$2" cat <<EOF | manifest_out -# Start option processing -jenkins_scripts_rev=$(git -C "$(dirname "$0")" rev-parse HEAD) +declare -g "jenkins_scripts_rev=$(git -C "$(dirname "$0")" rev-parse HEAD)" +EOF + + cat <<EOF | manifest_out +# Recording parameters to manifest: $2 EOF num=2 ;; "^^") + assert_with_msg "ERROR: Parameter value not provided for $1." \ + [ $# -ge 4 ] if [ x"$2" = x"true" ]; then - # Check that we have a manifest to reproduce - if [ x"$3" != x"%%" ] || [ ! -f "$4" ]; then - echo "ERROR: '^^ true' must be followed by '%% <MANIFEST>'" - exit 1 - fi - - # Source the manifest for reproduction. - # shellcheck disable=SC1090 - source "$4" + assert_with_msg "ERROR: manifest does not exist: $4/manifest.sh" \ + [ -f $4/manifest.sh ] + + convert_arg_source "$4" # Skip processing all following arguments. num=0 |