refactor(cmds): use new cmds lib in diag
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 26, 2018 at 22:37 UTC
b2badfbb068867980fc389ae70f80c8fd9da2648
4 files changed
+46
-63
core/commands/active.go
+31
-42
@@ -1,17 +1,15 @@
1
package commands
2
3
import (
4
- "bytes"
4
"fmt"
5
"io"
6
"sort"
7
"text/tabwriter"
8
"time"
9
11
- cmds "github.com/ipfs/go-ipfs/commands"
12
- e "github.com/ipfs/go-ipfs/core/commands/e"
13
-
14
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
10
+ oldcmds "github.com/ipfs/go-ipfs/commands"
11
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
12
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13
)
14
15
const (
@@ -25,8 +23,9 @@ var ActiveReqsCmd = &cmds.Command{
23
Lists running and recently run commands.
24
`,
25
},
28
- Run: func(req cmds.Request, res cmds.Response) {
29
- res.SetOutput(req.InvocContext().ReqLog.Report())
26
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
27
+ ctx := env.(*oldcmds.Context)
28
+ return res.Emit(ctx.ReqLog.Report())
29
},
30
Options: []cmdkit.Option{
31
cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
@@ -35,38 +34,27 @@ Lists running and recently run commands.
34
"clear": clearInactiveCmd,
35
"set-time": setRequestClearCmd,
36
},
38
- Marshalers: map[cmds.EncodingType]cmds.Marshaler{
39
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
40
- v, err := unwrapOutput(res.Output())
41
- if err != nil {
42
- return nil, err
43
- }
44
-
45
- out, ok := v.(*[]*cmds.ReqLogEntry)
46
- if !ok {
47
- return nil, e.TypeErr(out, v)
48
- }
49
- buf := new(bytes.Buffer)
50
-
51
- verbose, _, _ := res.Request().Option(verboseOptionName).Bool()
37
+ Encoders: cmds.EncoderMap {
38
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
39
+ verbose, _ := req.Options[verboseOptionName].(bool)
40
53
- w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
41
+ tw := tabwriter.NewWriter(w, 4, 4, 2, ' ', 0)
42
if verbose {
55
- fmt.Fprint(w, "ID\t")
43
+ fmt.Fprint(tw, "ID\t")
44
}
57
- fmt.Fprint(w, "Command\t")
45
+ fmt.Fprint(tw, "Command\t")
46
if verbose {
59
- fmt.Fprint(w, "Arguments\tOptions\t")
47
+ fmt.Fprint(tw, "Arguments\tOptions\t")
48
}
61
- fmt.Fprintln(w, "Active\tStartTime\tRunTime")
49
+ fmt.Fprintln(tw, "Active\tStartTime\tRunTime")
50
51
for _, req := range *out {
52
if verbose {
65
- fmt.Fprintf(w, "%d\t", req.ID)
53
+ fmt.Fprintf(tw, "%d\t", req.ID)
54
}
67
- fmt.Fprintf(w, "%s\t", req.Command)
55
+ fmt.Fprintf(tw, "%s\t", req.Command)
56
if verbose {
69
- fmt.Fprintf(w, "%v\t[", req.Args)
57
+ fmt.Fprintf(tw, "%v\t[", req.Args)
58
var keys []string
59
for k := range req.Options {
60
keys = append(keys, k)
@@ -74,9 +62,9 @@ Lists running and recently run commands.
62
sort.Strings(keys)
63
64
for _, k := range keys {
77
- fmt.Fprintf(w, "%s=%v,", k, req.Options[k])
65
+ fmt.Fprintf(tw, "%s=%v,", k, req.Options[k])
66
}
79
- fmt.Fprintf(w, "]\t")
67
+ fmt.Fprintf(tw, "]\t")
68
}
69
70
var live time.Duration
@@ -88,10 +76,8 @@ Lists running and recently run commands.
76
t := req.StartTime.Format(time.Stamp)
77
fmt.Fprintf(w, "%t\t%s\t%s\n", req.Active, t, live)
78
}
91
- w.Flush()
92
-
93
- return buf, nil
94
- },
79
+ return tw.Flush()
80
+ }),
81
},
82
Type: []*cmds.ReqLogEntry{},
83
}
@@ -100,8 +86,10 @@ var clearInactiveCmd = &cmds.Command{
86
Helptext: cmdkit.HelpText{
87
Tagline: "Clear inactive requests from the log.",
88
},
103
- Run: func(req cmds.Request, res cmds.Response) {
104
- req.InvocContext().ReqLog.ClearInactive()
89
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
90
+ ctx := env.(*oldcmds.Context)
91
+ ctx.ReqLog.ClearInactive()
92
+ return nil
93
},
94
}
95
@@ -112,13 +100,14 @@ var setRequestClearCmd = &cmds.Command{
100
Arguments: []cmdkit.Argument{
101
cmdkit.StringArg("time", true, false, "Time to keep inactive requests in log."),
102
},
115
- Run: func(req cmds.Request, res cmds.Response) {
116
- tval, err := time.ParseDuration(req.Arguments()[0])
103
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
104
+ tval, err := time.ParseDuration(req.Arguments[0])
105
if err != nil {
118
- res.SetError(err, cmdkit.ErrNormal)
119
- return
106
+ return err
107
}
108
+ ctx := env.(*oldcmds.Context)
109
+ ctx.ReqLog.SetKeepTime(tval)
110
122
- req.InvocContext().ReqLog.SetKeepTime(tval)
111
+ return nil
112
},
113
}
core/commands/diag.go
+2
-3
@@ -1,9 +1,8 @@
1
package commands
2
3
import (
4
- cmds "github.com/ipfs/go-ipfs/commands"
5
-
6
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
4
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
5
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
6
)
7
8
var DiagCmd = &cmds.Command{
core/commands/root.go
+1
-1
@@ -126,7 +126,7 @@ var rootSubcommands = map[string]*cmds.Command{
126
"config": lgc.NewCommand(ConfigCmd),
127
"dag": dag.DagCmd,
128
"dht": DhtCmd,
129
- "diag": lgc.NewCommand(DiagCmd),
129
+ "diag": DiagCmd,
130
"dns": DNSCmd,
131
"id": IDCmd,
132
"key": KeyCmd,
core/commands/sysdiag.go
+12
-17
@@ -5,9 +5,10 @@ import (
5
"path"
6
"runtime"
7
8
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
version "github.com/ipfs/go-ipfs"
9
- cmds "github.com/ipfs/go-ipfs/commands"
10
11
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
12
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
13
sysi "gx/ipfs/QmZRjKbHa6DenStpQJFiaPcEwkZqrx7TH6xTf342LDU3qM/go-sysinfo"
14
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
@@ -20,46 +21,40 @@ var sysDiagCmd = &cmds.Command{
21
Prints out information about your computer to aid in easier debugging.
22
`,
23
},
23
- Run: func(req cmds.Request, res cmds.Response) {
24
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
25
info := make(map[string]interface{})
26
err := runtimeInfo(info)
27
if err != nil {
27
- res.SetError(err, cmdkit.ErrNormal)
28
- return
28
+ return err
29
}
30
31
err = envVarInfo(info)
32
if err != nil {
33
- res.SetError(err, cmdkit.ErrNormal)
34
- return
33
+ return err
34
}
35
36
err = diskSpaceInfo(info)
37
if err != nil {
39
- res.SetError(err, cmdkit.ErrNormal)
40
- return
38
+ return err
39
}
40
41
err = memInfo(info)
42
if err != nil {
45
- res.SetError(err, cmdkit.ErrNormal)
46
- return
43
+ return err
44
}
48
- node, err := req.InvocContext().GetNode()
45
+ nd, err := cmdenv.GetNode(env)
46
if err != nil {
50
- res.SetError(err, cmdkit.ErrNormal)
51
- return
47
+ return err
48
}
49
54
- err = netInfo(node.OnlineMode(), info)
50
+ err = netInfo(nd.OnlineMode(), info)
51
if err != nil {
56
- res.SetError(err, cmdkit.ErrNormal)
57
- return
52
+ return err
53
}
54
55
info["ipfs_version"] = version.CurrentVersionNumber
56
info["ipfs_commit"] = version.CurrentCommit
62
- res.SetOutput(info)
57
+ return res.Emit(info)
58
},
59
}
60