Skip to content

Commit

Permalink
refactor(cmds): use new cmds lib in diag
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <[email protected]>
  • Loading branch information
overbool committed Oct 26, 2018
1 parent 26013ed commit 9ae375c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 63 deletions.
73 changes: 31 additions & 42 deletions core/commands/active.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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."),
Expand All @@ -35,48 +34,37 @@ 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
}

out, ok := v.(*[]*cmds.ReqLogEntry)
if !ok {
return nil, e.TypeErr(out, v)
}
buf := new(bytes.Buffer)

verbose, _, _ := res.Request().Option(verboseOptionName).Bool()
Encoders: cmds.EncoderMap {
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
verbose, _ := req.Options[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)
}
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
Expand All @@ -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{},
}
Expand All @@ -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
},
}

Expand All @@ -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
},
}
5 changes: 2 additions & 3 deletions core/commands/diag.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var rootSubcommands = map[string]*cmds.Command{
"config": lgc.NewCommand(ConfigCmd),
"dag": lgc.NewCommand(dag.DagCmd),
"dht": lgc.NewCommand(DhtCmd),
"diag": lgc.NewCommand(DiagCmd),
"diag": DiagCmd,
"dns": lgc.NewCommand(DNSCmd),
"id": IDCmd,
"key": KeyCmd,
Expand Down
29 changes: 12 additions & 17 deletions core/commands/sysdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
sysi "gx/ipfs/QmZRjKbHa6DenStpQJFiaPcEwkZqrx7TH6xTf342LDU3qM/go-sysinfo"
manet "gx/ipfs/Qmaabb1tJZ2CX5cp6MuuiGgns71NYoxdgQP6Xdid1dVceC/go-multiaddr-net"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Expand All @@ -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)
},
}

Expand Down

0 comments on commit 9ae375c

Please sign in to comment.