From 2ad594c59129d2370d3d6131cfb535d68f33b573 Mon Sep 17 00:00:00 2001 From: Foat Akhmadeev Date: Wed, 19 Mar 2025 20:38:18 +0300 Subject: [PATCH] escaped spaces update --- .github/workflows/verify.yml | 9 +++++++-- action.yml | 2 +- scripts/run.sh | 32 +++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 56e9623..05fa15c 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -25,7 +25,11 @@ jobs: ARG MY_ENV_VAR2 ENV MY_ENV_VAR=$MY_ENV_VAR ENV MY_ENV_VAR2=$MY_ENV_VAR2 - CMD ["sh", "-c", "echo Result: $MY_ENV_VAR $MY_ENV_VAR2"]' > Dockerfile + ARG EXTRA_ENV_VAR + ARG EXTRA_ENV_VAR2 + ENV EXTRA_ENV_VAR=$EXTRA_ENV_VAR + ENV EXTRA_ENV_VAR2=$EXTRA_ENV_VAR2 + CMD ["sh", "-c", "echo Result: $MY_ENV_VAR $MY_ENV_VAR2 ~$EXTRA_ENV_VAR~ ~$EXTRA_ENV_VAR2~"]' > Dockerfile cat Dockerfile - name: Use Build and Push action uses: ./ @@ -36,6 +40,7 @@ jobs: tags: ghcr.io/frozen-tapestry/podman-action-verify:latest dockerfile: Dockerfile build_args: MY_ENV_VAR=Test MY_ENV_VAR2=Test2 + extra_args: --build-arg EXTRA_ENV_VAR="Extra\ env\ 1" --build-arg=EXTRA_ENV_VAR2=Extra\ env\ 2 security: | --security-opt=seccomp=unconfined --security-opt=apparmor=unconfined @@ -46,7 +51,7 @@ jobs: OUTPUT=$(docker run --rm ghcr.io/frozen-tapestry/podman-action-verify:latest) echo "Container Output: $OUTPUT" - if [ "$OUTPUT" != "Result: Test Test2" ]; then + if [ "$OUTPUT" != "Result: Test Test2 ~\"Extra env 1\"~ ~Extra env 2~" ]; then echo "Output does not match expected string" exit 1 fi \ No newline at end of file diff --git a/action.yml b/action.yml index d65ef38..d17a570 100644 --- a/action.yml +++ b/action.yml @@ -31,7 +31,7 @@ inputs: extra_args: description: | Extra args to be passed to podman. Space-separated. e.g. - -e=MY_ENV=Test -e=MY_ENV=Test2 + -e=MY_ENV=Test -e=MY_ENV=Test2 -e MY_ENV="Var\ with\ spaces" required: false dockerfile: description: Path to the Dockerfile. If set, build step is performed. diff --git a/scripts/run.sh b/scripts/run.sh index fb52223..195fcf5 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -32,16 +32,29 @@ if [[ -n "$REGISTRY" && -n "$USERNAME" && -n "$PASSWORD" ]]; then run_cmd "${build_cmd[@]}" fi +# Function that splits on unescaped spaces (but not on escaped ones) +# and outputs each processed token on a new line. generate_args() { local input_args="$1" local prefix="$2" - local output="" + local output=() + local placeholder="__ESCAPED_SPACE__" if [[ -n "$input_args" ]]; then - output="$(echo "$input_args" | tr -s ' ' '\n' | sed "s/[^ ]* */$prefix&/g")" + # Replace escaped spaces (\ ) with a unique placeholder. + local temp="${input_args//\\ /$placeholder}" + # Split on spaces (escaped ones are now hidden). + IFS=' ' read -r -a parts <<< "$temp" + for part in "${parts[@]}"; do + # Skip any empty parts. + [[ -z "$part" ]] && continue + # Restore escaped spaces. + part="${part//$placeholder/ }" + output+=("$prefix$part") + done fi - echo "$output" + printf "%s\n" "${output[@]}" } ### BUILD @@ -53,13 +66,13 @@ if [[ -n "$DOCKERFILE" ]]; then echo "Main labels: $CREATED $REVISION $SOURCE" TAGS=$(generate_args "$ACTION_TAGS" "-t=") - echo "Tags: $TAGS" + echo "Tags: ${TAGS[@]}" LABELS=$(generate_args "$ACTION_LABELS" "--label=") - echo "Labels: $LABELS" + echo "Labels: ${LABELS[@]}" BUILD_ARGS=$(generate_args "$ACTION_BUILD_ARGS" "--build-arg=") - echo "Build args: $BUILD_ARGS" + echo "Build args: ${BUILD_ARGS[@]}" EXTRA_ARGS=$(generate_args "$ACTION_EXTRA_ARGS" "") - echo "Extra args: $EXTRA_ARGS" + echo "Extra args: ${EXTRA_ARGS[@]}" build_cmd=(podman build --platform="linux/amd64" @@ -81,11 +94,12 @@ fi if [[ -n "$PUSH" && "$PUSH" == "true" ]]; then TAGS=$(generate_args "$ACTION_TAGS" "") - echo "Tags: $TAGS" + echo "Tags: ${TAGS[@]}" build_cmd=(podman push --storage-driver=overlay - --authfile="$REGISTRY_AUTH_FILE" $TAGS + --authfile="$REGISTRY_AUTH_FILE" + $TAGS ) run_cmd "${build_cmd[@]}" fi \ No newline at end of file