Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to silent the patch output by using --silent flag #699

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions pkg/patch/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ignoreError bool
format string
output string
silent bool
bkOpts buildkit.Opts
}

Expand All @@ -50,6 +51,7 @@
ua.scanner,
ua.format,
ua.output,
ua.silent,

Check warning on line 54 in pkg/patch/cmd.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/cmd.go#L54

Added line #L54 was not covered by tests
ua.ignoreError,
bkopts)
},
Expand All @@ -66,6 +68,7 @@
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we specify that it only silences buildkit output?

flags.StringVarP(&ua.format, "format", "f", "openvex", "Output format, defaults to 'openvex'")
flags.StringVarP(&ua.output, "output", "o", "", "Output file path")

Expand Down
46 changes: 30 additions & 16 deletions pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
)

// 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 {

Check warning on line 46 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L46

Added line #L46 was not covered by tests
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)

Check warning on line 52 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L52

Added line #L52 was not covered by tests
}()

select {
Expand All @@ -74,7 +74,7 @@
}
}

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 {

Check warning on line 77 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L77

Added line #L77 was not covered by tests
imageName, err := reference.ParseNormalizedNamed(image)
if err != nil {
return err
Expand Down Expand Up @@ -275,21 +275,35 @@

return err
})
if silent {
eg.Go(func() error {
for {
select {
case <-ctx.Done():
return context.Cause(ctx)
case _, ok := <-buildChannel:
if !ok {
return nil

Check warning on line 286 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L278-L286

Added lines #L278 - L286 were not covered by tests
}
}
}
})
} else {
eg.Go(func() error {

Check warning on line 292 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L291-L292

Added lines #L291 - L292 were not covered by tests
// 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

Check warning on line 296 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L294-L296

Added lines #L294 - L296 were not covered by tests
}
display, err := progressui.NewDisplay(os.Stderr, mode)
if err != nil {
return err

Check warning on line 300 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L298-L300

Added lines #L298 - L300 were not covered by tests
}

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)

Check warning on line 303 in pkg/patch/patch.go

View check run for this annotation

Codecov / codecov/patch

pkg/patch/patch.go#L303

Added line #L303 was not covered by tests
return err
}

_, err = display.UpdateFrom(ctx, buildChannel)
return err
})
})
}

eg.Go(func() error {
if err := dockerLoad(ctx, pipeR); err != nil {
Expand Down
Loading