From 77e789cff9d39055c3316b782501b7bb416a8d10 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Mon, 21 Oct 2024 16:37:01 +0200 Subject: [PATCH] Print a message if a command is taking more than 10 seconds Signed-off-by: Itxaka --- pkg/console/console.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/console/console.go b/pkg/console/console.go index fe2e90b0..64921544 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -17,6 +17,7 @@ package console import ( "fmt" "os/exec" + "time" "github.com/hashicorp/go-multierror" "github.com/mudler/yip/pkg/logger" @@ -53,6 +54,7 @@ func (s StandardConsole) Run(cmd string, opts ...func(cmd *exec.Cmd)) (string, e for _, o := range opts { o(c) } + displayProgress(s.logger, 10*time.Second, fmt.Sprintf("Still running command '%s'", cmd)) out, err := c.CombinedOutput() if err != nil { return string(out), fmt.Errorf("failed to run %s: %v", cmd, err) @@ -61,6 +63,25 @@ func (s StandardConsole) Run(cmd string, opts ...func(cmd *exec.Cmd)) (string, e return string(out), err } +func displayProgress(log logger.Interface, tick time.Duration, message string) chan bool { + ticker := time.NewTicker(tick) + done := make(chan bool) + + go func() { + for { + select { + case <-done: + ticker.Stop() + return + case <-ticker.C: + log.Info(message) + } + } + }() + + return done +} + func (s StandardConsole) Start(cmd *exec.Cmd, opts ...func(cmd *exec.Cmd)) error { s.logger.Debugf("running command `%s`", cmd) for _, o := range opts {