@cryptotaxi247 / kubo / commits / 51fa833fd

cmds: use MakeTypedEncoder

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

Overbool committed Nov 10, 2018 at 11:57 UTC 51fa833fda660087da24868286f79a19856efcf9
11 files changed +86 -200
core/commands/bitswap.go
+13 -17
@@ -112,26 +112,21 @@ var bitswapStatCmd = &cmds.Command{
112 return cmds.EmitOnce(res, st)
113 },
114 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 {
116 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 {
126 fmt.Fprintf(w, "\t\t%s\n", k.String())
127 }
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 {
130 fmt.Fprintf(w, "\t\t%s\n", p)
131 }
132
@@ -172,6 +167,7 @@ prints the ledger associated with a given peer.
167 if err != nil {
168 return err
169 }
170 +
171 return cmds.EmitOnce(res, bs.LedgerForPeer(partner))
172 },
173 Encoders: cmds.EncoderMap{
core/commands/block.go
+2 -11
@@ -8,7 +8,6 @@ import (
8
9 util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
10 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11 - e "github.com/ipfs/go-ipfs/core/commands/e"
11 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12 "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
@@ -83,11 +82,7 @@ on raw IPFS blocks. It outputs the following to stdout:
82 },
83 Type: BlockStat{},
84 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 {
86 _, err := fmt.Fprintf(w, "%s", bs)
87 return err
88 }),
@@ -194,11 +189,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
189 })
190 },
191 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 {
193 _, err := fmt.Fprintf(w, "%s\n", bs.Key)
194 return err
195 }),
core/commands/cid.go
+2 -12
@@ -7,8 +7,6 @@ import (
7 "strings"
8 "unicode"
9
10 - "github.com/ipfs/go-ipfs/core/commands/e"
11 -
10 cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
11 verifcid "gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
12 cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
@@ -245,13 +243,9 @@ var basesCmd = &cmds.Command{
243 return nil
244 },
245 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 {
247 prefixes, _ := req.Options[prefixOptionName].(bool)
248 numeric, _ := req.Options[numericOptionName].(bool)
251 - val, ok := val0.([]CodeAndName)
252 - if !ok {
253 - return e.TypeErr(val, val0)
254 - }
249 sort.Sort(multibaseSorter{val})
250 for _, v := range val {
251 code := v.Code
@@ -297,12 +291,8 @@ var codecsCmd = &cmds.Command{
291 return nil
292 },
293 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 {
295 numeric, _ := req.Options[codecsNumericOptionName].(bool)
302 - val, ok := val0.([]CodeAndName)
303 - if !ok {
304 - return e.TypeErr(val, val0)
305 - }
296 sort.Sort(codeAndNameSorter{val})
297 for _, v := range val {
298 if numeric {
core/commands/keystore.go
+13 -29
@@ -6,11 +6,10 @@ import (
6 "text/tabwriter"
7
8 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"
10
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"
13 )
14
15 var KeyCmd = &cmds.Command{
@@ -106,13 +105,8 @@ var keyGenCmd = &cmds.Command{
105 })
106 },
107 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"))
110 return err
111 }),
112 },
@@ -146,7 +140,7 @@ var keyListCmd = &cmds.Command{
140 return cmds.EmitOnce(res, &KeyOutputList{list})
141 },
142 Encoders: cmds.EncoderMap{
149 - cmds.Text: keyOutputListMarshaler(),
143 + cmds.Text: keyOutputListEncoders(),
144 },
145 Type: KeyOutputList{},
146 }
@@ -189,16 +183,11 @@ var keyRenameCmd = &cmds.Command{
183 })
184 },
185 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)
189 } 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)
191 }
192 return nil
193 }),
@@ -237,20 +226,15 @@ var keyRmCmd = &cmds.Command{
226 return cmds.EmitOnce(res, &KeyOutputList{list})
227 },
228 Encoders: cmds.EncoderMap{
240 - cmds.Text: keyOutputListMarshaler(),
229 + cmds.Text: keyOutputListEncoders(),
230 },
231 Type: KeyOutputList{},
232 }
233
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 {
236 withID, _ := req.Options["l"].(bool)
237
249 - list, ok := v.(*KeyOutputList)
250 - if !ok {
251 - return e.TypeErr(list, v)
252 - }
253 -
238 tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
239 for _, s := range list.Keys {
240 if withID {
core/commands/name/ipns.go
+3 -8
@@ -8,14 +8,13 @@ import (
8 "time"
9
10 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11 - e "github.com/ipfs/go-ipfs/core/commands/e"
11 options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
12 nsopts "github.com/ipfs/go-ipfs/namesys/opts"
13
14 path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
15 cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
16 logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
18 - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
17 + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
18 )
19
20 var log = logging.Logger("core/commands/ipns")
@@ -158,12 +157,8 @@ Resolve the value of a dnslink:
157 return nil
158 },
159 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)
162 return err
163 }),
164 },
core/commands/name/ipnsps.go
+7 -24
@@ -6,8 +6,6 @@ import (
6 "strings"
7
8 "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9 - "github.com/ipfs/go-ipfs/core/commands/e"
10 -
9 "gx/ipfs/QmSoeYGNm8v8jAF49hX7UwHwkXjoeobSrn9sya5NPPsxXP/go-libp2p-record"
10 "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
11 "gx/ipfs/QmcqU6QUDSXprb1518vYDGczrTJTyGwLG9eUa5iNX4xUtS/go-libp2p-peer"
@@ -57,14 +55,9 @@ var ipnspsStateCmd = &cmds.Command{
55 },
56 Type: ipnsPubsubState{},
57 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 {
59 var state string
67 - if output.Enabled {
60 + if ips.Enabled {
61 state = "enabled"
62 } else {
63 state = "disabled"
@@ -108,7 +101,7 @@ var ipnspsSubsCmd = &cmds.Command{
101 },
102 Type: stringList{},
103 Encoders: cmds.EncoderMap{
111 - cmds.Text: stringListMarshaler(),
104 + cmds.Text: stringListEncoder(),
105 },
106 }
107
@@ -144,14 +137,9 @@ var ipnspsCancelCmd = &cmds.Command{
137 },
138 Type: ipnsPubsubCancel{},
139 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 {
141 var state string
154 - if output.Canceled {
142 + if ipc.Canceled {
143 state = "canceled"
144 } else {
145 state = "no subscription"
@@ -163,13 +151,8 @@ var ipnspsCancelCmd = &cmds.Command{
151 },
152 }
153
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 {
156 for _, s := range list.Strings {
157 _, err := fmt.Fprintln(w, s)
158 if err != nil {
core/commands/name/publish.go
+4 -10
@@ -7,12 +7,11 @@ import (
7 "time"
8
9 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10 - e "github.com/ipfs/go-ipfs/core/commands/e"
10 iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
11 options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
12
13 cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
15 - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
14 + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
15 )
16
17 var (
@@ -139,18 +138,13 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
138 })
139 },
140 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 {
142 var err error
143 quieter, _ := req.Options[quieterOptionName].(bool)
144 if quieter {
151 - _, err = fmt.Fprintln(w, entry.Name)
145 + _, err = fmt.Fprintln(w, ie.Name)
146 } 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)
148 }
149 return err
150 }),
core/commands/pubsub.go
+12 -32
@@ -9,7 +9,6 @@ import (
9 "sort"
10
11 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12 - e "github.com/ipfs/go-ipfs/core/commands/e"
12 options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
14 cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
@@ -116,35 +115,20 @@ This command outputs data in the following encodings:
115 }
116 },
117 Encoders: cmds.EncoderMap{
119 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
120 - m, ok := v.(*pubsubMessage)
121 - if !ok {
122 - return fmt.Errorf("unexpected type: %T", v)
123 - }
124 -
125 - _, err := w.Write(m.Data)
118 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, psm *pubsubMessage) error {
119 + _, err := w.Write(psm.Data)
120 return err
121 }),
128 - "ndpayload": cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
129 - m, ok := v.(*pubsubMessage)
130 - if !ok {
131 - return fmt.Errorf("unexpected type: %T", v)
132 - }
133 -
134 - m.Data = append(m.Data, '\n')
135 - _, err := w.Write(m.Data)
122 + "ndpayload": cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, psm *pubsubMessage) error {
123 + psm.Data = append(psm.Data, '\n')
124 + _, err := w.Write(psm.Data)
125 return err
126 }),
138 - "lenpayload": cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
139 - m, ok := v.(*pubsubMessage)
140 - if !ok {
141 - return fmt.Errorf("unexpected type: %T", v)
142 - }
143 -
144 - buf := make([]byte, 8, len(m.Data)+8)
127 + "lenpayload": cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, psm *pubsubMessage) error {
128 + buf := make([]byte, 8, len(psm.Data)+8)
129
146 - n := binary.PutUvarint(buf, uint64(len(m.Data)))
147 - buf = append(buf[:n], m.Data...)
130 + n := binary.PutUvarint(buf, uint64(len(psm.Data)))
131 + buf = append(buf[:n], psm.Data...)
132 _, err := w.Write(buf)
133 return err
134 }),
@@ -218,15 +202,11 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
202 },
203 Type: stringList{},
204 Encoders: cmds.EncoderMap{
221 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
205 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
206 },
207 }
208
225 -func stringListEncoder(req *cmds.Request, w io.Writer, v interface{}) error {
226 - list, ok := v.(*stringList)
227 - if !ok {
228 - return e.TypeErr(list, v)
229 - }
209 +func stringListEncoder(req *cmds.Request, w io.Writer, list *stringList) error {
210 for _, str := range list.Strings {
211 _, err := fmt.Fprintf(w, "%s\n", str)
212 if err != nil {
@@ -279,6 +259,6 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
259 },
260 Type: stringList{},
261 Encoders: cmds.EncoderMap{
282 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
262 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
263 },
264 }
core/commands/repo.go
+5 -16
@@ -13,7 +13,6 @@ import (
13 "text/tabwriter"
14
15 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
16 - e "github.com/ipfs/go-ipfs/core/commands/e"
16 corerepo "github.com/ipfs/go-ipfs/core/corerepo"
17 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18
@@ -109,16 +108,11 @@ order to reclaim hard disk space.
108 },
109 Type: GcResult{},
110 Encoders: cmds.EncoderMap{
112 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
111 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, gcr *GcResult) error {
112 quiet, _ := req.Options[repoQuietOptionName].(bool)
113
115 - obj, ok := v.(*GcResult)
116 - if !ok {
117 - return e.TypeErr(obj, v)
118 - }
119 -
120 - if obj.Error != "" {
121 - _, err := fmt.Fprintf(w, "Error: %s\n", obj.Error)
114 + if gcr.Error != "" {
115 + _, err := fmt.Fprintf(w, "Error: %s\n", gcr.Error)
116 return err
117 }
118
@@ -127,7 +121,7 @@ order to reclaim hard disk space.
121 prefix = ""
122 }
123
130 - _, err := fmt.Fprintf(w, "%s%s\n", prefix, obj.Key)
124 + _, err := fmt.Fprintf(w, "%s%s\n", prefix, gcr.Key)
125 return err
126 }),
127 },
@@ -183,12 +177,7 @@ Version string The repo version.
177 },
178 Type: &corerepo.Stat{},
179 Encoders: cmds.EncoderMap{
186 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
187 - stat, ok := v.(*corerepo.Stat)
188 - if !ok {
189 - return e.TypeErr(stat, v)
190 - }
191 -
180 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, stat *corerepo.Stat) error {
181 wtr := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
182 defer wtr.Flush()
183
core/commands/resolve.go
+4 -10
@@ -8,7 +8,6 @@ import (
8 "time"
9
10 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11 - e "github.com/ipfs/go-ipfs/core/commands/e"
11 ncmd "github.com/ipfs/go-ipfs/core/commands/name"
12 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13 options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -16,8 +15,8 @@ import (
15 nsopts "github.com/ipfs/go-ipfs/namesys/opts"
16 path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
17
19 - "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
20 - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
18 + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
19 + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
20 )
21
22 const (
@@ -144,13 +143,8 @@ Resolve the value of an IPFS DAG path:
143 return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.Path("/" + rp.Namespace() + "/" + rp.Cid().String())})
144 },
145 Encoders: cmds.EncoderMap{
147 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
148 - output, ok := v.(*ncmd.ResolvedPath)
149 - if !ok {
150 - return e.TypeErr(output, v)
151 - }
152 -
153 - fmt.Fprintln(w, output.Path.String())
146 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, rp *ncmd.ResolvedPath) error {
147 + fmt.Fprintln(w, rp.Path.String())
148 return nil
149 }),
150 },
core/commands/swarm.go
+21 -31
@@ -7,22 +7,21 @@ import (
7 "path"
8 "sort"
9
10 - "github.com/ipfs/go-ipfs/commands"
11 - "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12 - "github.com/ipfs/go-ipfs/core/commands/e"
13 - "github.com/ipfs/go-ipfs/repo"
14 - "github.com/ipfs/go-ipfs/repo/fsrepo"
10 + commands "github.com/ipfs/go-ipfs/commands"
11 + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12 + repo "github.com/ipfs/go-ipfs/repo"
13 + fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
14
15 ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
16 inet "gx/ipfs/QmRKbEchaYADxSCyyjhDh4cTrUby8ftXUb8MRLBTHQYupw/go-libp2p-net"
17 mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
18 iaddr "gx/ipfs/QmUSE3APe1pMFVsUBZUZaKQKERiPteCWvTAERtVQmtXzgE/go-ipfs-addr"
19 pstore "gx/ipfs/QmUymf8fJtideyv3z727BcZUifGBjMZMpCJqu3Gxk5aRUk/go-libp2p-peerstore"
21 - "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
22 - "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
23 - "gx/ipfs/QmcYC4ayKi7bq8xecEZxHVEuTL6HREZWTTErrSRd1S3Spz/go-libp2p-swarm"
20 + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
21 + config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
22 + swarm "gx/ipfs/QmcYC4ayKi7bq8xecEZxHVEuTL6HREZWTTErrSRd1S3Spz/go-libp2p-swarm"
23 peer "gx/ipfs/QmcqU6QUDSXprb1518vYDGczrTJTyGwLG9eUa5iNX4xUtS/go-libp2p-peer"
25 - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
24 + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
25 )
26
27 type stringList struct {
@@ -129,12 +128,7 @@ var swarmPeersCmd = &cmds.Command{
128 return cmds.EmitOnce(res, &out)
129 },
130 Encoders: cmds.EncoderMap{
132 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
133 - ci, ok := v.(*connInfos)
134 - if !ok {
135 - return e.TypeErr(ci, v)
136 - }
137 -
131 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ci *connInfos) error {
132 pipfs := ma.ProtocolWithCode(ma.P_IPFS).Name
133 for _, info := range ci.Peers {
134 fmt.Fprintf(w, "%s/%s/%s", info.Addr, pipfs, info.Peer)
@@ -248,26 +242,22 @@ var swarmAddrsCmd = &cmds.Command{
242 return cmds.EmitOnce(res, &addrMap{Addrs: out})
243 },
244 Encoders: cmds.EncoderMap{
251 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
252 - m, ok := v.(*addrMap)
253 - if !ok {
254 - return e.TypeErr(m, v)
255 - }
256 -
245 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, am *addrMap) error {
246 // sort the ids first
258 - ids := make([]string, 0, len(m.Addrs))
259 - for p := range m.Addrs {
247 + ids := make([]string, 0, len(am.Addrs))
248 + for p := range am.Addrs {
249 ids = append(ids, p)
250 }
251 sort.Sort(sort.StringSlice(ids))
252
253 for _, p := range ids {
265 - paddrs := m.Addrs[p]
254 + paddrs := am.Addrs[p]
255 fmt.Fprintf(w, "%s (%d)\n", p, len(paddrs))
256 for _, addr := range paddrs {
257 fmt.Fprintf(w, "\t"+addr+"\n")
258 }
259 }
260 +
261 return nil
262 }),
263 },
@@ -314,7 +304,7 @@ var swarmAddrsLocalCmd = &cmds.Command{
304 },
305 Type: stringList{},
306 Encoders: cmds.EncoderMap{
317 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
307 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
308 },
309 }
310
@@ -346,7 +336,7 @@ var swarmAddrsListenCmd = &cmds.Command{
336 },
337 Type: stringList{},
338 Encoders: cmds.EncoderMap{
349 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
339 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
340 },
341 }
342
@@ -391,7 +381,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
381 return cmds.EmitOnce(res, &stringList{output})
382 },
383 Encoders: cmds.EncoderMap{
394 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
384 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
385 },
386 Type: stringList{},
387 }
@@ -436,7 +426,7 @@ it will reconnect.
426 return cmds.EmitOnce(res, &stringList{output})
427 },
428 Encoders: cmds.EncoderMap{
439 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
429 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
430 },
431 Type: stringList{},
432 }
@@ -532,7 +522,7 @@ Filters default to those specified under the "Swarm.AddrFilters" config key.
522 return cmds.EmitOnce(res, &stringList{output})
523 },
524 Encoders: cmds.EncoderMap{
535 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
525 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
526 },
527 Type: stringList{},
528 }
@@ -596,7 +586,7 @@ add your filters to the ipfs config file.
586 return cmds.EmitOnce(res, &stringList{added})
587 },
588 Encoders: cmds.EncoderMap{
599 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
589 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
590 },
591 Type: stringList{},
592 }
@@ -669,7 +659,7 @@ remove your filters from the ipfs config file.
659 return cmds.EmitOnce(res, &stringList{removed})
660 },
661 Encoders: cmds.EncoderMap{
672 - cmds.Text: cmds.MakeEncoder(stringListEncoder),
662 + cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
663 },
664 Type: stringList{},
665 }