From 81dfd16adc4d5b3b5dadc00d155be97d887bacff Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Fri, 9 Feb 2024 14:41:58 +0100 Subject: [PATCH 1/8] escape output --- scripts/action-entrypoint.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/action-entrypoint.sh b/scripts/action-entrypoint.sh index c6508be..134079d 100755 --- a/scripts/action-entrypoint.sh +++ b/scripts/action-entrypoint.sh @@ -1,7 +1,8 @@ -#!/bin/sh +#!/bin/bash /app/cac "$@" > /tmp/out # Merge the output into a single line so it can be used as github action output content=$(awk '{printf "%s\\n", $0}' /tmp/out) -echo "::set-output name=result::$content" \ No newline at end of file +escaped=$(printf '%q' "$content") +echo "::set-output name=result::$escaped" \ No newline at end of file From ecb89e43b872fa3a92e3511e13ca81a34dabe872 Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Fri, 9 Feb 2024 14:43:23 +0100 Subject: [PATCH 2/8] add bash --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 3a52bfd..6d05625 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,8 @@ FROM alpine:latest WORKDIR /app +RUN apk add bash + COPY --from=build /app/cac . COPY /scripts/action-entrypoint.sh /action-entrypoint.sh From d917024667f4541419fc3f9ce853a85526cc78ea Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 14:20:24 +0100 Subject: [PATCH 3/8] test different entrypoint --- scripts/action-entrypoint.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/action-entrypoint.sh b/scripts/action-entrypoint.sh index 134079d..2d03352 100755 --- a/scripts/action-entrypoint.sh +++ b/scripts/action-entrypoint.sh @@ -1,8 +1,7 @@ #!/bin/bash -/app/cac "$@" > /tmp/out +cac "$@" > /tmp/out # Merge the output into a single line so it can be used as github action output content=$(awk '{printf "%s\\n", $0}' /tmp/out) -escaped=$(printf '%q' "$content") -echo "::set-output name=result::$escaped" \ No newline at end of file +echo "::set-output name=result::${content@Q}" \ No newline at end of file From 11bb867b425830dd6d3ed690f2bc30e9958e0b56 Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 14:42:16 +0100 Subject: [PATCH 4/8] fix cac path --- scripts/action-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/action-entrypoint.sh b/scripts/action-entrypoint.sh index 2d03352..e39d38a 100755 --- a/scripts/action-entrypoint.sh +++ b/scripts/action-entrypoint.sh @@ -1,6 +1,6 @@ #!/bin/bash -cac "$@" > /tmp/out +/app/cac "$@" > /tmp/out # Merge the output into a single line so it can be used as github action output content=$(awk '{printf "%s\\n", $0}' /tmp/out) From 5e59e5b3f5b0fd375d07da7e3da8ad5e591cd2ef Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 15:21:29 +0100 Subject: [PATCH 5/8] output file --- .github/workflows/test.yaml | 5 ++++- scripts/action-entrypoint.sh | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4e24280..65a048c 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,4 +18,7 @@ jobs: id: cac with: args: --help - - run: echo -e "${{ steps.cac.outputs.result }}" \ No newline at end of file + - run: cat "${{ steps.cac.outputs.result }}" + name: print action output + - run: cat out + name: print output file \ No newline at end of file diff --git a/scripts/action-entrypoint.sh b/scripts/action-entrypoint.sh index e39d38a..9b35e65 100755 --- a/scripts/action-entrypoint.sh +++ b/scripts/action-entrypoint.sh @@ -1,7 +1,7 @@ #!/bin/bash -/app/cac "$@" > /tmp/out +/app/cac "$@" > out # Merge the output into a single line so it can be used as github action output -content=$(awk '{printf "%s\\n", $0}' /tmp/out) +content=$(awk '{printf "%s\\n", $0}' out) echo "::set-output name=result::${content@Q}" \ No newline at end of file From 722dd8bbb6ccd188b2ea5216cf294e0748651725 Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 21:35:48 +0100 Subject: [PATCH 6/8] instead of trying to write to output just write to selected file... --- .github/workflows/test.yaml | 4 +--- Dockerfile | 2 -- cmd/diff.go | 21 +++++++++++++++++++++ scripts/action-entrypoint.sh | 8 ++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 65a048c..c306d9e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,6 +19,4 @@ jobs: with: args: --help - run: cat "${{ steps.cac.outputs.result }}" - name: print action output - - run: cat out - name: print output file \ No newline at end of file + name: print action output \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6d05625..3a52bfd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,8 +12,6 @@ FROM alpine:latest WORKDIR /app -RUN apk add bash - COPY --from=build /app/cac . COPY /scripts/action-entrypoint.sh /action-entrypoint.sh diff --git a/cmd/diff.go b/cmd/diff.go index 1e68d16..4dc8042 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -29,6 +29,7 @@ var ( With("profile", rootConfig.Profile). With("source", diffConfig.Source). With("target", diffConfig.Target). + With("out", diffConfig.Out). Info("Comparing workspace configuration") if app, err = cac.InitApp(rootConfig.ConfigPath, rootConfig.Profile); err != nil { @@ -52,6 +53,24 @@ var ( return err } + if diffConfig.Out != "-" { + var ( + file *os.File + ) + + if file, err = os.Create(diffConfig.Out); err != nil { + return errors.Wrap(err, "failed to create output file") + } + + defer file.Close() + + if _, err = file.Write([]byte(result)); err != nil { + return errors.Wrap(err, "failed to write diff result to file") + } + + return nil + } + if _, err = os.Stdout.Write([]byte(result)); err != nil { return errors.Wrap(err, "failed to write diff result to stdout") } @@ -66,6 +85,7 @@ var ( WithSecrets bool Colors bool OnlyPresent bool + Out string } ) @@ -75,6 +95,7 @@ func init() { diffCmd.PersistentFlags().StringVar(&diffConfig.Workspace, "workspace", "", "Workspace to compare") diffCmd.PersistentFlags().BoolVar(&diffConfig.Colors, "colors", true, "Colorize output") diffCmd.PersistentFlags().BoolVar(&diffConfig.OnlyPresent, "only-present", false, "Compare only resources present at source") + diffCmd.PersistentFlags().StringVar(&pushConfig.Out, "out", "-", "Dry execution output. It can be a file or '-' for stdout") mustMarkRequired(diffCmd, "source", "target", "workspace") } diff --git a/scripts/action-entrypoint.sh b/scripts/action-entrypoint.sh index 9b35e65..c6508be 100755 --- a/scripts/action-entrypoint.sh +++ b/scripts/action-entrypoint.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh -/app/cac "$@" > out +/app/cac "$@" > /tmp/out # Merge the output into a single line so it can be used as github action output -content=$(awk '{printf "%s\\n", $0}' out) -echo "::set-output name=result::${content@Q}" \ No newline at end of file +content=$(awk '{printf "%s\\n", $0}' /tmp/out) +echo "::set-output name=result::$content" \ No newline at end of file From 432ac832d53bc0672aa285fdf045b62f892ba37a Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 21:48:58 +0100 Subject: [PATCH 7/8] fix write file --- cmd/diff.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 4dc8042..9efc75d 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -54,17 +54,7 @@ var ( } if diffConfig.Out != "-" { - var ( - file *os.File - ) - - if file, err = os.Create(diffConfig.Out); err != nil { - return errors.Wrap(err, "failed to create output file") - } - - defer file.Close() - - if _, err = file.Write([]byte(result)); err != nil { + if err = os.WriteFile(diffConfig.Out, []byte(result), 0644); err != nil { return errors.Wrap(err, "failed to write diff result to file") } @@ -95,7 +85,7 @@ func init() { diffCmd.PersistentFlags().StringVar(&diffConfig.Workspace, "workspace", "", "Workspace to compare") diffCmd.PersistentFlags().BoolVar(&diffConfig.Colors, "colors", true, "Colorize output") diffCmd.PersistentFlags().BoolVar(&diffConfig.OnlyPresent, "only-present", false, "Compare only resources present at source") - diffCmd.PersistentFlags().StringVar(&pushConfig.Out, "out", "-", "Dry execution output. It can be a file or '-' for stdout") + diffCmd.PersistentFlags().StringVar(&diffConfig.Out, "out", "-", "Diff output. It can be a file or '-' for stdout") mustMarkRequired(diffCmd, "source", "target", "workspace") } From 2cc6afec9ccc69e0db890a48ec5427abcdbfd66f Mon Sep 17 00:00:00 2001 From: Piotrek Janus Date: Tue, 13 Feb 2024 23:33:48 +0100 Subject: [PATCH 8/8] fix test action --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c306d9e..51e1bca 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,5 +18,5 @@ jobs: id: cac with: args: --help - - run: cat "${{ steps.cac.outputs.result }}" + - run: echo -e "${{ steps.cac.outputs.result }}" name: print action output \ No newline at end of file