Skip to content

Commit 7d8cf08

Browse files
authored
refactor!: update progress writer interface (#85)
## 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
1 parent ae18051 commit 7d8cf08

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

helpers/progress.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,29 @@ func (DiscardProgressWriter) Write(p []byte) (int, error) {
1010
return len(p), nil
1111
}
1212

13-
// UpdateTitle doesn't do anything but satisfy implementation
14-
func (DiscardProgressWriter) UpdateTitle(_ string) {}
13+
// Close doesn't do anything but satisfy implementation
14+
func (DiscardProgressWriter) Close() error {
15+
return nil
16+
}
17+
18+
// Updatef doesn't do anything but satisfy implementation
19+
func (DiscardProgressWriter) Updatef(_ string, _ ...any) {}
20+
21+
// Successf doesn't do anything but satisfy implementation
22+
func (DiscardProgressWriter) Successf(_ string, _ ...any) {}
23+
24+
// Failf doesn't do anything but satisfy implementation
25+
func (DiscardProgressWriter) Failf(_ string, _ ...any) {}
1526

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

20-
// ProgressWriter wraps io.Writer, but also includes an updateTitle function to give the user
31+
// ProgressWriter wraps io.Writer, but also includes functions to give the user
2132
// additional context on what's going on. Useful in OCI for tracking layers
2233
type ProgressWriter interface {
23-
UpdateTitle(string)
24-
io.Writer
34+
Updatef(string, ...any)
35+
Successf(string, ...any)
36+
Failf(string, ...any)
37+
io.WriteCloser
2538
}

oci/copier.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Copy(ctx context.Context, src *OrasRemote, dst *OrasRemote,
5555
src.log.Debug("Layer already exists in destination, skipping")
5656
b := make([]byte, layer.Size)
5757
_, _ = progressBar.Write(b)
58-
progressBar.UpdateTitle(fmt.Sprintf("[%d/%d] layers copied", idx+1, len(layers)))
58+
progressBar.Updatef("[%d/%d] layers copied", idx+1, len(layers))
5959
sem.Release(1)
6060
continue
6161
}
@@ -102,7 +102,7 @@ func Copy(ctx context.Context, src *OrasRemote, dst *OrasRemote,
102102
return err
103103
}
104104
sem.Release(1)
105-
progressBar.UpdateTitle(fmt.Sprintf("[%d/%d] layers copied", idx+1, len(layers)))
105+
progressBar.Updatef("[%d/%d] layers copied", idx+1, len(layers))
106106
}
107107

108108
duration := time.Since(start)

oci/oci_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,20 @@ func (tpw *TestProgressWriter) Write(b []byte) (int, error) {
194194
return len(b), nil
195195
}
196196

197-
func (TestProgressWriter) UpdateTitle(s string) {
198-
fmt.Printf("this is the title %s", s)
197+
func (TestProgressWriter) Close() error {
198+
return nil
199+
}
200+
201+
func (TestProgressWriter) Updatef(s string, a ...any) {
202+
fmt.Printf(s, a...)
203+
}
204+
205+
func (TestProgressWriter) Successf(s string, a ...any) {
206+
fmt.Printf(s, a...)
207+
}
208+
209+
func (TestProgressWriter) Failf(s string, a ...any) {
210+
fmt.Printf(s, a...)
199211
}
200212

201213
type TestProgressWriter struct {

0 commit comments

Comments
 (0)