forked from ipfs-cluster/ipfs-cluster
/
formatters.go
148 lines (130 loc) · 3.11 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
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/ipfs/ipfs-cluster/api"
)
const (
formatNone = iota
formatID
formatGPInfo
formatString
formatVersion
formatPin
formatError
)
type format int
func textFormat(body []byte, format int) {
if len(body) < 2 {
fmt.Println("")
}
slice := body[0] == '['
if slice {
textFormatSlice(body, format)
} else {
textFormatObject(body, format)
}
}
func textFormatObject(body []byte, format int) {
switch format {
case formatID:
var obj api.IDSerial
textFormatDecodeOn(body, &obj)
textFormatPrintIDSerial(&obj)
case formatGPInfo:
var obj api.GlobalPinInfoSerial
textFormatDecodeOn(body, &obj)
textFormatPrintGPinfo(&obj)
case formatVersion:
var obj api.Version
textFormatDecodeOn(body, &obj)
textFormatPrintVersion(&obj)
case formatPin:
var obj api.PinSerial
textFormatDecodeOn(body, &obj)
textFormatPrintPin(&obj)
case formatError:
var obj api.Error
textFormatDecodeOn(body, &obj)
textFormatPrintError(&obj)
default:
var obj interface{}
textFormatDecodeOn(body, &obj)
fmt.Printf("%s\n", obj)
}
}
func textFormatSlice(body []byte, format int) {
var rawMsg []json.RawMessage
textFormatDecodeOn(body, &rawMsg)
for _, raw := range rawMsg {
textFormatObject(raw, format)
}
}
func textFormatDecodeOn(body []byte, obj interface{}) {
checkErr("decoding JSON", json.Unmarshal(body, obj))
}
func textFormatPrintIDSerial(obj *api.IDSerial) {
if obj.Error != "" {
fmt.Printf("%s | ERROR: %s\n", obj.ID, obj.Error)
return
}
fmt.Printf("%s | %d peers\n", obj.ID, len(obj.ClusterPeers))
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 v.Error != "" {
fmt.Printf(" > Peer %s : ERROR | %s\n", k, v.Error)
continue
}
fmt.Printf(" > Peer %s : %s | %s\n", k, strings.ToUpper(v.Status), v.TS)
}
}
func textFormatPrintVersion(obj *api.Version) {
fmt.Println(obj.Version)
}
func textFormatPrintPin(obj *api.PinSerial) {
fmt.Printf("%s | Allocations: ", obj.Cid)
if obj.ReplicationFactor < 0 {
fmt.Printf("[everywhere]\n")
} else {
var sortAlloc sort.StringSlice = obj.Allocations
sortAlloc.Sort()
fmt.Printf("%s\n", sortAlloc)
}
}
func textFormatPrintError(obj *api.Error) {
fmt.Printf("An error ocurred:\n")
fmt.Printf(" Code: %d\n", obj.Code)
fmt.Printf(" Message: %s\n", obj.Message)
}