-
Notifications
You must be signed in to change notification settings - Fork 162
/
ui.go
159 lines (126 loc) · 3.8 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ui
import (
"errors"
"fmt"
"io"
"os"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
"github.com/mattn/go-isatty"
"github.com/vito/go-interact/interact"
. "github.com/cloudfoundry/bosh-cli/ui/table"
)
type WriterUI struct {
outWriter io.Writer
errWriter io.Writer
logger boshlog.Logger
logTag string
}
func NewConsoleUI(logger boshlog.Logger) *WriterUI {
return NewWriterUI(os.Stdout, os.Stderr, logger)
}
func NewWriterUI(outWriter, errWriter io.Writer, logger boshlog.Logger) *WriterUI {
return &WriterUI{
outWriter: outWriter,
errWriter: errWriter,
logTag: "ui",
logger: logger,
}
}
func (ui *WriterUI) IsTTY() bool {
file, ok := ui.outWriter.(*os.File)
return ok && isatty.IsTerminal(file.Fd())
}
// ErrorLinef starts and ends a text error line
func (ui *WriterUI) 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 *WriterUI) 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 *WriterUI) 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 *WriterUI) 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)
}
}
func (ui *WriterUI) PrintBlock(block []byte) {
_, err := ui.outWriter.Write(block)
if err != nil {
ui.logger.Error(ui.logTag, "UI.PrintBlock failed (message='%s'): %s", block, err)
}
}
func (ui *WriterUI) PrintErrorBlock(block string) {
_, err := fmt.Fprint(ui.outWriter, block)
if err != nil {
ui.logger.Error(ui.logTag, "UI.PrintErrorBlock failed (message='%s'): %s", block, err)
}
}
func (ui *WriterUI) PrintTable(table Table) {
err := table.Print(ui.outWriter)
if err != nil {
ui.logger.Error(ui.logTag, "UI.PrintTable failed: %s", err)
}
}
func (ui *WriterUI) AskForText(label string) (string, error) {
var text string
err := interact.NewInteraction(label).Resolve(&text)
if err != nil {
return "", bosherr.WrapError(err, "Asking for text")
}
return text, nil
}
func (ui *WriterUI) AskForChoice(label string, options []string) (int, error) {
var choices []interact.Choice
for i, opt := range options {
choices = append(choices, interact.Choice{Display: opt, Value: i})
}
var chosen int
err := interact.NewInteraction(label, choices...).Resolve(&chosen)
if err != nil {
return 0, bosherr.WrapError(err, "Asking for choice")
}
return chosen, nil
}
func (ui *WriterUI) AskForPassword(label string) (string, error) {
var password interact.Password
err := interact.NewInteraction(label).Resolve(&password)
if err != nil {
return "", bosherr.WrapError(err, "Asking for password")
}
return string(password), nil
}
func (ui *WriterUI) AskForConfirmation() error {
falseByDefault := false
err := interact.NewInteraction("Continue?").Resolve(&falseByDefault)
if err != nil {
return bosherr.WrapError(err, "Asking for confirmation")
}
if falseByDefault == false {
return errors.New("Stopped")
}
return nil
}
func (ui *WriterUI) IsInteractive() bool {
return true
}
func (ui *WriterUI) Flush() {}