Skip to content

Commit 51fa833

Browse files
committed
cmds: use MakeTypedEncoder
License: MIT Signed-off-by: Overbool <[email protected]>
1 parent 085217e commit 51fa833

File tree

11 files changed

+86
-200
lines changed

11 files changed

+86
-200
lines changed

core/commands/bitswap.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,21 @@ var bitswapStatCmd = &cmds.Command{
112112
return cmds.EmitOnce(res, st)
113113
},
114114
Encoders: cmds.EncoderMap{
115-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
116-
out, ok := v.(*bitswap.Stat)
117-
if !ok {
118-
return e.TypeErr(out, v)
119-
}
120-
115+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *bitswap.Stat) error {
121116
fmt.Fprintln(w, "bitswap status")
122-
fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
123-
fmt.Fprintf(w, "\tblocks received: %d\n", out.BlocksReceived)
124-
fmt.Fprintf(w, "\tblocks sent: %d\n", out.BlocksSent)
125-
fmt.Fprintf(w, "\tdata received: %d\n", out.DataReceived)
126-
fmt.Fprintf(w, "\tdata sent: %d\n", out.DataSent)
127-
fmt.Fprintf(w, "\tdup blocks received: %d\n", out.DupBlksReceived)
128-
fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
129-
fmt.Fprintf(w, "\twantlist [%d keys]\n", len(out.Wantlist))
130-
for _, k := range out.Wantlist {
117+
fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize)
118+
fmt.Fprintf(w, "\tblocks received: %d\n", s.BlocksReceived)
119+
fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
120+
fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
121+
fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
122+
fmt.Fprintf(w, "\tdup blocks received: %d\n", s.DupBlksReceived)
123+
fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
124+
fmt.Fprintf(w, "\twantlist [%d keys]\n", len(s.Wantlist))
125+
for _, k := range s.Wantlist {
131126
fmt.Fprintf(w, "\t\t%s\n", k.String())
132127
}
133-
fmt.Fprintf(w, "\tpartners [%d]\n", len(out.Peers))
134-
for _, p := range out.Peers {
128+
fmt.Fprintf(w, "\tpartners [%d]\n", len(s.Peers))
129+
for _, p := range s.Peers {
135130
fmt.Fprintf(w, "\t\t%s\n", p)
136131
}
137132

@@ -172,6 +167,7 @@ prints the ledger associated with a given peer.
172167
if err != nil {
173168
return err
174169
}
170+
175171
return cmds.EmitOnce(res, bs.LedgerForPeer(partner))
176172
},
177173
Encoders: cmds.EncoderMap{

core/commands/block.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
1010
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11-
e "github.com/ipfs/go-ipfs/core/commands/e"
1211
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1312
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1413

@@ -83,11 +82,7 @@ on raw IPFS blocks. It outputs the following to stdout:
8382
},
8483
Type: BlockStat{},
8584
Encoders: cmds.EncoderMap{
86-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
87-
bs, ok := v.(*BlockStat)
88-
if !ok {
89-
return e.TypeErr(bs, v)
90-
}
85+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
9186
_, err := fmt.Fprintf(w, "%s", bs)
9287
return err
9388
}),
@@ -194,11 +189,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
194189
})
195190
},
196191
Encoders: cmds.EncoderMap{
197-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
198-
bs, ok := v.(*BlockStat)
199-
if !ok {
200-
return e.TypeErr(bs, v)
201-
}
192+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
202193
_, err := fmt.Fprintf(w, "%s\n", bs.Key)
203194
return err
204195
}),

core/commands/cid.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"strings"
88
"unicode"
99

10-
"github.com/ipfs/go-ipfs/core/commands/e"
11-
1210
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1311
verifcid "gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
1412
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
@@ -245,13 +243,9 @@ var basesCmd = &cmds.Command{
245243
return nil
246244
},
247245
Encoders: cmds.EncoderMap{
248-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
246+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, val []CodeAndName) error {
249247
prefixes, _ := req.Options[prefixOptionName].(bool)
250248
numeric, _ := req.Options[numericOptionName].(bool)
251-
val, ok := val0.([]CodeAndName)
252-
if !ok {
253-
return e.TypeErr(val, val0)
254-
}
255249
sort.Sort(multibaseSorter{val})
256250
for _, v := range val {
257251
code := v.Code
@@ -297,12 +291,8 @@ var codecsCmd = &cmds.Command{
297291
return nil
298292
},
299293
Encoders: cmds.EncoderMap{
300-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
294+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, val []CodeAndName) error {
301295
numeric, _ := req.Options[codecsNumericOptionName].(bool)
302-
val, ok := val0.([]CodeAndName)
303-
if !ok {
304-
return e.TypeErr(val, val0)
305-
}
306296
sort.Sort(codeAndNameSorter{val})
307297
for _, v := range val {
308298
if numeric {

core/commands/keystore.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"text/tabwriter"
77

88
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9-
"github.com/ipfs/go-ipfs/core/commands/e"
10-
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9+
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1110

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

1615
var KeyCmd = &cmds.Command{
@@ -106,13 +105,8 @@ var keyGenCmd = &cmds.Command{
106105
})
107106
},
108107
Encoders: cmds.EncoderMap{
109-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
110-
k, ok := v.(*KeyOutput)
111-
if !ok {
112-
return e.TypeErr(k, v)
113-
}
114-
115-
_, err := w.Write([]byte(k.Id + "\n"))
108+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ko *KeyOutput) error {
109+
_, err := w.Write([]byte(ko.Id + "\n"))
116110
return err
117111
}),
118112
},
@@ -146,7 +140,7 @@ var keyListCmd = &cmds.Command{
146140
return cmds.EmitOnce(res, &KeyOutputList{list})
147141
},
148142
Encoders: cmds.EncoderMap{
149-
cmds.Text: keyOutputListMarshaler(),
143+
cmds.Text: keyOutputListEncoders(),
150144
},
151145
Type: KeyOutputList{},
152146
}
@@ -189,16 +183,11 @@ var keyRenameCmd = &cmds.Command{
189183
})
190184
},
191185
Encoders: cmds.EncoderMap{
192-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
193-
k, ok := v.(*KeyRenameOutput)
194-
if !ok {
195-
return fmt.Errorf("expected a KeyRenameOutput as command result")
196-
}
197-
198-
if k.Overwrite {
199-
fmt.Fprintf(w, "Key %s renamed to %s with overwriting\n", k.Id, k.Now)
186+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, kro *KeyRenameOutput) error {
187+
if kro.Overwrite {
188+
fmt.Fprintf(w, "Key %s renamed to %s with overwriting\n", kro.Id, kro.Now)
200189
} else {
201-
fmt.Fprintf(w, "Key %s renamed to %s\n", k.Id, k.Now)
190+
fmt.Fprintf(w, "Key %s renamed to %s\n", kro.Id, kro.Now)
202191
}
203192
return nil
204193
}),
@@ -237,20 +226,15 @@ var keyRmCmd = &cmds.Command{
237226
return cmds.EmitOnce(res, &KeyOutputList{list})
238227
},
239228
Encoders: cmds.EncoderMap{
240-
cmds.Text: keyOutputListMarshaler(),
229+
cmds.Text: keyOutputListEncoders(),
241230
},
242231
Type: KeyOutputList{},
243232
}
244233

245-
func keyOutputListMarshaler() cmds.EncoderFunc {
246-
return cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
234+
func keyOutputListEncoders() cmds.EncoderFunc {
235+
return cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *KeyOutputList) error {
247236
withID, _ := req.Options["l"].(bool)
248237

249-
list, ok := v.(*KeyOutputList)
250-
if !ok {
251-
return e.TypeErr(list, v)
252-
}
253-
254238
tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
255239
for _, s := range list.Keys {
256240
if withID {

core/commands/name/ipns.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88
"time"
99

1010
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11-
e "github.com/ipfs/go-ipfs/core/commands/e"
1211
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1312
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
1413

1514
path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
1615
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
1716
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
18-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
17+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1918
)
2019

2120
var log = logging.Logger("core/commands/ipns")
@@ -158,12 +157,8 @@ Resolve the value of a dnslink:
158157
return nil
159158
},
160159
Encoders: cmds.EncoderMap{
161-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
162-
output, ok := v.(*ResolvedPath)
163-
if !ok {
164-
return e.TypeErr(output, v)
165-
}
166-
_, err := fmt.Fprintln(w, output.Path)
160+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, rp *ResolvedPath) error {
161+
_, err := fmt.Fprintln(w, rp.Path)
167162
return err
168163
}),
169164
},

core/commands/name/ipnsps.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77

88
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
9-
"github.com/ipfs/go-ipfs/core/commands/e"
10-
119
"gx/ipfs/QmSoeYGNm8v8jAF49hX7UwHwkXjoeobSrn9sya5NPPsxXP/go-libp2p-record"
1210
"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
1311
"gx/ipfs/QmcqU6QUDSXprb1518vYDGczrTJTyGwLG9eUa5iNX4xUtS/go-libp2p-peer"
@@ -57,14 +55,9 @@ var ipnspsStateCmd = &cmds.Command{
5755
},
5856
Type: ipnsPubsubState{},
5957
Encoders: cmds.EncoderMap{
60-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
61-
output, ok := v.(*ipnsPubsubState)
62-
if !ok {
63-
return e.TypeErr(output, v)
64-
}
65-
58+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ips *ipnsPubsubState) error {
6659
var state string
67-
if output.Enabled {
60+
if ips.Enabled {
6861
state = "enabled"
6962
} else {
7063
state = "disabled"
@@ -108,7 +101,7 @@ var ipnspsSubsCmd = &cmds.Command{
108101
},
109102
Type: stringList{},
110103
Encoders: cmds.EncoderMap{
111-
cmds.Text: stringListMarshaler(),
104+
cmds.Text: stringListEncoder(),
112105
},
113106
}
114107

@@ -144,14 +137,9 @@ var ipnspsCancelCmd = &cmds.Command{
144137
},
145138
Type: ipnsPubsubCancel{},
146139
Encoders: cmds.EncoderMap{
147-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
148-
output, ok := v.(*ipnsPubsubCancel)
149-
if !ok {
150-
return e.TypeErr(output, v)
151-
}
152-
140+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ipc *ipnsPubsubCancel) error {
153141
var state string
154-
if output.Canceled {
142+
if ipc.Canceled {
155143
state = "canceled"
156144
} else {
157145
state = "no subscription"
@@ -163,13 +151,8 @@ var ipnspsCancelCmd = &cmds.Command{
163151
},
164152
}
165153

166-
func stringListMarshaler() cmds.EncoderFunc {
167-
return cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
168-
list, ok := v.(*stringList)
169-
if !ok {
170-
return e.TypeErr(list, v)
171-
}
172-
154+
func stringListEncoder() cmds.EncoderFunc {
155+
return cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *stringList) error {
173156
for _, s := range list.Strings {
174157
_, err := fmt.Fprintln(w, s)
175158
if err != nil {

core/commands/name/publish.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import (
77
"time"
88

99
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10-
e "github.com/ipfs/go-ipfs/core/commands/e"
1110
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1211
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1312

1413
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
15-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
14+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1615
)
1716

1817
var (
@@ -139,18 +138,13 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
139138
})
140139
},
141140
Encoders: cmds.EncoderMap{
142-
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
143-
entry, ok := v.(*IpnsEntry)
144-
if !ok {
145-
return e.TypeErr(entry, v)
146-
}
147-
141+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ie *IpnsEntry) error {
148142
var err error
149143
quieter, _ := req.Options[quieterOptionName].(bool)
150144
if quieter {
151-
_, err = fmt.Fprintln(w, entry.Name)
145+
_, err = fmt.Fprintln(w, ie.Name)
152146
} else {
153-
_, err = fmt.Fprintf(w, "Published to %s: %s\n", entry.Name, entry.Value)
147+
_, err = fmt.Fprintf(w, "Published to %s: %s\n", ie.Name, ie.Value)
154148
}
155149
return err
156150
}),

0 commit comments

Comments
 (0)