瀏覽代碼

Add tail messages on progress writer

Signed-off-by: Ulysses Souza <[email protected]>
Ulysses Souza 4 年之前
父節點
當前提交
e5834f37fd
共有 6 個文件被更改,包括 34 次插入21 次删除
  1. 3 0
      api/progress/noop.go
  2. 4 0
      api/progress/plain.go
  3. 24 7
      api/progress/tty.go
  4. 1 0
      api/progress/writer.go
  5. 1 8
      local/compose/pull.go
  6. 1 6
      local/compose/push.go

+ 3 - 0
api/progress/noop.go

@@ -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() {
 }

+ 4 - 0
api/progress/plain.go

@@ -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
 }

+ 24 - 7
api/progress/tty.go

@@ -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()

+ 1 - 0
api/progress/writer.go

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

+ 1 - 8
local/compose/pull.go

@@ -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
 		})

+ 1 - 6
local/compose/push.go

@@ -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
 		})