Skip to content

Commit

Permalink
Add silent option to patch
Browse files Browse the repository at this point in the history
Signed-off-by: Shishir Kushwaha <[email protected]>
  • Loading branch information
shishir-11 committed Jul 11, 2024
1 parent 1fcc6e0 commit 9e4308b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
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 @@ type patchArgs struct {
ignoreError bool
format string
output string
silent bool
bkOpts buildkit.Opts
}

Expand All @@ -50,6 +51,7 @@ func NewPatchCmd() *cobra.Command {
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 @@ 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")

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

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

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 @@ 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

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

0 comments on commit 9e4308b

Please sign in to comment.