mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 20:37:53 +08:00
refactor(cmds): use new cmds lib in diag
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
This commit is contained in:
parent
a8dd21a59b
commit
b2badfbb06
@ -1,17 +1,15 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
||||
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
oldcmds "github.com/ipfs/go-ipfs/commands"
|
||||
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -25,8 +23,9 @@ var ActiveReqsCmd = &cmds.Command{
|
||||
Lists running and recently run commands.
|
||||
`,
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
res.SetOutput(req.InvocContext().ReqLog.Report())
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
ctx := env.(*oldcmds.Context)
|
||||
return res.Emit(ctx.ReqLog.Report())
|
||||
},
|
||||
Options: []cmdkit.Option{
|
||||
cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
|
||||
@ -35,38 +34,27 @@ Lists running and recently run commands.
|
||||
"clear": clearInactiveCmd,
|
||||
"set-time": setRequestClearCmd,
|
||||
},
|
||||
Marshalers: map[cmds.EncodingType]cmds.Marshaler{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
v, err := unwrapOutput(res.Output())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Encoders: cmds.EncoderMap {
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
|
||||
verbose, _ := req.Options[verboseOptionName].(bool)
|
||||
|
||||
out, ok := v.(*[]*cmds.ReqLogEntry)
|
||||
if !ok {
|
||||
return nil, e.TypeErr(out, v)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
verbose, _, _ := res.Request().Option(verboseOptionName).Bool()
|
||||
|
||||
w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
|
||||
tw := tabwriter.NewWriter(w, 4, 4, 2, ' ', 0)
|
||||
if verbose {
|
||||
fmt.Fprint(w, "ID\t")
|
||||
fmt.Fprint(tw, "ID\t")
|
||||
}
|
||||
fmt.Fprint(w, "Command\t")
|
||||
fmt.Fprint(tw, "Command\t")
|
||||
if verbose {
|
||||
fmt.Fprint(w, "Arguments\tOptions\t")
|
||||
fmt.Fprint(tw, "Arguments\tOptions\t")
|
||||
}
|
||||
fmt.Fprintln(w, "Active\tStartTime\tRunTime")
|
||||
fmt.Fprintln(tw, "Active\tStartTime\tRunTime")
|
||||
|
||||
for _, req := range *out {
|
||||
if verbose {
|
||||
fmt.Fprintf(w, "%d\t", req.ID)
|
||||
fmt.Fprintf(tw, "%d\t", req.ID)
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t", req.Command)
|
||||
fmt.Fprintf(tw, "%s\t", req.Command)
|
||||
if verbose {
|
||||
fmt.Fprintf(w, "%v\t[", req.Args)
|
||||
fmt.Fprintf(tw, "%v\t[", req.Args)
|
||||
var keys []string
|
||||
for k := range req.Options {
|
||||
keys = append(keys, k)
|
||||
@ -74,9 +62,9 @@ Lists running and recently run commands.
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
fmt.Fprintf(w, "%s=%v,", k, req.Options[k])
|
||||
fmt.Fprintf(tw, "%s=%v,", k, req.Options[k])
|
||||
}
|
||||
fmt.Fprintf(w, "]\t")
|
||||
fmt.Fprintf(tw, "]\t")
|
||||
}
|
||||
|
||||
var live time.Duration
|
||||
@ -88,10 +76,8 @@ Lists running and recently run commands.
|
||||
t := req.StartTime.Format(time.Stamp)
|
||||
fmt.Fprintf(w, "%t\t%s\t%s\n", req.Active, t, live)
|
||||
}
|
||||
w.Flush()
|
||||
|
||||
return buf, nil
|
||||
},
|
||||
return tw.Flush()
|
||||
}),
|
||||
},
|
||||
Type: []*cmds.ReqLogEntry{},
|
||||
}
|
||||
@ -100,8 +86,10 @@ var clearInactiveCmd = &cmds.Command{
|
||||
Helptext: cmdkit.HelpText{
|
||||
Tagline: "Clear inactive requests from the log.",
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
req.InvocContext().ReqLog.ClearInactive()
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
ctx := env.(*oldcmds.Context)
|
||||
ctx.ReqLog.ClearInactive()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -112,13 +100,14 @@ var setRequestClearCmd = &cmds.Command{
|
||||
Arguments: []cmdkit.Argument{
|
||||
cmdkit.StringArg("time", true, false, "Time to keep inactive requests in log."),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
tval, err := time.ParseDuration(req.Arguments()[0])
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
tval, err := time.ParseDuration(req.Arguments[0])
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
ctx := env.(*oldcmds.Context)
|
||||
ctx.ReqLog.SetKeepTime(tval)
|
||||
|
||||
req.InvocContext().ReqLog.SetKeepTime(tval)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
var DiagCmd = &cmds.Command{
|
||||
|
||||
@ -126,7 +126,7 @@ var rootSubcommands = map[string]*cmds.Command{
|
||||
"config": lgc.NewCommand(ConfigCmd),
|
||||
"dag": dag.DagCmd,
|
||||
"dht": DhtCmd,
|
||||
"diag": lgc.NewCommand(DiagCmd),
|
||||
"diag": DiagCmd,
|
||||
"dns": DNSCmd,
|
||||
"id": IDCmd,
|
||||
"key": KeyCmd,
|
||||
|
||||
@ -5,9 +5,10 @@ import (
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
||||
version "github.com/ipfs/go-ipfs"
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
|
||||
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
|
||||
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
|
||||
sysi "gx/ipfs/QmZRjKbHa6DenStpQJFiaPcEwkZqrx7TH6xTf342LDU3qM/go-sysinfo"
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
@ -20,46 +21,40 @@ var sysDiagCmd = &cmds.Command{
|
||||
Prints out information about your computer to aid in easier debugging.
|
||||
`,
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
info := make(map[string]interface{})
|
||||
err := runtimeInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = envVarInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = diskSpaceInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = memInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
node, err := req.InvocContext().GetNode()
|
||||
nd, err := cmdenv.GetNode(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = netInfo(node.OnlineMode(), info)
|
||||
err = netInfo(nd.OnlineMode(), info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
info["ipfs_version"] = version.CurrentVersionNumber
|
||||
info["ipfs_commit"] = version.CurrentCommit
|
||||
res.SetOutput(info)
|
||||
return res.Emit(info)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user