@cryptotaxi247 / kubo / commits / 204a53247

commands/refs: use new cmds

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Oct 27, 2018 at 20:55 UTC 204a53247c2b880ef378a1ecd43c20ab3b44026a
2 files changed +56 -62
core/commands/refs.go
+52 -58
@@ -4,16 +4,19 @@ import (
4 "bytes"
5 "context"
6 "errors"
7 + "fmt"
8 "io"
9 "strings"
10
10 - cmds "github.com/ipfs/go-ipfs/commands"
11 - "github.com/ipfs/go-ipfs/core"
11 + oldcmds "github.com/ipfs/go-ipfs/commands"
12 + core "github.com/ipfs/go-ipfs/core"
13 + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
14 e "github.com/ipfs/go-ipfs/core/commands/e"
15
16 cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
17 path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
18 ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
19 + cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
20 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
21 )
22
@@ -31,7 +34,7 @@ const (
34 )
35
36 // KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
34 -func KeyListTextMarshaler(res cmds.Response) (io.Reader, error) {
37 +func KeyListTextMarshaler(res oldcmds.Response) (io.Reader, error) {
38 out, err := unwrapOutput(res.Output())
39 if err != nil {
40 return nil, err
@@ -74,65 +77,37 @@ NOTE: List all references recursively by using the flag '-r'.
77 cmdkit.BoolOption(refsRecursiveOptionName, "r", "Recursively list links of child nodes."),
78 cmdkit.IntOption(refsMaxDepthOptionName, "Only for recursive refs, limits fetch and listing to the given depth").WithDefault(-1),
79 },
77 - Run: func(req cmds.Request, res cmds.Response) {
78 - ctx := req.Context()
79 - n, err := req.InvocContext().GetNode()
80 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
81 + ctx := req.Context
82 + n, err := cmdenv.GetNode(env)
83 if err != nil {
81 - res.SetError(err, cmdkit.ErrNormal)
82 - return
84 + return err
85 }
86
85 - unique, _, err := req.Option(refsUniqueOptionName).Bool()
86 - if err != nil {
87 - res.SetError(err, cmdkit.ErrNormal)
88 - return
89 - }
90 -
91 - recursive, _, err := req.Option(refsRecursiveOptionName).Bool()
92 - if err != nil {
93 - res.SetError(err, cmdkit.ErrNormal)
94 - return
95 - }
96 -
97 - maxDepth, _, err := req.Option(refsMaxDepthOptionName).Int()
98 - if err != nil {
99 - res.SetError(err, cmdkit.ErrNormal)
100 - return
101 - }
87 + unique, _ := req.Options[refsUniqueOptionName].(bool)
88 + recursive, _ := req.Options[refsRecursiveOptionName].(bool)
89 + maxDepth, _ := req.Options[refsMaxDepthOptionName].(int)
90 + edges, _ := req.Options[refsEdgesOptionName].(bool)
91 + format, _ := req.Options[refsFormatOptionName].(string)
92
93 if !recursive {
94 maxDepth = 1 // write only direct refs
95 }
96
107 - format, _, err := req.Option(refsFormatOptionName).String()
108 - if err != nil {
109 - res.SetError(err, cmdkit.ErrNormal)
110 - return
111 - }
112 -
113 - edges, _, err := req.Option(refsEdgesOptionName).Bool()
114 - if err != nil {
115 - res.SetError(err, cmdkit.ErrNormal)
116 - return
117 - }
97 if edges {
98 if format != "<dst>" {
120 - res.SetError(errors.New("using format argument with edges is not allowed"),
121 - cmdkit.ErrClient)
122 - return
99 + return errors.New("using format argument with edges is not allowed")
100 }
101
102 format = "<src> -> <dst>"
103 }
104
128 - objs, err := objectsForPaths(ctx, n, req.Arguments())
105 + objs, err := objectsForPaths(ctx, n, req.Arguments)
106 if err != nil {
130 - res.SetError(err, cmdkit.ErrNormal)
131 - return
107 + return err
108 }
109
110 out := make(chan interface{})
135 - res.SetOutput((<-chan interface{})(out))
111
112 go func() {
113 defer close(out)
@@ -156,9 +131,20 @@ NOTE: List all references recursively by using the flag '-r'.
131 }
132 }
133 }()
134 +
135 + return res.Emit(out)
136 + },
137 + Encoders: cmds.EncoderMap{
138 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error {
139 + if out.Err != "" {
140 + return fmt.Errorf(out.Err)
141 + }
142 + fmt.Fprintln(w, out.Ref)
143 +
144 + return nil
145 + }),
146 },
160 - Marshalers: refsMarshallerMap,
161 - Type: RefWrapper{},
147 + Type: RefWrapper{},
148 }
149
150 var RefsLocalCmd = &cmds.Command{
@@ -169,23 +155,20 @@ Displays the hashes of all local objects.
155 `,
156 },
157
172 - Run: func(req cmds.Request, res cmds.Response) {
173 - ctx := req.Context()
174 - n, err := req.InvocContext().GetNode()
158 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
159 + ctx := req.Context
160 + n, err := cmdenv.GetNode(env)
161 if err != nil {
176 - res.SetError(err, cmdkit.ErrNormal)
177 - return
162 + return err
163 }
164
165 // todo: make async
166 allKeys, err := n.Blockstore.AllKeysChan(ctx)
167 if err != nil {
183 - res.SetError(err, cmdkit.ErrNormal)
184 - return
168 + return err
169 }
170
171 out := make(chan interface{})
188 - res.SetOutput((<-chan interface{})(out))
172
173 go func() {
174 defer close(out)
@@ -193,18 +176,29 @@ Displays the hashes of all local objects.
176 for k := range allKeys {
177 select {
178 case out <- &RefWrapper{Ref: k.String()}:
196 - case <-req.Context().Done():
179 + case <-req.Context.Done():
180 return
181 }
182 }
183 }()
184 +
185 + return res.Emit(out)
186 + },
187 + Encoders: cmds.EncoderMap{
188 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error {
189 + if out.Err != "" {
190 + return fmt.Errorf(out.Err)
191 + }
192 + fmt.Fprintln(w, out.Ref)
193 +
194 + return nil
195 + }),
196 },
202 - Marshalers: refsMarshallerMap,
203 - Type: RefWrapper{},
197 + Type: RefWrapper{},
198 }
199
206 -var refsMarshallerMap = cmds.MarshalerMap{
207 - cmds.Text: func(res cmds.Response) (io.Reader, error) {
200 +var refsMarshallerMap = oldcmds.MarshalerMap{
201 + cmds.Text: func(res oldcmds.Response) (io.Reader, error) {
202 v, err := unwrapOutput(res.Output())
203 if err != nil {
204 return nil, err
core/commands/root.go
+4 -4
@@ -138,7 +138,7 @@ var rootSubcommands = map[string]*cmds.Command{
138 "pin": lgc.NewCommand(PinCmd),
139 "ping": PingCmd,
140 "p2p": P2PCmd,
141 - "refs": lgc.NewCommand(RefsCmd),
141 + "refs": RefsCmd,
142 "resolve": ResolveCmd,
143 "swarm": SwarmCmd,
144 "tar": TarCmd,
@@ -155,7 +155,7 @@ var RootRO = &cmds.Command{}
155
156 var CommandsDaemonROCmd = CommandsCmd(RootRO)
157
158 -var RefsROCmd = &oldcmds.Command{}
158 +var RefsROCmd = &cmds.Command{}
159
160 var rootROSubcommands = map[string]*cmds.Command{
161 "commands": CommandsDaemonROCmd,
@@ -198,12 +198,12 @@ func init() {
198
199 // sanitize readonly refs command
200 *RefsROCmd = *RefsCmd
201 - RefsROCmd.Subcommands = map[string]*oldcmds.Command{}
201 + RefsROCmd.Subcommands = map[string]*cmds.Command{}
202
203 // this was in the big map definition above before,
204 // but if we leave it there lgc.NewCommand will be executed
205 // before the value is updated (:/sanitize readonly refs command/)
206 - rootROSubcommands["refs"] = lgc.NewCommand(RefsROCmd)
206 + rootROSubcommands["refs"] = RefsROCmd
207
208 Root.Subcommands = rootSubcommands
209