refactor(cmds): use new cmds lib in log
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 26, 2018 at 23:54 UTC
35959d623bd8111c9da5bee718ed9faabf3d5ac9
4 files changed
+27
-46
core/commands/log.go
+23
-40
@@ -1,16 +1,13 @@
1
package commands
2
3
import (
4
- "bytes"
4
"fmt"
5
"io"
6
8
- cmds "github.com/ipfs/go-ipfs/commands"
9
- e "github.com/ipfs/go-ipfs/core/commands/e"
10
-
7
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
8
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
9
lwriter "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log/writer"
13
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
10
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
11
)
12
13
// Golang os.Args overrides * and replaces the character argument with
@@ -52,9 +49,8 @@ the event log.
49
One of: debug, info, warning, error, critical.
50
`),
51
},
55
- Run: func(req cmds.Request, res cmds.Response) {
56
-
57
- args := req.Arguments()
52
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
53
+ args := req.Arguments
54
subsystem, level := args[0], args[1]
55
56
if subsystem == logAllKeyword {
@@ -62,16 +58,18 @@ the event log.
58
}
59
60
if err := logging.SetLogLevel(subsystem, level); err != nil {
65
- res.SetError(err, cmdkit.ErrNormal)
66
- return
61
+ return err
62
}
63
64
s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level)
65
log.Info(s)
71
- res.SetOutput(&MessageOutput{s})
66
+ return res.Emit(&MessageOutput{s})
67
},
73
- Marshalers: cmds.MarshalerMap{
74
- cmds.Text: MessageTextMarshaler,
68
+ Encoders: cmds.EncoderMap{
69
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *MessageOutput) error {
70
+ fmt.Fprint(w, out.Message)
71
+ return nil
72
+ }),
73
},
74
Type: MessageOutput{},
75
}
@@ -84,11 +82,16 @@ var logLsCmd = &cmds.Command{
82
subsystems of a running daemon.
83
`,
84
},
87
- Run: func(req cmds.Request, res cmds.Response) {
88
- res.SetOutput(&stringList{logging.GetSubsystems()})
85
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
86
+ return res.Emit(&stringList{logging.GetSubsystems()})
87
},
90
- Marshalers: cmds.MarshalerMap{
91
- cmds.Text: stringListMarshaler,
88
+ Encoders: cmds.EncoderMap{
89
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *stringList) error {
90
+ for _, s := range list.Strings {
91
+ fmt.Fprintln(w, s)
92
+ }
93
+ return nil
94
+ }),
95
},
96
Type: stringList{},
97
}
@@ -101,34 +104,14 @@ Outputs event log messages (not other log messages) as they are generated.
104
`,
105
},
106
104
- Run: func(req cmds.Request, res cmds.Response) {
105
- ctx := req.Context()
107
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
108
+ ctx := req.Context
109
r, w := io.Pipe()
110
go func() {
111
defer w.Close()
112
<-ctx.Done()
113
}()
114
lwriter.WriterGroup.AddWriter(w)
112
- res.SetOutput(r)
115
+ return res.Emit(r)
116
},
117
}
115
-
116
-func stringListMarshaler(res cmds.Response) (io.Reader, error) {
117
- v, err := unwrapOutput(res.Output())
118
- if err != nil {
119
- return nil, err
120
- }
121
-
122
- list, ok := v.(*stringList)
123
- if !ok {
124
- return nil, e.TypeErr(list, v)
125
- }
126
-
127
- buf := new(bytes.Buffer)
128
- for _, s := range list.Strings {
129
- buf.WriteString(s)
130
- buf.WriteString("\n")
131
- }
132
-
133
- return buf, nil
134
-}
core/commands/mount_nofuse.go
+2
-3
@@ -3,9 +3,8 @@
3
package commands
4
5
import (
6
- cmds "github.com/ipfs/go-ipfs/commands"
7
-
8
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
6
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
7
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
8
)
9
10
var MountCmd = &cmds.Command{
core/commands/mount_windows.go
+1
-2
@@ -3,8 +3,7 @@ package commands
3
import (
4
"errors"
5
6
- cmds "github.com/ipfs/go-ipfs/commands"
7
-
6
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
7
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
8
)
9
core/commands/root.go
+1
-1
@@ -130,7 +130,7 @@ var rootSubcommands = map[string]*cmds.Command{
130
"dns": DNSCmd,
131
"id": IDCmd,
132
"key": KeyCmd,
133
- "log": lgc.NewCommand(LogCmd),
133
+ "log": LogCmd,
134
"ls": lgc.NewCommand(LsCmd),
135
"mount": MountCmd,
136
"name": name.NameCmd,