Skip to content

Commit 3accb7b

Browse files
authored
Merge pull request #5650 from overbool/refactor/new-cmds-lib
refactor(cmds): use new cmds lib in version, tar and dns
2 parents 90d8cff + 4160eaa commit 3accb7b

File tree

5 files changed

+75
-130
lines changed

5 files changed

+75
-130
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/id.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
core "github.com/ipfs/go-ipfs/core"
1212
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
13-
e "github.com/ipfs/go-ipfs/core/commands/e"
1413

1514
ic "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
1615
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
@@ -112,25 +111,20 @@ EXAMPLE:
112111
return cmds.EmitOnce(res, output)
113112
},
114113
Encoders: cmds.EncoderMap{
115-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
116-
val, ok := v.(*IdOutput)
117-
if !ok {
118-
return e.TypeErr(val, v)
119-
}
120-
114+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *IdOutput) error {
121115
format, found := req.Options[formatOptionName].(string)
122116
if found {
123117
output := format
124-
output = strings.Replace(output, "<id>", val.ID, -1)
125-
output = strings.Replace(output, "<aver>", val.AgentVersion, -1)
126-
output = strings.Replace(output, "<pver>", val.ProtocolVersion, -1)
127-
output = strings.Replace(output, "<pubkey>", val.PublicKey, -1)
128-
output = strings.Replace(output, "<addrs>", strings.Join(val.Addresses, "\n"), -1)
118+
output = strings.Replace(output, "<id>", out.ID, -1)
119+
output = strings.Replace(output, "<aver>", out.AgentVersion, -1)
120+
output = strings.Replace(output, "<pver>", out.ProtocolVersion, -1)
121+
output = strings.Replace(output, "<pubkey>", out.PublicKey, -1)
122+
output = strings.Replace(output, "<addrs>", strings.Join(out.Addresses, "\n"), -1)
129123
output = strings.Replace(output, "\\n", "\n", -1)
130124
output = strings.Replace(output, "\\t", "\t", -1)
131125
fmt.Fprint(w, output)
132126
} else {
133-
marshaled, err := json.MarshalIndent(val, "", "\t")
127+
marshaled, err := json.MarshalIndent(out, "", "\t")
134128
if err != nil {
135129
return err
136130
}

core/commands/root.go

+5-5
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,11 +141,11 @@ 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,
148-
"version": lgc.NewCommand(VersionCmd),
148+
"version": VersionCmd,
149149
"shutdown": daemonShutdownCmd,
150150
"cid": CidCmd,
151151
}
@@ -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{
@@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{
189189
},
190190
}),
191191
"resolve": ResolveCmd,
192-
"version": lgc.NewCommand(VersionCmd),
192+
"version": VersionCmd,
193193
}
194194

195195
func init() {

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

+18-39
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"fmt"
55
"io"
66
"runtime"
7-
"strings"
87

98
version "github.com/ipfs/go-ipfs"
10-
cmds "github.com/ipfs/go-ipfs/commands"
11-
e "github.com/ipfs/go-ipfs/core/commands/e"
129
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
1310

11+
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
1412
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1513
)
1614

@@ -41,66 +39,47 @@ var VersionCmd = &cmds.Command{
4139
cmdkit.BoolOption(versionRepoOptionName, "Show repo version."),
4240
cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
4341
},
44-
Run: func(req cmds.Request, res cmds.Response) {
45-
res.SetOutput(&VersionOutput{
42+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
43+
return res.Emit(&VersionOutput{
4644
Version: version.CurrentVersionNumber,
4745
Commit: version.CurrentCommit,
4846
Repo: fmt.Sprint(fsrepo.RepoVersion),
4947
System: runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
5048
Golang: runtime.Version(),
5149
})
5250
},
53-
Marshalers: cmds.MarshalerMap{
54-
cmds.Text: func(res cmds.Response) (io.Reader, error) {
55-
v, err := unwrapOutput(res.Output())
56-
if err != nil {
57-
return nil, err
58-
}
59-
60-
version, ok := v.(*VersionOutput)
61-
if !ok {
62-
return nil, e.TypeErr(version, v)
63-
}
64-
65-
repo, _, err := res.Request().Option(versionRepoOptionName).Bool()
66-
if err != nil {
67-
return nil, err
68-
}
69-
51+
Encoders: cmds.EncoderMap{
52+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, version *VersionOutput) error {
53+
repo, _ := req.Options[versionRepoOptionName].(bool)
7054
if repo {
71-
return strings.NewReader(version.Repo + "\n"), nil
55+
fmt.Fprintln(w, version.Repo)
56+
return nil
7257
}
7358

74-
commit, _, err := res.Request().Option(versionCommitOptionName).Bool()
59+
commit, _ := req.Options[versionCommitOptionName].(bool)
7560
commitTxt := ""
76-
if err != nil {
77-
return nil, err
78-
}
7961
if commit {
8062
commitTxt = "-" + version.Commit
8163
}
8264

83-
number, _, err := res.Request().Option(versionNumberOptionName).Bool()
84-
if err != nil {
85-
return nil, err
86-
}
65+
number, _ := req.Options[versionNumberOptionName].(bool)
8766
if number {
88-
return strings.NewReader(fmt.Sprintln(version.Version + commitTxt)), nil
67+
fmt.Fprintln(w, version.Version+commitTxt)
68+
return nil
8969
}
9070

91-
all, _, err := res.Request().Option(versionAllOptionName).Bool()
92-
if err != nil {
93-
return nil, err
94-
}
71+
all, _ := req.Options[versionAllOptionName].(bool)
9572
if all {
9673
out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
9774
"Repo version: %s\nSystem version: %s\nGolang version: %s\n",
9875
version.Version, version.Commit, version.Repo, version.System, version.Golang)
99-
return strings.NewReader(out), nil
76+
fmt.Fprint(w, out)
77+
return nil
10078
}
10179

102-
return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt)), nil
103-
},
80+
fmt.Fprint(w, fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt))
81+
return nil
82+
}),
10483
},
10584
Type: VersionOutput{},
10685
}

0 commit comments

Comments
 (0)