forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
72 lines (62 loc) · 1.85 KB
/
ui.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
package ui
import (
"fmt"
"io"
"os"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type UI interface {
ErrorLinef(pattern string, args ...interface{})
PrintLinef(pattern string, args ...interface{})
BeginLinef(pattern string, args ...interface{})
EndLinef(pattern string, args ...interface{})
}
type ui struct {
outWriter io.Writer
errWriter io.Writer
logger boshlog.Logger
logTag string
}
func NewConsoleUI(logger boshlog.Logger) UI {
return NewWriterUI(os.Stdout, os.Stderr, logger)
}
func NewWriterUI(outWriter, errWriter io.Writer, logger boshlog.Logger) UI {
return &ui{
outWriter: outWriter,
errWriter: errWriter,
logger: logger,
logTag: "ui",
}
}
// ErrorLinef starts and ends a text error line
func (ui *ui) ErrorLinef(pattern string, args ...interface{}) {
message := fmt.Sprintf(pattern, args...)
_, err := fmt.Fprintln(ui.errWriter, message)
if err != nil {
ui.logger.Error(ui.logTag, "UI.ErrorLinef failed (message='%s'): %s", message, err)
}
}
// Printlnf starts and ends a text line
func (ui *ui) PrintLinef(pattern string, args ...interface{}) {
message := fmt.Sprintf(pattern, args...)
_, err := fmt.Fprintln(ui.outWriter, message)
if err != nil {
ui.logger.Error(ui.logTag, "UI.PrintLinef failed (message='%s'): %s", message, err)
}
}
// PrintBeginf starts a text line
func (ui *ui) BeginLinef(pattern string, args ...interface{}) {
message := fmt.Sprintf(pattern, args...)
_, err := fmt.Fprint(ui.outWriter, message)
if err != nil {
ui.logger.Error(ui.logTag, "UI.BeginLinef failed (message='%s'): %s", message, err)
}
}
// PrintEndf ends a text line
func (ui *ui) EndLinef(pattern string, args ...interface{}) {
message := fmt.Sprintf(pattern, args...)
_, err := fmt.Fprintln(ui.outWriter, message)
if err != nil {
ui.logger.Error(ui.logTag, "UI.EndLinef failed (message='%s'): %s", message, err)
}
}