forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
101 lines (85 loc) · 2.14 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package docker
import (
"fmt"
"io"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/cmd/util/prefixwriter"
)
const (
taskNamePrefix = "-- "
taskIndent = " "
)
// TaskPrinter is a helper for start task output
type TaskPrinter struct {
taskWriter *taskWriter
out io.Writer
}
// NewTaskPrinter creates a new TaskPrinter
func NewTaskPrinter(out io.Writer) *TaskPrinter {
return &TaskPrinter{
out: out,
}
}
// StartTask writes out the header for a task
func (p *TaskPrinter) StartTask(name string) {
fmt.Fprintf(p.out, "%s%s ... ", taskNamePrefix, name)
if glog.V(1) {
fmt.Fprintf(p.out, "\n")
}
}
// Success writes out a success marker for a task
func (p *TaskPrinter) Success() {
if (p.taskWriter != nil && p.taskWriter.used) || bool(glog.V(1)) {
return
}
fmt.Fprintf(p.out, "OK\n")
}
// TaskWriter is a writer that can be used to write task output
func (p *TaskPrinter) TaskWriter() io.Writer {
p.taskWriter = &taskWriter{w: p.out}
return prefixwriter.New(taskIndent, p.taskWriter)
}
// Failure writes out a failure marker for a task and outputs the error
// that caused the failure
func (p *TaskPrinter) Failure(err error) {
fmt.Fprintf(p.out, "FAIL\n")
PrintError(err, prefixwriter.New(taskIndent, p.out))
}
type hasCause interface {
Cause() error
}
type hasDetails interface {
Details() string
}
type hasSolution interface {
Solution() string
}
func PrintError(err error, out io.Writer) {
fmt.Fprintf(out, "Error: %v\n", err)
if d, ok := err.(hasDetails); ok && len(d.Details()) > 0 {
fmt.Fprintf(out, "Details:\n")
w := prefixwriter.New(" ", out)
fmt.Fprintf(w, "%s\n", d.Details())
}
if s, ok := err.(hasSolution); ok && len(s.Solution()) > 0 {
fmt.Fprintf(out, "Solution:\n")
w := prefixwriter.New(" ", out)
fmt.Fprintf(w, "%s\n", s.Solution())
}
if c, ok := err.(hasCause); ok && c.Cause() != nil {
fmt.Fprintf(out, "Caused By:\n")
w := prefixwriter.New(" ", out)
PrintError(c.Cause(), w)
}
}
type taskWriter struct {
w io.Writer
used bool
}
func (t *taskWriter) Write(p []byte) (n int, err error) {
if !t.used {
t.used = true
t.w.Write([]byte("\n"))
}
return t.w.Write(p)
}