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

new(cmd,pkg/driver): properly use a spinner for long operations. #366

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Dec 5, 2023

What type of PR is this?

/kind feature

Any specific area of the project related to this PR?

/area library
/area cli

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

@@ -317,7 +314,7 @@ func (o *artifactInstallOptions) RunArtifactInstall(ctx context.Context, args []
logger.Info("Extracting and installing artifact", logger.Args("type", result.Type, "file", result.Filename))

if !o.Printer.DisableStyling {
sp, _ = o.Printer.Spinner.Start("Extracting and installing")
o.Printer.Spinner, _ = o.Printer.Spinner.Start("Extracting and installing")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to save the spinner to our o.Printer one to let main properly stop the right spinner when a context error happens.

// Print much more readable output as-is
o.Printer.DefaultText.Print(buf.String())
}
buf.Reset()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not needed here since it is not reused.
I think it is safer to let it there though.

@@ -40,6 +40,9 @@ func main() {
if opt.Printer != nil && opt.Printer.ProgressBar != nil && opt.Printer.ProgressBar.IsActive {
_, _ = opt.Printer.ProgressBar.Stop()
}
if opt.Printer != nil && opt.Printer.Spinner != nil && opt.Printer.Spinner.IsActive {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there's a spinner in use, stop it upon leaving.

@FedeDP
Copy link
Contributor Author

FedeDP commented Dec 5, 2023

/milestone v0.7.0

@poiana poiana added this to the v0.7.0 milestone Dec 5, 2023
if o.Printer.Spinner != nil {
_ = o.Printer.Spinner.Stop()
}
if o.Printer.Logger.Formatter == pterm.LogFormatterJSON {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is ugly: we should probably find a better way to deal with unformatted logs while in JSON mode.

if !o.Printer.DisableStyling {
o.Printer.Spinner, _ = o.Printer.Spinner.Start("Cleaning up existing drivers")
}
err = driver.Type.Cleanup(o.Printer.WithWriter(&buf), driver.Name)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Idea is pretty simple: we start the spinner and to avoid called commands to print anythng to screen, we pass to them a new printer variable that prints onto a buffer.
After we stop the spinner, we are free to print all messages from the buffer.

@poiana poiana added the lgtm label Dec 6, 2023
@poiana
Copy link
Contributor

poiana commented Dec 6, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, leogr

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit d5893e1 into main Dec 6, 2023
14 checks passed
@poiana poiana deleted the new/driver_use_spinner branch December 6, 2023 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants