Skip to content

Commit 4160eaa

Browse files
committed
refactor(cmds): use new cmds lib in tar, dns
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
1 parent 9611d5f commit 4160eaa

File tree

4 files changed

+49
-77
lines changed

4 files changed

+49
-77
lines changed

core/commands/dns.go

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package commands
22

33
import (
4+
"fmt"
45
"io"
5-
"strings"
66

7-
cmds "github.com/ipfs/go-ipfs/commands"
8-
e "github.com/ipfs/go-ipfs/core/commands/e"
97
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
108
namesys "github.com/ipfs/go-ipfs/namesys"
119
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
1210

13-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
11+
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
12+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1413
)
1514

1615
const (
@@ -61,41 +60,30 @@ The resolver can recursively resolve:
6160
Options: []cmdkit.Option{
6261
cmdkit.BoolOption(dnsRecursiveOptionName, "r", "Resolve until the result is not a DNS link."),
6362
},
64-
Run: func(req cmds.Request, res cmds.Response) {
65-
66-
recursive, _, _ := req.Option(dnsRecursiveOptionName).Bool()
67-
name := req.Arguments()[0]
63+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
64+
recursive, _ := req.Options[dnsRecursiveOptionName].(bool)
65+
name := req.Arguments[0]
6866
resolver := namesys.NewDNSResolver()
6967

7068
var ropts []nsopts.ResolveOpt
7169
if !recursive {
7270
ropts = append(ropts, nsopts.Depth(1))
7371
}
7472

75-
output, err := resolver.Resolve(req.Context(), name, ropts...)
73+
output, err := resolver.Resolve(req.Context, name, ropts...)
7674
if err == namesys.ErrResolveFailed {
77-
res.SetError(err, cmdkit.ErrNotFound)
78-
return
75+
return err
7976
}
8077
if err != nil {
81-
res.SetError(err, cmdkit.ErrNormal)
82-
return
78+
return err
8379
}
84-
res.SetOutput(&ncmd.ResolvedPath{Path: output})
80+
return res.Emit(&ncmd.ResolvedPath{Path: output})
8581
},
86-
Marshalers: cmds.MarshalerMap{
87-
cmds.Text: func(res cmds.Response) (io.Reader, error) {
88-
v, err := unwrapOutput(res.Output())
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
output, ok := v.(*ncmd.ResolvedPath)
94-
if !ok {
95-
return nil, e.TypeErr(output, v)
96-
}
97-
return strings.NewReader(output.Path.String() + "\n"), nil
98-
},
82+
Encoders: cmds.EncoderMap{
83+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error {
84+
fmt.Fprintln(w, out.Path.String())
85+
return nil
86+
}),
9987
},
10088
Type: ncmd.ResolvedPath{},
10189
}

core/commands/root.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ var rootSubcommands = map[string]*cmds.Command{
127127
"dag": lgc.NewCommand(dag.DagCmd),
128128
"dht": lgc.NewCommand(DhtCmd),
129129
"diag": lgc.NewCommand(DiagCmd),
130-
"dns": lgc.NewCommand(DNSCmd),
130+
"dns": DNSCmd,
131131
"id": IDCmd,
132132
"key": KeyCmd,
133133
"log": lgc.NewCommand(LogCmd),
@@ -141,7 +141,7 @@ var rootSubcommands = map[string]*cmds.Command{
141141
"refs": lgc.NewCommand(RefsCmd),
142142
"resolve": ResolveCmd,
143143
"swarm": SwarmCmd,
144-
"tar": lgc.NewCommand(TarCmd),
144+
"tar": TarCmd,
145145
"file": lgc.NewCommand(unixfs.UnixFSCmd),
146146
"update": lgc.NewCommand(ExternalBinary()),
147147
"urlstore": urlStoreCmd,
@@ -167,7 +167,7 @@ var rootROSubcommands = map[string]*cmds.Command{
167167
},
168168
},
169169
"get": GetCmd,
170-
"dns": lgc.NewCommand(DNSCmd),
170+
"dns": DNSCmd,
171171
"ls": lgc.NewCommand(LsCmd),
172172
"name": &cmds.Command{
173173
Subcommands: map[string]*cmds.Command{

core/commands/tar.go

+30-46
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package commands
22

33
import (
4+
"fmt"
45
"io"
5-
"strings"
66

7-
cmds "github.com/ipfs/go-ipfs/commands"
87
core "github.com/ipfs/go-ipfs/core"
9-
e "github.com/ipfs/go-ipfs/core/commands/e"
8+
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
109
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1110
tar "github.com/ipfs/go-ipfs/tar"
12-
path "gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
13-
dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
1411

15-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
12+
"gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
13+
dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
14+
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
15+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1616
)
1717

1818
var TarCmd = &cmds.Command{
@@ -38,47 +38,36 @@ represent it.
3838
Arguments: []cmdkit.Argument{
3939
cmdkit.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
4040
},
41-
Run: func(req cmds.Request, res cmds.Response) {
42-
nd, err := req.InvocContext().GetNode()
41+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
42+
nd, err := cmdenv.GetNode(env)
4343
if err != nil {
44-
res.SetError(err, cmdkit.ErrNormal)
45-
return
44+
return err
4645
}
4746

48-
fi, err := req.Files().NextFile()
47+
fi, err := req.Files.NextFile()
4948
if err != nil {
50-
res.SetError(err, cmdkit.ErrNormal)
51-
return
49+
return err
5250
}
5351

54-
node, err := tar.ImportTar(req.Context(), fi, nd.DAG)
52+
node, err := tar.ImportTar(req.Context, fi, nd.DAG)
5553
if err != nil {
56-
res.SetError(err, cmdkit.ErrNormal)
57-
return
54+
return err
5855
}
5956

6057
c := node.Cid()
6158

6259
fi.FileName()
63-
res.SetOutput(&coreiface.AddEvent{
60+
return res.Emit(&coreiface.AddEvent{
6461
Name: fi.FileName(),
6562
Hash: c.String(),
6663
})
6764
},
6865
Type: coreiface.AddEvent{},
69-
Marshalers: cmds.MarshalerMap{
70-
cmds.Text: func(res cmds.Response) (io.Reader, error) {
71-
v, err := unwrapOutput(res.Output())
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
o, ok := v.(*coreiface.AddEvent)
77-
if !ok {
78-
return nil, e.TypeErr(o, v)
79-
}
80-
return strings.NewReader(o.Hash + "\n"), nil
81-
},
66+
Encoders: cmds.EncoderMap{
67+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *coreiface.AddEvent) error {
68+
fmt.Fprintln(w, out.Hash)
69+
return nil
70+
}),
8271
},
8372
}
8473

@@ -93,37 +82,32 @@ var tarCatCmd = &cmds.Command{
9382
Arguments: []cmdkit.Argument{
9483
cmdkit.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
9584
},
96-
Run: func(req cmds.Request, res cmds.Response) {
97-
nd, err := req.InvocContext().GetNode()
85+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
86+
nd, err := cmdenv.GetNode(env)
9887
if err != nil {
99-
res.SetError(err, cmdkit.ErrNormal)
100-
return
88+
return err
10189
}
10290

103-
p, err := path.ParsePath(req.Arguments()[0])
91+
p, err := path.ParsePath(req.Arguments[0])
10492
if err != nil {
105-
res.SetError(err, cmdkit.ErrNormal)
106-
return
93+
return err
10794
}
10895

109-
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, p)
96+
root, err := core.Resolve(req.Context, nd.Namesys, nd.Resolver, p)
11097
if err != nil {
111-
res.SetError(err, cmdkit.ErrNormal)
112-
return
98+
return err
11399
}
114100

115101
rootpb, ok := root.(*dag.ProtoNode)
116102
if !ok {
117-
res.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
118-
return
103+
return dag.ErrNotProtobuf
119104
}
120105

121-
r, err := tar.ExportTar(req.Context(), rootpb, nd.DAG)
106+
r, err := tar.ExportTar(req.Context, rootpb, nd.DAG)
122107
if err != nil {
123-
res.SetError(err, cmdkit.ErrNormal)
124-
return
108+
return err
125109
}
126110

127-
res.SetOutput(r)
111+
return res.Emit(r)
128112
},
129113
}

core/commands/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
version "github.com/ipfs/go-ipfs"
99
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
1010

11-
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
11+
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
1212
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1313
)
1414

0 commit comments

Comments
 (0)