-
Notifications
You must be signed in to change notification settings - Fork 4
/
printer.go
201 lines (176 loc) · 4.52 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (C) 2017-2019 Miquel Sabaté Solà <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"strings"
"text/tabwriter"
"unicode"
"unicode/utf8"
)
func header(elements interface{}, join bool) string {
t := reflect.TypeOf(elements).Elem()
header := ""
for i := 0; i < t.NumField(); i++ {
header += t.Field(i).Name + "\t"
}
return strings.TrimRight(header, "\t")
}
func tabifyStruct(element interface{}) string {
str := ""
v := reflect.ValueOf(element)
for i := 0; i < v.NumField(); i++ {
str += fmt.Sprintf("%v\t", v.Field(i))
}
return str
}
func printList(elements interface{}) {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, header(elements, true))
v := reflect.ValueOf(elements)
for i := 0; i < v.Len(); i++ {
fmt.Fprintln(w, v.Index(i))
}
w.Flush()
}
func printValidate(data Validate) error {
if data.Valid {
if !globalConfig.quiet {
fmt.Println("Valid")
}
return nil
}
return messagesError(data.Messages)
}
func capitalize(str string) string {
r, n := utf8.DecodeRuneInString(str)
return string(unicode.ToUpper(r)) + str[n:]
}
func printHealth(data Health) error {
success := true
keys, values := "", ""
for k, v := range data {
if !globalConfig.quiet {
keys += strings.ToUpper(k) + "\t"
values += capitalize(v.Message) + "\t"
}
if !v.Success {
success = false
if globalConfig.quiet {
break
}
}
}
if !globalConfig.quiet {
keys = strings.TrimRight(keys, "\t")
values = strings.TrimRight(values, "\t")
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, keys)
fmt.Fprintln(w, values)
w.Flush()
}
if success {
return nil
}
return errors.New("some services are not healthy")
}
func messagesError(messages map[string][]string) error {
str := []string{}
for k, v := range messages {
str = append(str, "- "+capitalize(k)+": "+strings.Join(v, ". "))
}
return errors.New(strings.Join(str, "\n"))
}
func prettyPrint(kind int, body []byte, single bool) error {
var bodyStr string
if len(body) == 0 || string(body) == "null" {
return nil
}
if single {
bodyStr = "[" + string(body) + "]"
} else {
bodyStr = string(body)
}
// TODO: generilize
switch kind {
case kindUser:
data := Users{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindApplicationToken:
data := ApplicationTokens{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindPlainToken:
data := PlainTokens{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindTeam:
data := Teams{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindNamespace:
data := Namespaces{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindTag:
data := Tags{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindRegistry:
data := Registries{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindRepository:
data := Repositories{}
_ = json.Unmarshal([]byte(bodyStr), &data)
printList(data)
case kindValidate:
data := Validate{}
_ = json.Unmarshal([]byte(bodyStr), &data)
return printValidate(data)
case kindHealth:
data := Health{}
_ = json.Unmarshal([]byte(bodyStr), &data)
return printHealth(data)
case kindBootstrap:
data := PlainTokens{}
_ = json.Unmarshal([]byte(bodyStr), &data)
return printBootstrap(data[0].PlainToken)
default:
}
return nil
}
func printBootstrap(token string) error {
fmt.Printf("You can now use the user '%v' with the following token: %v\n",
retainedValues["username"], token)
return nil
}
func printAndQuit(response *http.Response, kind int, single bool) error {
body, _ := ioutil.ReadAll(response.Body)
switch globalConfig.format {
case jsonFmt:
fmt.Print(string(body))
default:
return prettyPrint(kind, body, single)
}
return nil
}