Add tail messages on progress writer

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2021-03-25 18:21:36 -03:00
parent ee4e85ae1d
commit e5834f37fd
6 changed files with 34 additions and 21 deletions

View File

@ -30,5 +30,8 @@ func (p *noopWriter) Start(ctx context.Context) error {
func (p *noopWriter) Event(e Event) {
}
func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {
}
func (p *noopWriter) Stop() {
}

View File

@ -40,6 +40,10 @@ func (p *plainWriter) Event(e Event) {
fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText)
}
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
}
func (p *plainWriter) Stop() {
p.done <- true
}

View File

@ -32,13 +32,14 @@ import (
)
type ttyWriter struct {
out io.Writer
events map[string]Event
eventIDs []string
repeated bool
numLines int
done chan bool
mtx *sync.RWMutex
out io.Writer
events map[string]Event
eventIDs []string
repeated bool
numLines int
done chan bool
mtx *sync.RWMutex
tailEvents []string
}
func (w *ttyWriter) Start(ctx context.Context) error {
@ -48,9 +49,11 @@ func (w *ttyWriter) Start(ctx context.Context) error {
select {
case <-ctx.Done():
w.print()
w.printTailEvents()
return ctx.Err()
case <-w.done:
w.print()
w.printTailEvents()
return nil
case <-ticker.C:
w.print()
@ -91,6 +94,20 @@ func (w *ttyWriter) Event(e Event) {
}
}
func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
w.mtx.Lock()
defer w.mtx.Unlock()
w.tailEvents = append(w.tailEvents, fmt.Sprintf(msg, args...))
}
func (w *ttyWriter) printTailEvents() {
w.mtx.Lock()
defer w.mtx.Unlock()
for _, msg := range w.tailEvents {
fmt.Fprintln(w.out, msg)
}
}
func (w *ttyWriter) print() {
w.mtx.Lock()
defer w.mtx.Unlock()

View File

@ -31,6 +31,7 @@ type Writer interface {
Start(context.Context) error
Stop()
Event(Event)
TailMsgf(string, ...interface{})
}
type writerKey struct{}

View File

@ -21,7 +21,6 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
@ -72,13 +71,7 @@ func (s *composeService) Pull(ctx context.Context, project *types.Project, opts
if !opts.IgnoreFailures {
return err
}
// If IgnoreFailures we still want to show the error message
w.Event(progress.Event{
ID: fmt.Sprintf("Pulling %s:", service.Name),
Text: fmt.Sprintf("%v", err),
Status: progress.Error,
StatusText: fmt.Sprintf("%s", err),
})
w.TailMsgf("Pulling %s: %s", service.Name, err.Error())
}
return nil
})

View File

@ -70,12 +70,7 @@ func (s *composeService) Push(ctx context.Context, project *types.Project, optio
if !options.IgnoreFailures {
return err
}
w.Event(progress.Event{
ID: fmt.Sprintf("Pushing %s:", service.Name),
Text: fmt.Sprintf("%v", err),
Status: progress.Error,
StatusText: fmt.Sprintf("%s", err),
})
w.TailMsgf("Pushing %s: %s", service.Name, err.Error())
}
return nil
})