Skip to content

Commit

Permalink
refactor!: update progress writer interface (#85)
Browse files Browse the repository at this point in the history
## Description

This updates the `ProgressWriter` interface to be more flexible for more
usecases (i.e. both the Spinner and ProgressBar in Maru runner).

## Related Issue

Fixes #N/A
  • Loading branch information
Racer159 authored May 15, 2024
1 parent ae18051 commit 7d8cf08
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
23 changes: 18 additions & 5 deletions helpers/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ func (DiscardProgressWriter) Write(p []byte) (int, error) {
return len(p), nil
}

// UpdateTitle doesn't do anything but satisfy implementation
func (DiscardProgressWriter) UpdateTitle(_ string) {}
// Close doesn't do anything but satisfy implementation
func (DiscardProgressWriter) Close() error {
return nil
}

// Updatef doesn't do anything but satisfy implementation
func (DiscardProgressWriter) Updatef(_ string, _ ...any) {}

// Successf doesn't do anything but satisfy implementation
func (DiscardProgressWriter) Successf(_ string, _ ...any) {}

// Failf doesn't do anything but satisfy implementation
func (DiscardProgressWriter) Failf(_ string, _ ...any) {}

// DiscardProgressWriter is a ProgressWriter in which all calls succeed without doing anything
// Use this or nil or if you don't care about writing progress
type DiscardProgressWriter struct{}

// ProgressWriter wraps io.Writer, but also includes an updateTitle function to give the user
// ProgressWriter wraps io.Writer, but also includes functions to give the user
// additional context on what's going on. Useful in OCI for tracking layers
type ProgressWriter interface {
UpdateTitle(string)
io.Writer
Updatef(string, ...any)
Successf(string, ...any)
Failf(string, ...any)
io.WriteCloser
}
4 changes: 2 additions & 2 deletions oci/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Copy(ctx context.Context, src *OrasRemote, dst *OrasRemote,
src.log.Debug("Layer already exists in destination, skipping")
b := make([]byte, layer.Size)
_, _ = progressBar.Write(b)
progressBar.UpdateTitle(fmt.Sprintf("[%d/%d] layers copied", idx+1, len(layers)))
progressBar.Updatef("[%d/%d] layers copied", idx+1, len(layers))
sem.Release(1)
continue
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func Copy(ctx context.Context, src *OrasRemote, dst *OrasRemote,
return err
}
sem.Release(1)
progressBar.UpdateTitle(fmt.Sprintf("[%d/%d] layers copied", idx+1, len(layers)))
progressBar.Updatef("[%d/%d] layers copied", idx+1, len(layers))
}

duration := time.Since(start)
Expand Down
16 changes: 14 additions & 2 deletions oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,20 @@ func (tpw *TestProgressWriter) Write(b []byte) (int, error) {
return len(b), nil
}

func (TestProgressWriter) UpdateTitle(s string) {
fmt.Printf("this is the title %s", s)
func (TestProgressWriter) Close() error {
return nil
}

func (TestProgressWriter) Updatef(s string, a ...any) {
fmt.Printf(s, a...)
}

func (TestProgressWriter) Successf(s string, a ...any) {
fmt.Printf(s, a...)
}

func (TestProgressWriter) Failf(s string, a ...any) {
fmt.Printf(s, a...)
}

type TestProgressWriter struct {
Expand Down

0 comments on commit 7d8cf08

Please sign in to comment.