From b9524622cbbd444bb551ef0d96916fe840cbb12c Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Thu, 19 Aug 2021 11:40:55 -0700 Subject: [PATCH] Use DisplayCLI instead of PostRun for `stat bw` command Distinguish polled/non polled output cases in text-specific DisplayCLI output mode. Exposes ability to request json and xml output, according to global `--encoding` flag. --- core/commands/stat.go | 84 ++++++++++++++++++++++++++----------------- go.mod | 4 ++- go.sum | 2 ++ 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/core/commands/stat.go b/core/commands/stat.go index e38b5b31b34..0e8c14285b9 100644 --- a/core/commands/stat.go +++ b/core/commands/stat.go @@ -3,7 +3,6 @@ package commands import ( "fmt" "io" - "os" "time" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" @@ -154,42 +153,61 @@ Example: } }, Type: metrics.Stats{}, - PostRun: cmds.PostRunMap{ - cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error { - polling, _ := res.Request().Options[statPollOptionName].(bool) + DisplayCLI: func(res cmds.Response, stdout, stderr io.Writer) error { + polling, _ := res.Request().Options[statPollOptionName].(bool) - if polling { - fmt.Fprintln(os.Stdout, "Total Up Total Down Rate Up Rate Down") - } - for { - v, err := res.Next() - if err != nil { - if err == io.EOF { - return nil - } - return err - } + output := printStats - bs := v.(*metrics.Stats) - - if !polling { - printStats(os.Stdout, bs) - return nil - } + if polling { + output = pollStats + } - fmt.Fprintf(os.Stdout, "%8s ", humanize.Bytes(uint64(bs.TotalOut))) - fmt.Fprintf(os.Stdout, "%8s ", humanize.Bytes(uint64(bs.TotalIn))) - fmt.Fprintf(os.Stdout, "%8s/s ", humanize.Bytes(uint64(bs.RateOut))) - fmt.Fprintf(os.Stdout, "%8s/s \r", humanize.Bytes(uint64(bs.RateIn))) - } - }, + return output(res, stdout, stderr) }, } -func printStats(out io.Writer, bs *metrics.Stats) { - fmt.Fprintln(out, "Bandwidth") - fmt.Fprintf(out, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn))) - fmt.Fprintf(out, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut))) - fmt.Fprintf(out, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn))) - fmt.Fprintf(out, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut))) +// Non-polled output + +func printStats(res cmds.Response, stdout io.Writer, stderr io.Writer) error { + + v, err := res.Next() + if err != nil { + if err == io.EOF { + return nil + } + return err + } + + bs := v.(*metrics.Stats) + + fmt.Fprintln(stdout, "Bandwidth") + fmt.Fprintf(stdout, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn))) + fmt.Fprintf(stdout, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut))) + fmt.Fprintf(stdout, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn))) + fmt.Fprintf(stdout, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut))) + + return nil +} + +// Polled output + +func pollStats(res cmds.Response, stdout io.Writer, stderr io.Writer) error { + fmt.Fprintln(stdout, "Total Up Total Down Rate Up Rate Down") + + for { + v, err := res.Next() + if err != nil { + if err == io.EOF { + return nil + } + return err + } + + bs := v.(*metrics.Stats) + + fmt.Fprintf(stdout, "%8s ", humanize.Bytes(uint64(bs.TotalOut))) + fmt.Fprintf(stdout, "%8s ", humanize.Bytes(uint64(bs.TotalIn))) + fmt.Fprintf(stdout, "%8s/s ", humanize.Bytes(uint64(bs.RateOut))) + fmt.Fprintf(stdout, "%8s/s \r", humanize.Bytes(uint64(bs.RateIn))) + } } diff --git a/go.mod b/go.mod index a1e8721ef62..bc18b5379a5 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/ipfs/go-graphsync v0.8.0 github.com/ipfs/go-ipfs-blockstore v0.1.6 github.com/ipfs/go-ipfs-chunker v0.0.5 - github.com/ipfs/go-ipfs-cmds v0.6.0 github.com/ipfs/go-ipfs-config v0.14.0 github.com/ipfs/go-ipfs-exchange-interface v0.0.1 github.com/ipfs/go-ipfs-exchange-offline v0.0.1 @@ -59,6 +58,7 @@ require ( github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c github.com/jbenet/go-temp-err-catcher v0.1.0 github.com/jbenet/goprocess v0.1.4 + github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12 // indirect github.com/libp2p/go-doh-resolver v0.3.1 github.com/libp2p/go-libp2p v0.14.4 github.com/libp2p/go-libp2p-circuit v0.4.0 @@ -108,3 +108,5 @@ require ( ) go 1.15 + +// replace github.com/ipfs/go-ipfs-cmds => ../go-ipfs-cmds diff --git a/go.sum b/go.sum index a17da770743..7518f032443 100644 --- a/go.sum +++ b/go.sum @@ -537,6 +537,8 @@ github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsj github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12 h1:Fpq4JqaVM4/7R+iHwT5vWdsxHGjZabzqBuWkAyqTcc8= +github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12/go.mod h1:g6wLI6iRb7guDxPSbVzhAHdhsIhn7ggvyKc0by6SR+U= github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=