This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_ui.go
161 lines (125 loc) · 3.28 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ui
import (
"encoding/json"
"fmt"
"reflect"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
. "github.com/cloudfoundry/bosh-cli/ui/table"
"strconv"
)
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 map[string]string
Rows []map[string]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
header := map[string]string{}
if len(table.Header) > 0 {
for i, val := range table.Header {
if val.Hidden {
continue
}
if val.Key == string(UNKNOWN_HEADER_MAPPING) {
table.Header[i].Key = strconv.Itoa(i)
}
header[table.Header[i].Key] = val.Title
}
} else if len(table.AsRows()) > 0 {
var rawHeaders []Header
for i, _ := range table.AsRows()[0] {
val := Header{
Key: fmt.Sprintf("col_%d", i),
Hidden: false,
}
header[val.Key] = val.Title
rawHeaders = append(rawHeaders, val)
}
table.Header = rawHeaders
}
resp := tableResp{
Content: table.Content,
Header: header,
Rows: ui.stringRows(table.Header, 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(header []Header, rows [][]Value) []map[string]string {
result := []map[string]string{}
for _, row := range rows {
data := map[string]string{}
for i, col := range row {
if header[i].Hidden {
continue
}
data[header[i].Key] = col.String()
}
result = append(result, data)
}
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)
}