-
Notifications
You must be signed in to change notification settings - Fork 61
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
Adds progress bar for image push #421
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,32 +14,40 @@ import ( | |
"github.com/mattn/go-isatty" | ||
) | ||
|
||
// ProgressLogger Progress bar | ||
type ProgressLogger interface { | ||
Start(progress <-chan regv1.Update) | ||
Start(ctx context.Context, progress <-chan regv1.Update) | ||
End() | ||
} | ||
|
||
// NewProgressBar constructor to build a ProgressLogger responsible for printing out a progress bar using updates when | ||
// writing to a registry via ggcr | ||
func NewProgressBar(ui goui.UI, finalMessage, errorMessagePrefix string) ProgressLogger { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
if isatty.IsTerminal(os.Stdout.Fd()) { | ||
return &ProgressBarLogger{ctx: ctx, cancelFunc: cancel, ui: ui, finalMessage: finalMessage, errorMessagePrefix: errorMessagePrefix} | ||
return &ProgressBarLogger{ui: ui, finalMessage: finalMessage, errorMessagePrefix: errorMessagePrefix} | ||
} | ||
|
||
return &ProgressBarNoTTYLogger{ui: ui, ctx: ctx, cancelFunc: cancel, finalMessage: finalMessage} | ||
return &ProgressBarNoTTYLogger{ui: ui, finalMessage: finalMessage} | ||
} | ||
|
||
// NewNoopProgressBar constructs a Noop Progress bar that will not display anything | ||
func NewNoopProgressBar() ProgressLogger { | ||
return &ProgressBarNoTTYLogger{} | ||
} | ||
|
||
// ProgressBarLogger display progress bar on output | ||
type ProgressBarLogger struct { | ||
ctx context.Context | ||
cancelFunc context.CancelFunc | ||
bar *pb.ProgressBar | ||
ui goui.UI | ||
finalMessage string | ||
errorMessagePrefix string | ||
} | ||
|
||
func (l *ProgressBarLogger) Start(progressChan <-chan regv1.Update) { | ||
// Start the display of the Progress Bar | ||
func (l *ProgressBarLogger) Start(ctx context.Context, progressChan <-chan regv1.Update) { | ||
ctx, cancelFunc := context.WithCancel(ctx) | ||
l.cancelFunc = cancelFunc | ||
// Add a new empty line to separate the progress bar from prior output | ||
fmt.Println() | ||
l.bar = pb.New64(0) | ||
|
@@ -48,7 +56,7 @@ func (l *ProgressBarLogger) Start(progressChan <-chan regv1.Update) { | |
go func() { | ||
for { | ||
select { | ||
case <-l.ctx.Done(): | ||
case <-ctx.Done(): | ||
return | ||
case update := <-progressChan: | ||
if update.Error != nil { | ||
|
@@ -70,32 +78,43 @@ func (l *ProgressBarLogger) Start(progressChan <-chan regv1.Update) { | |
}() | ||
} | ||
|
||
// End stops the progress bar and writes the final message | ||
func (l *ProgressBarLogger) End() { | ||
l.cancelFunc() | ||
if l.cancelFunc != nil { | ||
l.cancelFunc() | ||
} | ||
l.bar.Finish() | ||
l.ui.BeginLinef("\n%s", l.finalMessage) | ||
} | ||
|
||
// ProgressBarNoTTYLogger does not display the progress bar | ||
type ProgressBarNoTTYLogger struct { | ||
ctx context.Context | ||
cancelFunc context.CancelFunc | ||
ui goui.UI | ||
finalMessage string | ||
} | ||
|
||
func (l *ProgressBarNoTTYLogger) Start(progressChan <-chan regv1.Update) { | ||
// Start consuming the progress channel but does not display anything | ||
func (l *ProgressBarNoTTYLogger) Start(ctx context.Context, progressChan <-chan regv1.Update) { | ||
ctx, cancelFunc := context.WithCancel(ctx) | ||
l.cancelFunc = cancelFunc | ||
go func() { | ||
for { | ||
select { | ||
case <-l.ctx.Done(): | ||
case <-ctx.Done(): | ||
return | ||
case <-progressChan: | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// End Write the final message | ||
func (l *ProgressBarNoTTYLogger) End() { | ||
l.cancelFunc() | ||
l.ui.BeginLinef(l.finalMessage) | ||
if l.cancelFunc != nil { | ||
l.cancelFunc() | ||
} | ||
if l.ui != nil && l.finalMessage != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to check on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of a |
||
l.ui.BeginLinef(l.finalMessage) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im assuming we are not using ProgressBarLogger concurrently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No we are not using