Skip to content

Commit

Permalink
Fix the timer display
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 237d4b4 commit e7039da
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package console

import (
"context"
"fmt"
"os/exec"
"time"
Expand Down Expand Up @@ -54,33 +55,37 @@ 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))

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start the timer log
go displayProgress(ctx, s.logger, 10*time.Second, fmt.Sprintf("Still running command '%s'", cmd))

// Run the command
out, err := c.CombinedOutput()

// Stop the timer log
cancel()

if err != nil {
return string(out), fmt.Errorf("failed to run %s: %v", cmd, err)
}

return string(out), err
}

func displayProgress(log logger.Interface, tick time.Duration, message string) {
func displayProgress(ctx context.Context, log logger.Interface, tick time.Duration, message string) {
ticker := time.NewTicker(tick)
done := make(chan bool)

go func() {
for {
select {
case <-done:
ticker.Stop()
return
case <-ticker.C:
log.Info(message)
}
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
log.Info(message)
}
}()

return
}
}

func (s StandardConsole) Start(cmd *exec.Cmd, opts ...func(cmd *exec.Cmd)) error {
Expand Down

0 comments on commit e7039da

Please sign in to comment.