From 9e4308bc60012b4c46ecc08e0046d108c11c62a2 Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Thu, 11 Jul 2024 10:13:04 +0530 Subject: [PATCH 1/8] Add silent option to patch Signed-off-by: Shishir Kushwaha --- pkg/patch/cmd.go | 3 +++ pkg/patch/patch.go | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/pkg/patch/cmd.go b/pkg/patch/cmd.go index f980f978b..5ea3161c6 100644 --- a/pkg/patch/cmd.go +++ b/pkg/patch/cmd.go @@ -25,6 +25,7 @@ type patchArgs struct { ignoreError bool format string output string + silent bool bkOpts buildkit.Opts } @@ -50,6 +51,7 @@ func NewPatchCmd() *cobra.Command { ua.scanner, ua.format, ua.output, + ua.silent, ua.ignoreError, bkopts) }, @@ -66,6 +68,7 @@ func NewPatchCmd() *cobra.Command { flags.DurationVar(&ua.timeout, "timeout", 5*time.Minute, "Timeout for the operation, defaults to '5m'") flags.StringVarP(&ua.scanner, "scanner", "s", "trivy", "Scanner used to generate the report, defaults to 'trivy'") flags.BoolVar(&ua.ignoreError, "ignore-errors", false, "Ignore errors and continue patching") + flags.BoolVar(&ua.silent, "silent", false, "silences output while processing") flags.StringVarP(&ua.format, "format", "f", "openvex", "Output format, defaults to 'openvex'") flags.StringVarP(&ua.output, "output", "o", "", "Output file path") diff --git a/pkg/patch/patch.go b/pkg/patch/patch.go index 2e9d07214..7587565c6 100644 --- a/pkg/patch/patch.go +++ b/pkg/patch/patch.go @@ -43,13 +43,13 @@ const ( ) // Patch command applies package updates to an OCI image given a vulnerability report. -func Patch(ctx context.Context, timeout time.Duration, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error { +func Patch(ctx context.Context, timeout time.Duration, image, reportFile, patchedTag, workingFolder, scanner, format, output string, silent, ignoreError bool, bkOpts buildkit.Opts) error { timeoutCtx, cancel := context.WithTimeout(ctx, timeout) defer cancel() ch := make(chan error) go func() { - ch <- patchWithContext(timeoutCtx, ch, image, reportFile, patchedTag, workingFolder, scanner, format, output, ignoreError, bkOpts) + ch <- patchWithContext(timeoutCtx, ch, image, reportFile, patchedTag, workingFolder, scanner, format, output, silent, ignoreError, bkOpts) }() select { @@ -74,7 +74,7 @@ func removeIfNotDebug(workingFolder string) { } } -func patchWithContext(ctx context.Context, ch chan error, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error { +func patchWithContext(ctx context.Context, ch chan error, image, reportFile, patchedTag, workingFolder, scanner, format, output string, silent, ignoreError bool, bkOpts buildkit.Opts) error { imageName, err := reference.ParseNormalizedNamed(image) if err != nil { return err @@ -275,21 +275,35 @@ func patchWithContext(ctx context.Context, ch chan error, image, reportFile, pat return err }) + if silent { + eg.Go(func() error { + for { + select { + case <-ctx.Done(): + return context.Cause(ctx) + case _, ok := <-buildChannel: + if !ok { + return nil + } + } + } + }) + } else { + eg.Go(func() error { + // not using shared context to not disrupt display but let us finish reporting errors + mode := progressui.AutoMode + if log.GetLevel() >= log.DebugLevel { + mode = progressui.PlainMode + } + display, err := progressui.NewDisplay(os.Stderr, mode) + if err != nil { + return err + } - eg.Go(func() error { - // not using shared context to not disrupt display but let us finish reporting errors - mode := progressui.AutoMode - if log.GetLevel() >= log.DebugLevel { - mode = progressui.PlainMode - } - display, err := progressui.NewDisplay(os.Stderr, mode) - if err != nil { + _, err = display.UpdateFrom(ctx, buildChannel) return err - } - - _, err = display.UpdateFrom(ctx, buildChannel) - return err - }) + }) + } eg.Go(func() error { if err := dockerLoad(ctx, pipeR); err != nil { From deb5e30f9730e29dad48bdb448c2371642abe6e5 Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Fri, 19 Jul 2024 00:51:44 +0530 Subject: [PATCH 2/8] specifies output type silenced with flag Signed-off-by: Shishir Kushwaha --- pkg/patch/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/patch/cmd.go b/pkg/patch/cmd.go index 5ea3161c6..442acc56f 100644 --- a/pkg/patch/cmd.go +++ b/pkg/patch/cmd.go @@ -68,7 +68,7 @@ func NewPatchCmd() *cobra.Command { flags.DurationVar(&ua.timeout, "timeout", 5*time.Minute, "Timeout for the operation, defaults to '5m'") flags.StringVarP(&ua.scanner, "scanner", "s", "trivy", "Scanner used to generate the report, defaults to 'trivy'") flags.BoolVar(&ua.ignoreError, "ignore-errors", false, "Ignore errors and continue patching") - flags.BoolVar(&ua.silent, "silent", false, "silences output while processing") + flags.BoolVar(&ua.silent, "silent", false, "silences the buildkit output while processing") flags.StringVarP(&ua.format, "format", "f", "openvex", "Output format, defaults to 'openvex'") flags.StringVarP(&ua.output, "output", "o", "", "Output file path") From a1dfee6ba67edd45ca28634c33d013684506ae4c Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Sat, 20 Jul 2024 00:04:02 +0530 Subject: [PATCH 3/8] Add test for silent flag Signed-off-by: Shishir Kushwaha --- pkg/patch/cmd_test.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/patch/cmd_test.go b/pkg/patch/cmd_test.go index eaa21c95a..9510487c7 100644 --- a/pkg/patch/cmd_test.go +++ b/pkg/patch/cmd_test.go @@ -4,14 +4,22 @@ import "testing" func TestNewPatchCmd(t *testing.T) { tests := []struct { - name string - args []string - expected string + name string + args []string + expected bool + errString string }{ { - name: "Missing image flag", - args: []string{"-r", "trivy.json", "-t", "3.7-alpine-patched"}, - expected: "required flag(s) \"image\" not set", + name: "Missing image flag", + args: []string{"-r", "trivy.json", "-t", "3.7-alpine-patched"}, + expected: true, + errString: "required flag(s) \"image\" not set", + }, + { + name: "Silent flag used", + args: []string{"-t", "3.7-alpine-patched", "-i", "alpine:latest", "--silent"}, + expected: false, + errString: "", }, } @@ -24,7 +32,10 @@ func TestNewPatchCmd(t *testing.T) { // Run the command and capture the output err := cmd.Execute() - if err == nil || err.Error() != tt.expected { + if err != nil && !tt.expected { + t.Errorf("Unexpected error: %v", err) + } + if err != nil && err.Error() != tt.errString { t.Errorf("Unexpected error: %v, expected: %v", err, tt.expected) } }) From fdb32cfaf1f9f9031341bd6c1a6d66b981005f9b Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Sat, 20 Jul 2024 01:18:08 +0530 Subject: [PATCH 4/8] Change image for test Signed-off-by: Shishir Kushwaha --- pkg/patch/cmd_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/patch/cmd_test.go b/pkg/patch/cmd_test.go index 9510487c7..0043ab567 100644 --- a/pkg/patch/cmd_test.go +++ b/pkg/patch/cmd_test.go @@ -17,7 +17,7 @@ func TestNewPatchCmd(t *testing.T) { }, { name: "Silent flag used", - args: []string{"-t", "3.7-alpine-patched", "-i", "alpine:latest", "--silent"}, + args: []string{"-t", "3.7-alpine-patched", "-i", "alpine:3.14", "--silent"}, expected: false, errString: "", }, @@ -34,8 +34,7 @@ func TestNewPatchCmd(t *testing.T) { err := cmd.Execute() if err != nil && !tt.expected { t.Errorf("Unexpected error: %v", err) - } - if err != nil && err.Error() != tt.errString { + } else if err != nil && err.Error() != tt.errString { t.Errorf("Unexpected error: %v, expected: %v", err, tt.expected) } }) From 7b4b3ceda93d180155eb364be316e4c8d3a26c69 Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Sat, 20 Jul 2024 01:28:53 +0530 Subject: [PATCH 5/8] Change to test file Signed-off-by: Shishir Kushwaha --- pkg/patch/cmd_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/patch/cmd_test.go b/pkg/patch/cmd_test.go index 0043ab567..b3a6a7b5e 100644 --- a/pkg/patch/cmd_test.go +++ b/pkg/patch/cmd_test.go @@ -32,10 +32,16 @@ func TestNewPatchCmd(t *testing.T) { // Run the command and capture the output err := cmd.Execute() - if err != nil && !tt.expected { - t.Errorf("Unexpected error: %v", err) - } else if err != nil && err.Error() != tt.errString { - t.Errorf("Unexpected error: %v, expected: %v", err, tt.expected) + if !tt.expected { + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + } else { + if err == nil { + t.Errorf("Expected error: %v, got %v", tt.expected, err) + } else if err != nil && err.Error() != tt.errString { + t.Errorf("Unexpected error: %v, expected: %v", err, tt.expected) + } } }) } From 05074d7a550174e54bc2c1be0fe384db9b6a49a7 Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Sat, 20 Jul 2024 03:35:02 +0530 Subject: [PATCH 6/8] workflow change to debug Signed-off-by: Shishir Kushwaha --- .github/workflows/build.yml | 2 +- pkg/patch/cmd_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 240dcdca1..38b663f25 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: - name: Unit test shell: bash env: - CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic" + CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic --debug" run: make test - name: Upload coverage to Codecov uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 diff --git a/pkg/patch/cmd_test.go b/pkg/patch/cmd_test.go index b3a6a7b5e..8a8b23b89 100644 --- a/pkg/patch/cmd_test.go +++ b/pkg/patch/cmd_test.go @@ -17,7 +17,7 @@ func TestNewPatchCmd(t *testing.T) { }, { name: "Silent flag used", - args: []string{"-t", "3.7-alpine-patched", "-i", "alpine:3.14", "--silent"}, + args: []string{"-i", "alpine:3.14"}, expected: false, errString: "", }, From e62e5acd444dee91ad72f0858e398b61c706aa83 Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Mon, 22 Jul 2024 13:54:38 +0530 Subject: [PATCH 7/8] Debug run Signed-off-by: Shishir Kushwaha --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 38b663f25..a17553fb5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: - name: Unit test shell: bash env: - CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic --debug" + CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic -debug" run: make test - name: Upload coverage to Codecov uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 From 29a4f79186723328665eb6aa860327865f6fc3ee Mon Sep 17 00:00:00 2001 From: Shishir Kushwaha Date: Mon, 22 Jul 2024 14:03:03 +0530 Subject: [PATCH 8/8] Add debug flag Signed-off-by: Shishir Kushwaha --- .github/workflows/build.yml | 2 +- pkg/patch/cmd_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a17553fb5..240dcdca1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: - name: Unit test shell: bash env: - CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic -debug" + CODECOV_OPTS: "-coverprofile=coverage.txt -covermode=atomic" run: make test - name: Upload coverage to Codecov uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 diff --git a/pkg/patch/cmd_test.go b/pkg/patch/cmd_test.go index 8a8b23b89..b587861ef 100644 --- a/pkg/patch/cmd_test.go +++ b/pkg/patch/cmd_test.go @@ -17,7 +17,7 @@ func TestNewPatchCmd(t *testing.T) { }, { name: "Silent flag used", - args: []string{"-i", "alpine:3.14"}, + args: []string{"-i", "alpine:3.14", "--debug"}, expected: false, errString: "", },