2017-02-08 18:52:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
formatNone = iota
|
|
|
|
formatID
|
|
|
|
formatGPInfo
|
|
|
|
formatString
|
|
|
|
formatVersion
|
2017-03-08 15:57:27 +00:00
|
|
|
formatPin
|
2017-03-28 15:48:26 +00:00
|
|
|
formatError
|
2017-02-08 18:52:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2017-03-08 15:57:27 +00:00
|
|
|
case formatPin:
|
|
|
|
var obj api.PinSerial
|
2017-02-13 15:46:53 +00:00
|
|
|
textFormatDecodeOn(body, &obj)
|
2017-03-08 15:57:27 +00:00
|
|
|
textFormatPrintPin(&obj)
|
2017-03-28 15:48:26 +00:00
|
|
|
case formatError:
|
|
|
|
var obj api.Error
|
|
|
|
textFormatDecodeOn(body, &obj)
|
|
|
|
textFormatPrintError(&obj)
|
|
|
|
|
2017-02-08 18:52:34 +00:00
|
|
|
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))
|
|
|
|
fmt.Println(" > Addresses:")
|
|
|
|
for _, a := range obj.Addresses {
|
|
|
|
fmt.Printf(" - %s\n", a)
|
|
|
|
}
|
|
|
|
if obj.IPFS.Error != "" {
|
|
|
|
fmt.Printf(" > IPFS ERROR: %s\n", obj.IPFS.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf(" > IPFS: %s\n", obj.IPFS.ID)
|
|
|
|
for _, a := range obj.IPFS.Addresses {
|
|
|
|
fmt.Printf(" - %s\n", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func textFormatPrintGPinfo(obj *api.GlobalPinInfoSerial) {
|
2017-05-18 11:16:27 +00:00
|
|
|
fmt.Printf("%s :\n", obj.Cid)
|
2017-02-08 18:52:34 +00:00
|
|
|
for k, v := range obj.PeerMap {
|
|
|
|
if v.Error != "" {
|
2017-05-18 11:16:27 +00:00
|
|
|
fmt.Printf(" > Peer %s: ERROR | %s\n", k, v.Error)
|
2017-02-08 18:52:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf(" > Peer %s: %s | %s\n", k, strings.ToUpper(v.Status), v.TS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func textFormatPrintVersion(obj *api.Version) {
|
|
|
|
fmt.Println(obj.Version)
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
func textFormatPrintPin(obj *api.PinSerial) {
|
2017-02-13 15:46:53 +00:00
|
|
|
fmt.Printf("%s | Allocations: ", obj.Cid)
|
2017-03-08 17:28:43 +00:00
|
|
|
if obj.ReplicationFactor < 0 {
|
2017-02-13 15:46:53 +00:00
|
|
|
fmt.Printf("[everywhere]\n")
|
|
|
|
} else {
|
2017-03-08 15:57:27 +00:00
|
|
|
fmt.Printf("%s\n", obj.Allocations)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-28 15:48:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|