forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_ui.go
139 lines (106 loc) · 2.78 KB
/
json_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
package ui
import (
"encoding/json"
"fmt"
"reflect"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
. "github.com/cloudfoundry/bosh-cli/ui/table"
)
type jsonUI struct {
parent UI
uiResp uiResp
logTag string
logger boshlog.Logger
}
type uiResp struct {
Tables []tableResp
Blocks []string
Lines []string
}
type tableResp struct {
Content string
Header []string
Rows [][]string
Notes []string
}
func NewJSONUI(parent UI, logger boshlog.Logger) UI {
return &jsonUI{parent: parent, logTag: "JSONUI", logger: logger}
}
func (ui *jsonUI) ErrorLinef(pattern string, args ...interface{}) {
ui.addLine(pattern, args)
}
func (ui *jsonUI) PrintLinef(pattern string, args ...interface{}) {
ui.addLine(pattern, args)
}
func (ui *jsonUI) BeginLinef(pattern string, args ...interface{}) {
ui.addLine(pattern, args)
}
func (ui *jsonUI) EndLinef(pattern string, args ...interface{}) {
ui.addLine(pattern, args)
}
func (ui *jsonUI) PrintBlock(block string) {
ui.uiResp.Blocks = append(ui.uiResp.Blocks, block)
}
func (ui *jsonUI) PrintErrorBlock(block string) {
ui.uiResp.Blocks = append(ui.uiResp.Blocks, block)
}
func (ui *jsonUI) PrintTable(table Table) {
table.FillFirstColumn = true
var header []string
if len(table.HeaderVals) > 0 {
for _, val := range table.HeaderVals {
header = append(header, val.String())
}
} else if len(table.Header) > 0 {
header = table.Header
}
resp := tableResp{
Content: table.Content,
Header: header,
Rows: ui.stringRows(table.AsRows()),
Notes: table.Notes,
}
ui.uiResp.Tables = append(ui.uiResp.Tables, resp)
}
func (ui *jsonUI) AskForText(_ string) (string, error) {
panic("Cannot ask for input in JSON UI")
}
func (ui *jsonUI) AskForChoice(_ string, _ []string) (int, error) {
panic("Cannot ask for a choice in JSON UI")
}
func (ui *jsonUI) AskForPassword(_ string) (string, error) {
panic("Cannot ask for password in JSON UI")
}
func (ui *jsonUI) AskForConfirmation() error {
panic("Cannot ask for confirmation in JSON UI")
}
func (ui *jsonUI) IsInteractive() bool {
return ui.parent.IsInteractive()
}
func (ui *jsonUI) Flush() {
defer ui.parent.Flush()
if !reflect.DeepEqual(ui.uiResp, uiResp{}) {
bytes, err := json.MarshalIndent(ui.uiResp, "", " ")
if err != nil {
ui.logger.Error(ui.logTag, "Failed to marshal UI response")
return
}
ui.parent.PrintBlock(string(bytes))
}
}
func (ui *jsonUI) stringRows(vals [][]Value) [][]string {
var result [][]string
for _, row := range vals {
var strs []string
for _, v := range row {
strs = append(strs, v.String())
}
result = append(result, strs)
}
return result
}
func (ui *jsonUI) addLine(pattern string, args []interface{}) {
msg := fmt.Sprintf(pattern, args...)
ui.uiResp.Lines = append(ui.uiResp.Lines, msg)
ui.logger.Debug(ui.logTag, msg)
}