2017-02-08 18:52:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-05 20:44:22 +00:00
|
|
|
"errors"
|
2017-02-08 18:52:34 +00:00
|
|
|
"fmt"
|
2017-07-03 18:15:59 +00:00
|
|
|
"sort"
|
2019-10-24 14:37:26 +00:00
|
|
|
"strconv"
|
2017-02-08 18:52:34 +00:00
|
|
|
"strings"
|
2018-10-23 18:21:27 +00:00
|
|
|
"time"
|
2017-02-08 18:52:34 +00:00
|
|
|
|
2019-01-05 09:10:54 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
2019-06-14 10:41:11 +00:00
|
|
|
|
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
2019-10-24 14:37:26 +00:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2017-02-08 18:52:34 +00:00
|
|
|
)
|
|
|
|
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
type addedOutputQuiet struct {
|
2019-03-23 16:31:05 +00:00
|
|
|
*api.AddedOutput
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
quiet bool
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
func jsonFormatObject(resp interface{}) {
|
2020-04-14 17:58:00 +00:00
|
|
|
switch r := resp.(type) {
|
2017-12-05 20:44:22 +00:00
|
|
|
case nil:
|
|
|
|
return
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*addedOutputQuiet:
|
2019-01-10 10:13:21 +00:00
|
|
|
// print original objects as in JSON it makes
|
|
|
|
// no sense to have a human "quiet" output
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
var actual []*api.AddedOutput
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, s := range r {
|
2019-03-23 16:31:05 +00:00
|
|
|
actual = append(actual, s.AddedOutput)
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
}
|
|
|
|
jsonFormatPrint(actual)
|
2017-02-08 18:52:34 +00:00
|
|
|
default:
|
2019-02-27 17:04:35 +00:00
|
|
|
jsonFormatPrint(resp)
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
func jsonFormatPrint(obj interface{}) {
|
|
|
|
j, err := json.MarshalIndent(obj, "", " ")
|
|
|
|
checkErr("generating json output", err)
|
|
|
|
fmt.Printf("%s\n", j)
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
func textFormatObject(resp interface{}) {
|
2020-04-14 17:58:00 +00:00
|
|
|
switch r := resp.(type) {
|
2017-12-05 20:44:22 +00:00
|
|
|
case nil:
|
|
|
|
return
|
2019-10-24 14:37:26 +00:00
|
|
|
case string:
|
|
|
|
fmt.Println(resp)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.ID:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintID(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.GlobalPinInfo:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintGPInfo(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.Pin:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintPin(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.AddedOutput:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintAddedOutput(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *addedOutputQuiet:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintAddedOutputQuiet(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.Version:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintVersion(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.Error:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintError(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case *api.Metric:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintMetric(r)
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*api.ID:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*api.GlobalPinInfo:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*api.Pin:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*api.AddedOutput:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2018-03-05 20:20:33 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*addedOutputQuiet:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
case []*api.Metric:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2018-10-07 16:32:46 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2019-10-22 05:40:32 +00:00
|
|
|
case *api.GlobalRepoGC:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintGlobalRepoGC(r)
|
2019-10-24 14:37:26 +00:00
|
|
|
case []string:
|
2020-04-14 17:58:00 +00:00
|
|
|
for _, item := range r {
|
2019-10-24 14:37:26 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2017-12-05 20:44:22 +00:00
|
|
|
default:
|
|
|
|
checkErr("", errors.New("unsupported type returned"))
|
|
|
|
}
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func textFormatPrintID(obj *api.ID) {
|
2017-02-08 18:52:34 +00:00
|
|
|
if obj.Error != "" {
|
2019-03-14 12:58:36 +00:00
|
|
|
fmt.Printf("%s | ERROR: %s\n", obj.ID.Pretty(), obj.Error)
|
2017-02-08 18:52:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
fmt.Printf(
|
|
|
|
"%s | %s | Sees %d other peers\n",
|
|
|
|
obj.ID.Pretty(),
|
|
|
|
obj.Peername,
|
|
|
|
len(obj.ClusterPeers)-1,
|
|
|
|
)
|
|
|
|
|
2017-07-14 19:22:52 +00:00
|
|
|
addrs := make(sort.StringSlice, 0, len(obj.Addresses))
|
2017-02-08 18:52:34 +00:00
|
|
|
for _, a := range obj.Addresses {
|
2019-02-27 17:04:35 +00:00
|
|
|
addrs = append(addrs, a.String())
|
2017-07-03 18:15:59 +00:00
|
|
|
}
|
|
|
|
addrs.Sort()
|
|
|
|
fmt.Println(" > Addresses:")
|
|
|
|
for _, a := range addrs {
|
2017-02-08 18:52:34 +00:00
|
|
|
fmt.Printf(" - %s\n", a)
|
|
|
|
}
|
|
|
|
if obj.IPFS.Error != "" {
|
|
|
|
fmt.Printf(" > IPFS ERROR: %s\n", obj.IPFS.Error)
|
|
|
|
return
|
|
|
|
}
|
2017-07-03 18:15:59 +00:00
|
|
|
|
2017-07-14 19:22:52 +00:00
|
|
|
ipfsAddrs := make(sort.StringSlice, 0, len(obj.Addresses))
|
2017-02-08 18:52:34 +00:00
|
|
|
for _, a := range obj.IPFS.Addresses {
|
2019-02-27 17:04:35 +00:00
|
|
|
ipfsAddrs = append(ipfsAddrs, a.String())
|
2017-07-03 18:15:59 +00:00
|
|
|
}
|
|
|
|
ipfsAddrs.Sort()
|
2019-03-11 23:08:51 +00:00
|
|
|
fmt.Printf(" > IPFS: %s\n", obj.IPFS.ID.Pretty())
|
2017-07-03 18:15:59 +00:00
|
|
|
for _, a := range ipfsAddrs {
|
2017-02-08 18:52:34 +00:00
|
|
|
fmt.Printf(" - %s\n", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func textFormatPrintGPInfo(obj *api.GlobalPinInfo) {
|
2020-05-15 00:13:43 +00:00
|
|
|
var b strings.Builder
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
peers := make([]string, 0, len(obj.PeerMap))
|
2020-05-15 22:32:28 +00:00
|
|
|
for k := range obj.PeerMap {
|
2017-07-03 18:15:59 +00:00
|
|
|
peers = append(peers, k)
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
sort.Strings(peers)
|
2017-07-03 18:15:59 +00:00
|
|
|
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Fprintf(&b, "%s", obj.Cid)
|
2020-05-15 22:32:28 +00:00
|
|
|
if obj.Name != "" {
|
|
|
|
fmt.Fprintf(&b, " | %s", obj.Name)
|
2020-05-15 00:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteString(":\n")
|
|
|
|
|
2017-07-03 18:15:59 +00:00
|
|
|
for _, k := range peers {
|
|
|
|
v := obj.PeerMap[k]
|
2018-09-21 17:28:55 +00:00
|
|
|
if len(v.PeerName) > 0 {
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Fprintf(&b, " > %-20s : %s", v.PeerName, strings.ToUpper(v.Status.String()))
|
2018-09-21 17:28:55 +00:00
|
|
|
} else {
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Fprintf(&b, " > %-20s : %s", k, strings.ToUpper(v.Status.String()))
|
2018-09-21 17:28:55 +00:00
|
|
|
}
|
2017-02-08 18:52:34 +00:00
|
|
|
if v.Error != "" {
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Fprintf(&b, ": %s", v.Error)
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
txt, _ := v.TS.MarshalText()
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Fprintf(&b, " | %s\n", txt)
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Print(b.String())
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func textFormatPrintVersion(obj *api.Version) {
|
|
|
|
fmt.Println(obj.Version)
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func textFormatPrintPin(obj *api.Pin) {
|
2020-04-17 11:36:05 +00:00
|
|
|
t := strings.ToUpper(obj.Type.String())
|
|
|
|
if obj.Mode == api.PinModeDirect {
|
|
|
|
t = t + "-DIRECT"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s | %s | %s | ", obj.Cid, obj.Name, t)
|
2018-01-15 18:32:54 +00:00
|
|
|
|
|
|
|
if obj.ReplicationFactorMin < 0 {
|
2018-04-20 15:41:15 +00:00
|
|
|
fmt.Printf("Repl. Factor: -1 | Allocations: [everywhere]")
|
2017-02-13 15:46:53 +00:00
|
|
|
} else {
|
2019-02-27 17:04:35 +00:00
|
|
|
sortAlloc := api.PeersToStrings(obj.Allocations)
|
|
|
|
sort.Strings(sortAlloc)
|
2018-04-20 15:41:15 +00:00
|
|
|
fmt.Printf("Repl. Factor: %d--%d | Allocations: %s",
|
2018-01-16 10:19:39 +00:00
|
|
|
obj.ReplicationFactorMin, obj.ReplicationFactorMax,
|
|
|
|
sortAlloc)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2018-03-18 19:29:02 +00:00
|
|
|
var recStr string
|
2018-07-04 16:30:24 +00:00
|
|
|
switch obj.MaxDepth {
|
|
|
|
case 0:
|
|
|
|
recStr = "Direct"
|
|
|
|
case -1:
|
2018-03-18 19:29:02 +00:00
|
|
|
recStr = "Recursive"
|
2018-07-04 16:30:24 +00:00
|
|
|
default:
|
|
|
|
recStr = fmt.Sprintf("Recursive-%d", obj.MaxDepth)
|
2018-03-18 19:29:02 +00:00
|
|
|
}
|
2018-07-04 16:30:24 +00:00
|
|
|
|
2019-08-19 04:52:50 +00:00
|
|
|
fmt.Printf(" | %s", recStr)
|
|
|
|
|
|
|
|
fmt.Printf(" | Metadata:")
|
2019-08-27 04:02:34 +00:00
|
|
|
if len(obj.Metadata) == 0 {
|
2020-04-11 08:28:35 +00:00
|
|
|
fmt.Printf(" no")
|
2019-08-27 04:02:34 +00:00
|
|
|
} else {
|
2020-04-11 08:28:35 +00:00
|
|
|
fmt.Printf(" yes")
|
2019-08-19 04:52:50 +00:00
|
|
|
}
|
2020-04-11 08:28:35 +00:00
|
|
|
expireAt := "Exp: ∞"
|
|
|
|
if !obj.ExpireAt.IsZero() {
|
|
|
|
expireAt = humanize.Time(obj.ExpireAt)
|
|
|
|
}
|
|
|
|
fmt.Printf(" | %s\n", expireAt)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2017-03-28 15:48:26 +00:00
|
|
|
|
2018-03-05 20:20:33 +00:00
|
|
|
func textFormatPrintAddedOutput(obj *api.AddedOutput) {
|
2018-10-03 21:03:30 +00:00
|
|
|
fmt.Printf("added %s %s\n", obj.Cid, obj.Name)
|
2018-03-05 20:20:33 +00:00
|
|
|
}
|
|
|
|
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
func textFormatPrintAddedOutputQuiet(obj *addedOutputQuiet) {
|
|
|
|
if obj.quiet {
|
2019-03-23 16:31:05 +00:00
|
|
|
fmt.Printf("%s\n", obj.AddedOutput.Cid)
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
} else {
|
2019-03-23 16:31:05 +00:00
|
|
|
textFormatPrintAddedOutput(obj.AddedOutput)
|
Fix #632: Add --no-stream to ipfs-cluster-ctl
tl;dr: this solves the user's immediate need and, even if not tne strictest
solution, it is the simplest one.
I think we should not have the server buffer output when we can do it rather
easily client side and have the clients use their own memory for the task even
if `stream-channels=false` would do this.
We can always change the approach, but this is the minimal solution to
json array with all the AddedOutput things.
We might not buffer at all and hack a `[`, `,` separating elements and `]`
at the end, when json encoding is enabled, etc. But that would not be clean,
specially if we mean to support more output formats at some point.
Enabling supporting stream-channels=false in the api/rest/client means adding
new methods, with tests, modifying the interface etc etc. for what is
essentially a presentation issue in "ctl" in the end. Similarly we could
buffer inside the client, but it is so trivial that I don't see advatange.
We should also start thinking about moving to streaming API endpoints and
when that moment arrives we shall revisit this discussion.
I have removed the hacky manual output parts by declaring a custom
addedOutputQuiet type which wraps added output and is understood by
the formatters.go helpers.
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2019-01-09 17:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 16:32:46 +00:00
|
|
|
func textFormatPrintMetric(obj *api.Metric) {
|
2019-10-24 14:37:26 +00:00
|
|
|
if obj.Name == "freespace" {
|
|
|
|
u, err := strconv.ParseUint(obj.Value, 10, 64)
|
|
|
|
checkErr("parsing to uint64", err)
|
2020-04-14 17:58:00 +00:00
|
|
|
fmt.Printf("%s | freespace: %s | Expires in: %s\n", peer.Encode(obj.Peer), humanize.Bytes(u), humanize.Time(time.Unix(0, obj.Expire)))
|
2019-10-24 14:37:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:58:00 +00:00
|
|
|
fmt.Printf("%s | %s | Expires in: %s\n", peer.Encode(obj.Peer), obj.Name, humanize.Time(time.Unix(0, obj.Expire)))
|
2018-10-07 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 05:40:32 +00:00
|
|
|
func textFormatPrintGlobalRepoGC(obj *api.GlobalRepoGC) {
|
|
|
|
peers := make(sort.StringSlice, 0, len(obj.PeerMap))
|
|
|
|
for peer := range obj.PeerMap {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
peers.Sort()
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
item := obj.PeerMap[peer]
|
|
|
|
// If peer name is set, use it instead of peer ID.
|
|
|
|
if len(item.Peername) > 0 {
|
|
|
|
peer = item.Peername
|
|
|
|
}
|
|
|
|
if item.Error != "" {
|
|
|
|
fmt.Printf("%-15s | ERROR: %s\n", peer, item.Error)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%-15s\n", peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(" > CIDs:\n")
|
|
|
|
for _, key := range item.Keys {
|
|
|
|
if key.Error != "" {
|
|
|
|
// key.Key will be empty
|
|
|
|
fmt.Printf(" - ERROR: %s\n", key.Error)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(" - %s\n", key.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 15:48:26 +00:00
|
|
|
func textFormatPrintError(obj *api.Error) {
|
2017-12-06 14:00:01 +00:00
|
|
|
fmt.Printf("An error occurred:\n")
|
2017-03-28 15:48:26 +00:00
|
|
|
fmt.Printf(" Code: %d\n", obj.Code)
|
|
|
|
fmt.Printf(" Message: %s\n", obj.Message)
|
|
|
|
}
|
2019-01-05 09:10:54 +00:00
|
|
|
|
2019-01-07 18:45:07 +00:00
|
|
|
func trackerStatusAllString() string {
|
|
|
|
var strs []string
|
|
|
|
for _, st := range api.TrackerStatusAll() {
|
|
|
|
strs = append(strs, " - "+st.String())
|
2019-01-05 09:10:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:45:07 +00:00
|
|
|
sort.Strings(strs)
|
|
|
|
return strings.Join(strs, "\n")
|
2019-01-05 09:10:54 +00:00
|
|
|
}
|