sg: Show progress bar when waiting for commands to install (#30039)

This commit is contained in:
Thorsten Ball 2022-01-24 10:41:39 +01:00 committed by GitHub
parent e04eea71de
commit f2a5d3e2dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -94,7 +94,7 @@ func Commands(ctx context.Context, globalEnv map[string]string, verbose bool, cm
func waitForInstallation(cmdNames map[string]struct{}, installed chan string, failures chan failedRun, okayToStart chan struct{}) error {
stdout.Out.Write("")
stdout.Out.WriteLine(output.Linef(output.EmojiFingerPointRight, output.StyleBold, "Waiting for %d commands to finish installation...", len(cmdNames)))
stdout.Out.WriteLine(output.Linef(output.EmojiLightbulb, output.StyleBold, "Installing %d commands...", len(cmdNames)))
stdout.Out.Write("")
waitingMessages := []string{
@ -112,14 +112,28 @@ func waitForInstallation(cmdNames map[string]struct{}, installed chan string, fa
const tickInterval = 15 * time.Second
ticker := time.NewTicker(tickInterval)
done := 0.0
total := float64(len(cmdNames))
progress := stdout.Out.Progress([]output.ProgressBar{
{Label: fmt.Sprintf("Installing %d commands", len(cmdNames)), Max: total},
}, nil)
for {
select {
case cmdName := <-installed:
ticker.Reset(tickInterval)
delete(cmdNames, cmdName)
done += 1.0
progress.WriteLine(output.Linef("", output.StyleSuccess, "%s installed", cmdName))
progress.SetValue(0, done)
progress.SetLabelAndRecalc(0, fmt.Sprintf("%d/%d commands installed", int(done), int(total)))
// Everything installed!
if len(cmdNames) == 0 {
progress.Complete()
stdout.Out.Write("")
stdout.Out.WriteLine(output.Linef(output.EmojiSuccess, output.StyleSuccess, "Everything installed! Booting up the system!"))
stdout.Out.Write("")
@ -128,6 +142,8 @@ func waitForInstallation(cmdNames map[string]struct{}, installed chan string, fa
}
case failure := <-failures:
progress.Destroy()
// Something went wrong with an installation, no need to wait for the others
printCmdError(stdout.Out, failure.cmdName, failure.err)
return failure
@ -143,7 +159,13 @@ func waitForInstallation(cmdNames map[string]struct{}, installed chan string, fa
idx = len(waitingMessages) - 1
}
msg := waitingMessages[idx]
stdout.Out.WriteLine(output.Linef(output.EmojiFingerPointRight, output.StyleBold, msg, strings.Join(names, ", ")))
emoji := output.EmojiHourglass
if idx > 3 {
emoji = output.EmojiShrug
}
progress.WriteLine(output.Linef(emoji, output.StyleBold, msg, strings.Join(names, ", ")))
messageCount += 1
}
}
@ -284,7 +306,9 @@ func runWatch(
for {
// Build it
if cmd.Install != "" {
stdout.Out.WriteLine(output.Linef("", output.StylePending, "Installing %s...", cmd.Name))
if startedOnce {
stdout.Out.WriteLine(output.Linef("", output.StylePending, "Installing %s...", cmd.Name))
}
cmdOut, err := BashInRoot(ctx, cmd.Install, makeEnv(globalEnv, cmd.Env))
if err != nil {
@ -305,7 +329,9 @@ func runWatch(
default:
}
stdout.Out.WriteLine(output.Linef("", output.StyleSuccess, "%sSuccessfully installed %s%s", output.StyleBold, cmd.Name, output.StyleReset))
if startedOnce {
stdout.Out.WriteLine(output.Linef("", output.StyleSuccess, "%sSuccessfully installed %s%s", output.StyleBold, cmd.Name, output.StyleReset))
}
if cmd.CheckBinary != "" {
newHash, err := md5HashFile(filepath.Join(root, cmd.CheckBinary))

View File

@ -10,6 +10,8 @@ const (
EmojiAsterisk = "✱"
EmojiWarningSign = "⚠️"
EmojiFingerPointRight = "👉"
EmojiHourglass = "⌛"
EmojiShrug = "🤷"
EmojiOk = "👌"
EmojiQuestionMark = "❔"
)