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"
|
2022-03-19 01:52:46 +00:00
|
|
|
"reflect"
|
2017-07-03 18:15:59 +00:00
|
|
|
"sort"
|
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
|
|
|
|
2022-06-15 09:19:17 +00:00
|
|
|
"github.com/ipfs-cluster/ipfs-cluster/api"
|
2019-06-14 10:41:11 +00:00
|
|
|
|
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 {
|
2022-03-19 01:52:46 +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
|
2022-03-19 01:52:46 +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
|
2022-03-19 01:52:46 +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{}) {
|
2022-03-22 09:56:16 +00:00
|
|
|
print := func(o interface{}) {
|
|
|
|
j, err := json.MarshalIndent(o, "", " ")
|
|
|
|
checkErr("generating json output", err)
|
|
|
|
fmt.Printf("%s\n", j)
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
switch r := obj.(type) {
|
|
|
|
case chan api.Pin:
|
|
|
|
for o := range r {
|
2022-03-22 09:56:16 +00:00
|
|
|
print(o)
|
|
|
|
}
|
|
|
|
case chan api.GlobalPinInfo:
|
|
|
|
for o := range r {
|
|
|
|
print(o)
|
2022-03-19 01:52:46 +00:00
|
|
|
}
|
2022-03-23 00:27:57 +00:00
|
|
|
case chan api.ID:
|
|
|
|
for o := range r {
|
|
|
|
print(o)
|
|
|
|
}
|
2022-03-19 01:52:46 +00:00
|
|
|
default:
|
2022-03-22 09:56:16 +00:00
|
|
|
print(obj)
|
2022-03-19 01:52:46 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.ID:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintID(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.GlobalPinInfo:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintGPInfo(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.Pin:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintPin(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.AddedOutput:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintAddedOutput(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case addedOutputQuiet:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintAddedOutputQuiet(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.Version:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintVersion(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.Error:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintError(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.Metric:
|
2020-04-14 17:58:00 +00:00
|
|
|
textFormatPrintMetric(r)
|
2022-03-19 01:52:46 +00:00
|
|
|
case api.Alert:
|
2021-01-13 21:09:21 +00:00
|
|
|
textFormatPrintAlert(r)
|
2022-03-23 00:27:57 +00:00
|
|
|
case chan api.ID:
|
|
|
|
for item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2022-03-22 09:56:16 +00:00
|
|
|
case chan api.GlobalPinInfo:
|
|
|
|
for item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2022-03-19 01:52:46 +00:00
|
|
|
case chan api.Pin:
|
|
|
|
for item := range r {
|
2017-12-05 20:44:22 +00:00
|
|
|
textFormatObject(item)
|
|
|
|
}
|
2022-03-19 01:52:46 +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)
|
|
|
|
}
|
2022-03-19 01:52:46 +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)
|
|
|
|
}
|
2022-03-19 01:52:46 +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)
|
|
|
|
}
|
2022-03-19 01:52:46 +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)
|
|
|
|
}
|
2022-03-19 01:52:46 +00:00
|
|
|
case []api.Alert:
|
2021-01-13 21:09:21 +00:00
|
|
|
for _, item := range r {
|
|
|
|
textFormatObject(item)
|
2019-12-12 18:51:28 +00:00
|
|
|
}
|
2017-12-05 20:44:22 +00:00
|
|
|
default:
|
2022-03-19 01:52:46 +00:00
|
|
|
checkErr("", errors.New("unsupported type returned"+reflect.TypeOf(r).String()))
|
2017-12-05 20:44:22 +00:00
|
|
|
}
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +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))
|
Issue #162: Rework configuration format
The following commit reimplements ipfs-cluster configuration under
the following premises:
* Each component is initialized with a configuration object
defined by its module
* Each component decides how the JSON representation of its
configuration looks like
* Each component parses and validates its own configuration
* Each component exposes its own defaults
* Component configurations are make the sections of a
central JSON configuration file (which replaces the current
JSON format)
* Component configurations implement a common interface
(config.ComponentConfig) with a set of common operations
* The central configuration file is managed by a
config.ConfigManager which:
* Registers ComponentConfigs
* Assigns the correspondent sections from the JSON file to each
component and delegates the parsing
* Delegates the JSON generation for each section
* Can be notified when the configuration is updated and must be
saved to disk
The new service.json would then look as follows:
```json
{
"cluster": {
"id": "QmTVW8NoRxC5wBhV7WtAYtRn7itipEESfozWN5KmXUQnk2",
"private_key": "<...>",
"secret": "00224102ae6aaf94f2606abf69a0e278251ecc1d64815b617ff19d6d2841f786",
"peers": [],
"bootstrap": [],
"leave_on_shutdown": false,
"listen_multiaddress": "/ip4/0.0.0.0/tcp/9096",
"state_sync_interval": "1m0s",
"ipfs_sync_interval": "2m10s",
"replication_factor": -1,
"monitor_ping_interval": "15s"
},
"consensus": {
"raft": {
"heartbeat_timeout": "1s",
"election_timeout": "1s",
"commit_timeout": "50ms",
"max_append_entries": 64,
"trailing_logs": 10240,
"snapshot_interval": "2m0s",
"snapshot_threshold": 8192,
"leader_lease_timeout": "500ms"
}
},
"api": {
"restapi": {
"listen_multiaddress": "/ip4/127.0.0.1/tcp/9094",
"read_timeout": "30s",
"read_header_timeout": "5s",
"write_timeout": "1m0s",
"idle_timeout": "2m0s"
}
},
"ipfs_connector": {
"ipfshttp": {
"proxy_listen_multiaddress": "/ip4/127.0.0.1/tcp/9095",
"node_multiaddress": "/ip4/127.0.0.1/tcp/5001",
"connect_swarms_delay": "7s",
"proxy_read_timeout": "10m0s",
"proxy_read_header_timeout": "5s",
"proxy_write_timeout": "10m0s",
"proxy_idle_timeout": "1m0s"
}
},
"monitor": {
"monbasic": {
"check_interval": "15s"
}
},
"informer": {
"disk": {
"metric_ttl": "30s",
"metric_type": "freespace"
},
"numpin": {
"metric_ttl": "10s"
}
}
}
```
This new format aims to be easily extensible per component. As such,
it already surfaces quite a few new options which were hardcoded
before.
Additionally, since Go API have changed, some redundant methods have been
removed and small refactoring has happened to take advantage of the new
way.
License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2017-10-11 18:23:03 +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()
|
2021-10-27 09:04:47 +00:00
|
|
|
fmt.Fprintf(&b, " | %s", txt)
|
2021-10-29 15:25:05 +00:00
|
|
|
fmt.Fprintf(&b, " | Attempts: %d", v.AttemptCount)
|
|
|
|
fmt.Fprintf(&b, " | Priority: %t", v.PriorityPin)
|
2021-10-27 09:04:47 +00:00
|
|
|
fmt.Fprintf(&b, "\n")
|
2017-11-30 00:53:31 +00:00
|
|
|
}
|
2020-05-15 00:13:43 +00:00
|
|
|
fmt.Print(b.String())
|
2017-11-30 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
func textFormatPrintVersion(obj api.Version) {
|
2017-02-08 18:52:34 +00:00
|
|
|
fmt.Println(obj.Version)
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2022-03-19 01:52:46 +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
|
|
|
|
2021-03-23 23:47:15 +00:00
|
|
|
if obj.IsPinEverywhere() {
|
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
|
|
|
}
|
2021-10-20 13:23:58 +00:00
|
|
|
expireAt := "∞"
|
2020-04-11 08:28:35 +00:00
|
|
|
if !obj.ExpireAt.IsZero() {
|
2022-06-20 19:35:52 +00:00
|
|
|
expireAt = obj.ExpireAt.Format("2006-01-02 15:04:05")
|
2019-08-19 04:52:50 +00:00
|
|
|
}
|
2021-10-20 13:23:58 +00:00
|
|
|
fmt.Printf(" | Exp: %s", expireAt)
|
|
|
|
|
|
|
|
added := "unknown"
|
|
|
|
if !obj.Timestamp.IsZero() {
|
|
|
|
added = obj.Timestamp.Format("2006-01-02 15:04:05")
|
|
|
|
}
|
|
|
|
fmt.Printf(" | Added: %s\n", added)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2017-03-28 15:48:26 +00:00
|
|
|
|
2022-03-19 01:52:46 +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
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
func textFormatPrintAddedOutputQuiet(obj addedOutputQuiet) {
|
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
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
func textFormatPrintMetric(obj api.Metric) {
|
2021-12-08 11:01:59 +00:00
|
|
|
v := obj.Value
|
|
|
|
if obj.Name == "freespace" && obj.Weight > 0 {
|
|
|
|
v = humanize.Bytes(uint64(obj.Weight))
|
2019-10-24 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 14:57:17 +00:00
|
|
|
fmt.Printf("%s | %s: %s | Expires in: %s\n", obj.Peer, obj.Name, v, humanize.Time(time.Unix(0, obj.Expire)))
|
2018-10-07 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
func textFormatPrintAlert(obj api.Alert) {
|
2021-01-13 21:09:21 +00:00
|
|
|
fmt.Printf("%s: %s. Expired at: %s. Triggered at: %s\n",
|
|
|
|
obj.Peer,
|
|
|
|
obj.Name,
|
|
|
|
humanize.Time(time.Unix(0, obj.Expire)),
|
|
|
|
humanize.Time(obj.TriggeredAt),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +00:00
|
|
|
func textFormatPrintGlobalRepoGC(obj api.GlobalRepoGC) {
|
2019-10-22 05:40:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:52:46 +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
|
|
|
}
|