forked from ipfs-cluster/ipfs-cluster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatters.go
273 lines (249 loc) · 6.66 KB
/
formatters.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/elastos/Elastos.NET.Hive.Cluster/api"
)
type addedOutputQuiet struct {
added *api.AddedOutput
quiet bool
}
func jsonFormatObject(resp interface{}) {
switch resp.(type) {
case nil:
return
case api.ID:
jsonFormatPrint(resp.(api.ID).ToSerial())
case api.GlobalPinInfo:
jsonFormatPrint(resp.(api.GlobalPinInfo).ToSerial())
case api.Pin:
jsonFormatPrint(resp.(api.Pin).ToSerial())
case api.AddedOutput:
jsonFormatPrint(resp.(api.AddedOutput))
case addedOutputQuiet:
// print original object as in JSON it does
// not make sense to have a human "quiet" output.
jsonFormatPrint(resp.(addedOutputQuiet).added)
case api.Version:
jsonFormatPrint(resp.(api.Version))
case api.Metric:
serial := resp.(api.Metric)
textFormatPrintMetric(&serial)
case api.Error:
jsonFormatPrint(resp.(api.Error))
case []api.ID:
r := resp.([]api.ID)
serials := make([]api.IDSerial, len(r), len(r))
for i, item := range r {
serials[i] = item.ToSerial()
}
jsonFormatPrint(serials)
case []api.GlobalPinInfo:
r := resp.([]api.GlobalPinInfo)
serials := make([]api.GlobalPinInfoSerial, len(r), len(r))
for i, item := range r {
serials[i] = item.ToSerial()
}
jsonFormatPrint(serials)
case []api.Pin:
r := resp.([]api.Pin)
serials := make([]api.PinSerial, len(r), len(r))
for i, item := range r {
serials[i] = item.ToSerial()
}
jsonFormatPrint(serials)
case []api.AddedOutput:
serials := resp.([]api.AddedOutput)
jsonFormatPrint(serials)
case []addedOutputQuiet:
// print original objects as in JSON it makes
// no sense to have a human "quiet" output
serials := resp.([]addedOutputQuiet)
var actual []*api.AddedOutput
for _, s := range serials {
actual = append(actual, s.added)
}
jsonFormatPrint(actual)
case []api.Metric:
serials := resp.([]api.Metric)
jsonFormatPrint(serials)
default:
checkErr("", errors.New("unsupported type returned"))
}
}
func jsonFormatPrint(obj interface{}) {
j, err := json.MarshalIndent(obj, "", " ")
checkErr("generating json output", err)
fmt.Printf("%s\n", j)
}
func textFormatObject(resp interface{}) {
switch resp.(type) {
case nil:
return
case api.ID:
serial := resp.(api.ID).ToSerial()
textFormatPrintIDSerial(&serial)
case api.GlobalPinInfo:
serial := resp.(api.GlobalPinInfo).ToSerial()
textFormatPrintGPInfo(&serial)
case api.Pin:
serial := resp.(api.Pin).ToSerial()
textFormatPrintPin(&serial)
case api.AddedOutput:
serial := resp.(api.AddedOutput)
textFormatPrintAddedOutput(&serial)
case addedOutputQuiet:
serial := resp.(addedOutputQuiet)
textFormatPrintAddedOutputQuiet(&serial)
case api.Version:
serial := resp.(api.Version)
textFormatPrintVersion(&serial)
case api.Error:
serial := resp.(api.Error)
textFormatPrintError(&serial)
case api.Metric:
serial := resp.(api.Metric)
textFormatPrintMetric(&serial)
case []api.ID:
for _, item := range resp.([]api.ID) {
textFormatObject(item)
}
case []api.GlobalPinInfo:
for _, item := range resp.([]api.GlobalPinInfo) {
textFormatObject(item)
}
case []api.Pin:
for _, item := range resp.([]api.Pin) {
textFormatObject(item)
}
case []api.AddedOutput:
for _, item := range resp.([]api.AddedOutput) {
textFormatObject(item)
}
case []addedOutputQuiet:
for _, item := range resp.([]addedOutputQuiet) {
textFormatObject(item)
}
case []api.Metric:
for _, item := range resp.([]api.Metric) {
textFormatObject(item)
}
default:
checkErr("", errors.New("unsupported type returned"))
}
}
func textFormatPrintIDSerial(obj *api.IDSerial) {
if obj.Error != "" {
fmt.Printf("%s | ERROR: %s\n", obj.ID, obj.Error)
return
}
fmt.Printf("%s | %s | Sees %d other peers\n", obj.ID, obj.Peername, len(obj.ClusterPeers)-1)
addrs := make(sort.StringSlice, 0, len(obj.Addresses))
for _, a := range obj.Addresses {
addrs = append(addrs, string(a))
}
addrs.Sort()
fmt.Println(" > Addresses:")
for _, a := range addrs {
fmt.Printf(" - %s\n", a)
}
if obj.IPFS.Error != "" {
fmt.Printf(" > IPFS ERROR: %s\n", obj.IPFS.Error)
return
}
ipfsAddrs := make(sort.StringSlice, 0, len(obj.Addresses))
for _, a := range obj.IPFS.Addresses {
ipfsAddrs = append(ipfsAddrs, string(a))
}
ipfsAddrs.Sort()
fmt.Printf(" > IPFS: %s\n", obj.IPFS.ID)
for _, a := range ipfsAddrs {
fmt.Printf(" - %s\n", a)
}
}
func textFormatPrintGPInfo(obj *api.GlobalPinInfoSerial) {
fmt.Printf("%s :\n", obj.Cid)
peers := make(sort.StringSlice, 0, len(obj.PeerMap))
for k := range obj.PeerMap {
peers = append(peers, k)
}
peers.Sort()
for _, k := range peers {
v := obj.PeerMap[k]
if len(v.PeerName) > 0 {
fmt.Printf(" > %-15s : %s", v.PeerName, strings.ToUpper(v.Status))
} else {
fmt.Printf(" > %-15s : %s", k, strings.ToUpper(v.Status))
}
if v.Error != "" {
fmt.Printf(": %s", v.Error)
}
fmt.Printf(" | %s\n", v.TS)
}
}
func textFormatPrintPInfo(obj *api.PinInfoSerial) {
gpinfo := api.GlobalPinInfoSerial{
Cid: obj.Cid,
PeerMap: map[string]api.PinInfoSerial{
obj.Peer: *obj,
},
}
textFormatPrintGPInfo(&gpinfo)
}
func textFormatPrintVersion(obj *api.Version) {
fmt.Println(obj.Version)
}
func textFormatPrintPin(obj *api.PinSerial) {
fmt.Printf("%s | %s | %s | ", obj.Cid, obj.Name, strings.ToUpper(obj.ToPin().Type.String()))
if obj.ReplicationFactorMin < 0 {
fmt.Printf("Repl. Factor: -1 | Allocations: [everywhere]")
} else {
var sortAlloc sort.StringSlice = obj.Allocations
sortAlloc.Sort()
fmt.Printf("Repl. Factor: %d--%d | Allocations: %s",
obj.ReplicationFactorMin, obj.ReplicationFactorMax,
sortAlloc)
}
var recStr string
switch obj.MaxDepth {
case 0:
recStr = "Direct"
case -1:
recStr = "Recursive"
default:
recStr = fmt.Sprintf("Recursive-%d", obj.MaxDepth)
}
fmt.Printf(" | %s\n", recStr)
}
func textFormatPrintAddedOutput(obj *api.AddedOutput) {
fmt.Printf("added %s %s\n", obj.Cid, obj.Name)
}
func textFormatPrintAddedOutputQuiet(obj *addedOutputQuiet) {
if obj.quiet {
fmt.Printf("%s\n", obj.added.Cid)
} else {
textFormatPrintAddedOutput(obj.added)
}
}
func textFormatPrintMetric(obj *api.Metric) {
date := time.Unix(0, obj.Expire).UTC().Format(time.RFC3339)
fmt.Printf("%s: %s | Expire: %s\n", peer.IDB58Encode(obj.Peer), obj.Value, date)
}
func textFormatPrintError(obj *api.Error) {
fmt.Printf("An error occurred:\n")
fmt.Printf(" Code: %d\n", obj.Code)
fmt.Printf(" Message: %s\n", obj.Message)
}
func trackerStatusAllString() string {
var strs []string
for _, st := range api.TrackerStatusAll() {
strs = append(strs, " - "+st.String())
}
sort.Strings(strs)
return strings.Join(strs, "\n")
}