Skip to content

Commit f7d408d

Browse files
committed
Enable --cid-base for more commands.
Move APICid and Encoder to go-cidutil. License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent a2f4276 commit f7d408d

22 files changed

+232
-214
lines changed

core/apicid.go

-131
This file was deleted.

core/commands/add.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
cmds "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
2020
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
2121
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
22-
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
2322
mfs "gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
2423
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
2524
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
25+
cidutil "gx/ipfs/QmSwrzRygK8yHBzd8gJJYedttQVQTZJrbEbpEBYvQLaRy8/go-cidutil"
2626
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
2727
bstore "gx/ipfs/QmegPGspn3RpTMQ23Fd3GVVMopo1zsEMurudbFMZ5UXBLH/go-ipfs-blockstore"
2828
)
@@ -390,7 +390,7 @@ You can now check what blocks have been created by:
390390
log.Warning("cannot determine size of input file")
391391
}
392392

393-
err := HandleCidBase(req)
393+
_, err := NewCidBaseHandler(req).UseGlobal().Proc()
394394
if err != nil {
395395
re.SetError(err, cmdkit.ErrNormal)
396396
return re

core/commands/dns.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ The resolver can recursively resolve:
5454
cmdkit.BoolOption("recursive", "r", "Resolve until the result is not a DNS link."),
5555
},
5656
Run: func(req cmds.Request, res cmds.Response) {
57-
5857
recursive, _, _ := req.Option("recursive").Bool()
5958
name := req.Arguments()[0]
6059
resolver := namesys.NewDNSResolver()
@@ -77,6 +76,7 @@ The resolver can recursively resolve:
7776
},
7877
Marshalers: cmds.MarshalerMap{
7978
cmds.Text: func(res cmds.Response) (io.Reader, error) {
79+
8080
v, err := unwrapOutput(res.Output())
8181
if err != nil {
8282
return nil, err

core/commands/files.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
3131
mfs "gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
3232
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
33+
apicid "gx/ipfs/QmSwrzRygK8yHBzd8gJJYedttQVQTZJrbEbpEBYvQLaRy8/go-cidutil/apicid"
3334
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
3435
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
3536
)
@@ -77,7 +78,7 @@ var hashOption = cmdkit.StringOption("hash", "Hash function to use. Will set Cid
7778
var errFormat = errors.New("format was set by multiple options. Only one format option is allowed")
7879

7980
type statOutput struct {
80-
Hash string
81+
Hash apicid.Hash
8182
Size uint64
8283
CumulativeSize uint64
8384
Blocks int
@@ -167,13 +168,18 @@ var filesStatCmd = &cmds.Command{
167168
},
168169
Encoders: cmds.EncoderMap{
169170
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
171+
_, err := NewCidBaseHandler(req).UseGlobal().Proc()
172+
if err != nil {
173+
return err
174+
}
175+
170176
out, ok := v.(*statOutput)
171177
if !ok {
172178
return e.TypeErr(out, v)
173179
}
174180

175181
s, _ := statGetFormatOptions(req)
176-
s = strings.Replace(s, "<hash>", out.Hash, -1)
182+
s = strings.Replace(s, "<hash>", out.Hash.String(), -1)
177183
s = strings.Replace(s, "<size>", fmt.Sprintf("%d", out.Size), -1)
178184
s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1)
179185
s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1)
@@ -244,15 +250,15 @@ func statNode(nd ipld.Node) (*statOutput, error) {
244250
}
245251

246252
return &statOutput{
247-
Hash: c.String(),
253+
Hash: apicid.FromCid(c),
248254
Blocks: len(nd.Links()),
249255
Size: d.GetFilesize(),
250256
CumulativeSize: cumulsize,
251257
Type: ndtype,
252258
}, nil
253259
case *dag.RawNode:
254260
return &statOutput{
255-
Hash: c.String(),
261+
Hash: apicid.FromCid(c),
256262
Blocks: 0,
257263
Size: cumulsize,
258264
CumulativeSize: cumulsize,
@@ -491,6 +497,11 @@ Examples:
491497
},
492498
Marshalers: oldcmds.MarshalerMap{
493499
oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
500+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
501+
if err != nil {
502+
return nil, err
503+
}
504+
494505
v, err := unwrapOutput(res.Output())
495506
if err != nil {
496507
return nil, err

core/commands/filestore.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ The output is:
8484
cmds.CLI: func(req *cmds.Request, re cmds.ResponseEmitter) cmds.ResponseEmitter {
8585
reNext, res := cmds.NewChanResponsePair(req)
8686

87+
_, err := NewCidBaseHandler(req).UseGlobal().Proc()
88+
if err != nil {
89+
re.SetError(err, cmdkit.ErrNormal)
90+
return reNext
91+
}
92+
8793
go func() {
8894
defer re.Close()
8995

@@ -174,6 +180,11 @@ For ERROR entries the error will also be printed to stderr.
174180
},
175181
Marshalers: oldCmds.MarshalerMap{
176182
oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) {
183+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
184+
if err != nil {
185+
return nil, err
186+
}
187+
177188
v, err := unwrapOutput(res.Output())
178189
if err != nil {
179190
return nil, err
@@ -210,6 +221,13 @@ var dupsFileStore = &oldCmds.Command{
210221
return
211222
}
212223

224+
h, err := NewCidBaseHandlerLegacy(req).Proc()
225+
if err != nil {
226+
res.SetError(err, cmdkit.ErrNormal)
227+
return
228+
}
229+
enc := h.Encoder()
230+
213231
out := make(chan interface{}, 128)
214232
res.SetOutput((<-chan interface{})(out))
215233

@@ -226,7 +244,7 @@ var dupsFileStore = &oldCmds.Command{
226244
}
227245
if have {
228246
select {
229-
case out <- &RefWrapper{Ref: cid.String()}:
247+
case out <- &RefWrapper{Ref: enc.Encode(cid)}:
230248
case <-req.Context().Done():
231249
return
232250
}

core/commands/ls.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919

2020
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
2121
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
22+
apicid "gx/ipfs/QmSwrzRygK8yHBzd8gJJYedttQVQTZJrbEbpEBYvQLaRy8/go-cidutil/apicid"
2223
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
2324
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
2425
)
2526

2627
type LsLink struct {
2728
Name string
28-
Hash core.APICid
29+
Hash apicid.Hash
2930
Size uint64
3031
Type unixfspb.Data_DataType
3132
}
@@ -161,7 +162,7 @@ The JSON output contains type information.
161162
}
162163
output[i].Links[j] = LsLink{
163164
Name: link.Name,
164-
Hash: core.FromCid(link.Cid),
165+
Hash: apicid.FromCid(link.Cid),
165166
Size: link.Size,
166167
Type: t,
167168
}
@@ -172,7 +173,7 @@ The JSON output contains type information.
172173
},
173174
Marshalers: cmds.MarshalerMap{
174175
cmds.Text: func(res cmds.Response) (io.Reader, error) {
175-
err := HandleCidBaseLegacy(res.Request())
176+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
176177
if err != nil {
177178
return nil, err
178179
}

core/commands/pin.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
1818
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
19+
apicid "gx/ipfs/QmSwrzRygK8yHBzd8gJJYedttQVQTZJrbEbpEBYvQLaRy8/go-cidutil/apicid"
1920
"gx/ipfs/QmVkMRSkXrpjqrroEXWuYBvDBnXCdMMY6gsKicBGVGUqKT/go-verifcid"
2021
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
2122
resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
@@ -39,11 +40,11 @@ var PinCmd = &cmds.Command{
3940
}
4041

4142
type PinOutput struct {
42-
Pins []core.APICid
43+
Pins []apicid.Hash
4344
}
4445

4546
type AddPinOutput struct {
46-
Pins []core.APICid
47+
Pins []apicid.Hash
4748
Progress int `json:",omitempty"`
4849
}
4950

@@ -130,7 +131,7 @@ var addPinCmd = &cmds.Command{
130131
},
131132
Marshalers: cmds.MarshalerMap{
132133
cmds.Text: func(res cmds.Response) (io.Reader, error) {
133-
err := HandleCidBaseLegacy(res.Request())
134+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
134135
if err != nil {
135136
return nil, err
136137
}
@@ -139,7 +140,7 @@ var addPinCmd = &cmds.Command{
139140
return nil, err
140141
}
141142

142-
var added []core.APICid
143+
var added []apicid.Hash
143144

144145
switch out := v.(type) {
145146
case *AddPinOutput:
@@ -214,10 +215,11 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
214215
},
215216
Marshalers: cmds.MarshalerMap{
216217
cmds.Text: func(res cmds.Response) (io.Reader, error) {
217-
err := HandleCidBaseLegacy(res.Request())
218+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
218219
if err != nil {
219220
return nil, err
220221
}
222+
221223
v, err := unwrapOutput(res.Output())
222224
if err != nil {
223225
return nil, err
@@ -326,10 +328,6 @@ Example:
326328
Type: RefKeyList{},
327329
Marshalers: cmds.MarshalerMap{
328330
cmds.Text: func(res cmds.Response) (io.Reader, error) {
329-
err := HandleCidBaseLegacy(res.Request())
330-
if err != nil {
331-
return nil, err
332-
}
333331
v, err := unwrapOutput(res.Output())
334332
if err != nil {
335333
return nil, err
@@ -696,10 +694,10 @@ func (r PinVerifyRes) Format(out io.Writer) {
696694
}
697695
}
698696

699-
func toAPICids(cs []cid.Cid) []core.APICid {
700-
out := make([]core.APICid, 0, len(cs))
697+
func toAPICids(cs []cid.Cid) []apicid.Hash {
698+
out := make([]apicid.Hash, 0, len(cs))
701699
for _, c := range cs {
702-
out = append(out, core.FromCid(c))
700+
out = append(out, apicid.FromCid(c))
703701
}
704702
return out
705703
}

0 commit comments

Comments
 (0)