Skip to content

Commit

Permalink
Print a message if a command is taking more than 10 seconds
Browse files Browse the repository at this point in the history
Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Oct 21, 2024
1 parent 5338e89 commit 77e789c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package console
import (
"fmt"
"os/exec"
"time"

"github.com/hashicorp/go-multierror"
"github.com/mudler/yip/pkg/logger"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 77e789c

Please sign in to comment.