Skip to content

Commit 3263143

Browse files
committed
Add --cid-base & --upgrade-cidv0 global opt. and enable them on select cmds
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 987fef1 commit 3263143

File tree

21 files changed

+290
-72
lines changed

21 files changed

+290
-72
lines changed

core/commands/add.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
dagtest "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag/test"
1616
blockservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
1717

18+
cidutil "gx/ipfs/QmNWQygwYxgz3QzXG2ytTkrHkZ4HnnSh94ASox3JjktFcR/go-cidutil"
1819
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
1920
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
2021
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
21-
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
2222
mfs "gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
2323
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
2424
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
@@ -367,6 +367,11 @@ You can now check what blocks have been created by:
367367
log.Warning("cannot determine size of input file")
368368
}
369369

370+
_, err := NewCidBaseHandler(req).UseGlobal().Proc()
371+
if err != nil {
372+
return err
373+
}
374+
370375
progressBar := func(wait chan struct{}) {
371376
defer close(wait)
372377

@@ -402,8 +407,9 @@ You can now check what blocks have been created by:
402407
break LOOP
403408
}
404409
output := out.(*coreunix.AddedObject)
405-
if len(output.Hash) > 0 {
406-
lastHash = output.Hash
410+
hash := output.Hash.String()
411+
if len(hash) > 0 {
412+
lastHash = hash
407413
if quieter {
408414
continue
409415
}
@@ -413,9 +419,9 @@ You can now check what blocks have been created by:
413419
fmt.Fprintf(os.Stderr, "\033[2K\r")
414420
}
415421
if quiet {
416-
fmt.Fprintf(os.Stdout, "%s\n", output.Hash)
422+
fmt.Fprintf(os.Stdout, "%s\n", hash)
417423
} else {
418-
fmt.Fprintf(os.Stdout, "added %s %s\n", output.Hash, output.Name)
424+
fmt.Fprintf(os.Stdout, "added %s %s\n", hash, output.Name)
419425
}
420426

421427
} else {

core/commands/dns.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
dag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
2424
bservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
2525

26+
apicid "gx/ipfs/QmNWQygwYxgz3QzXG2ytTkrHkZ4HnnSh94ASox3JjktFcR/go-cidutil/apicid"
2627
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
2728
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
2829
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
@@ -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
@@ -162,13 +163,18 @@ var filesStatCmd = &cmds.Command{
162163
},
163164
Encoders: cmds.EncoderMap{
164165
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
166+
_, err := NewCidBaseHandler(req).UseGlobal().Proc()
167+
if err != nil {
168+
return err
169+
}
170+
165171
out, ok := v.(*statOutput)
166172
if !ok {
167173
return e.TypeErr(out, v)
168174
}
169175

170176
s, _ := statGetFormatOptions(req)
171-
s = strings.Replace(s, "<hash>", out.Hash, -1)
177+
s = strings.Replace(s, "<hash>", out.Hash.String(), -1)
172178
s = strings.Replace(s, "<size>", fmt.Sprintf("%d", out.Size), -1)
173179
s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1)
174180
s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1)
@@ -239,15 +245,15 @@ func statNode(nd ipld.Node) (*statOutput, error) {
239245
}
240246

241247
return &statOutput{
242-
Hash: c.String(),
248+
Hash: apicid.FromCid(c),
243249
Blocks: len(nd.Links()),
244250
Size: d.GetFilesize(),
245251
CumulativeSize: cumulsize,
246252
Type: ndtype,
247253
}, nil
248254
case *dag.RawNode:
249255
return &statOutput{
250-
Hash: c.String(),
256+
Hash: apicid.FromCid(c),
251257
Blocks: 0,
252258
Size: cumulsize,
253259
CumulativeSize: cumulsize,
@@ -486,6 +492,11 @@ Examples:
486492
},
487493
Marshalers: oldcmds.MarshalerMap{
488494
oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
495+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
496+
if err != nil {
497+
return nil, err
498+
}
499+
489500
v, err := unwrapOutput(res.Output())
490501
if err != nil {
491502
return nil, err

core/commands/filestore.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ The output is:
7474
},
7575
PostRun: cmds.PostRunMap{
7676
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
77+
_, err := NewCidBaseHandler(res.Request()).UseGlobal().Proc()
78+
if err != nil {
79+
return err
80+
}
81+
7782
var errors bool
7883
for {
7984
v, err := res.Next()
@@ -162,6 +167,11 @@ For ERROR entries the error will also be printed to stderr.
162167
},
163168
Marshalers: oldCmds.MarshalerMap{
164169
oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) {
170+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
171+
if err != nil {
172+
return nil, err
173+
}
174+
165175
v, err := unwrapOutput(res.Output())
166176
if err != nil {
167177
return nil, err
@@ -198,6 +208,13 @@ var dupsFileStore = &oldCmds.Command{
198208
return
199209
}
200210

211+
h, err := NewCidBaseHandlerLegacy(req).Proc()
212+
if err != nil {
213+
res.SetError(err, cmdkit.ErrNormal)
214+
return
215+
}
216+
enc := h.Encoder()
217+
201218
out := make(chan interface{}, 128)
202219
res.SetOutput((<-chan interface{})(out))
203220

@@ -214,7 +231,7 @@ var dupsFileStore = &oldCmds.Command{
214231
}
215232
if have {
216233
select {
217-
case out <- &RefWrapper{Ref: cid.String()}:
234+
case out <- &RefWrapper{Ref: enc.Encode(cid)}:
218235
case <-req.Context().Done():
219236
return
220237
}

core/commands/ls.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ import (
1717
merkledag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
1818
blockservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
1919

20+
apicid "gx/ipfs/QmNWQygwYxgz3QzXG2ytTkrHkZ4HnnSh94ASox3JjktFcR/go-cidutil/apicid"
2021
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
2122
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
2223
offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
2324
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
2425
)
2526

2627
type LsLink struct {
27-
Name, Hash string
28-
Size uint64
29-
Type unixfspb.Data_DataType
28+
Name string
29+
Hash apicid.Hash
30+
Size uint64
31+
Type unixfspb.Data_DataType
3032
}
3133

3234
type LsObject struct {
@@ -160,7 +162,7 @@ The JSON output contains type information.
160162
}
161163
output[i].Links[j] = LsLink{
162164
Name: link.Name,
163-
Hash: link.Cid.String(),
165+
Hash: apicid.FromCid(link.Cid),
164166
Size: link.Size,
165167
Type: t,
166168
}
@@ -171,6 +173,10 @@ The JSON output contains type information.
171173
},
172174
Marshalers: cmds.MarshalerMap{
173175
cmds.Text: func(res cmds.Response) (io.Reader, error) {
176+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
177+
if err != nil {
178+
return nil, err
179+
}
174180

175181
v, err := unwrapOutput(res.Output())
176182
if err != nil {

core/commands/pin.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
pin "github.com/ipfs/go-ipfs/pin"
1515
uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
1616

17+
apicid "gx/ipfs/QmNWQygwYxgz3QzXG2ytTkrHkZ4HnnSh94ASox3JjktFcR/go-cidutil/apicid"
1718
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
1819
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
1920
"gx/ipfs/QmVkMRSkXrpjqrroEXWuYBvDBnXCdMMY6gsKicBGVGUqKT/go-verifcid"
@@ -39,11 +40,11 @@ var PinCmd = &cmds.Command{
3940
}
4041

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

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

@@ -84,7 +85,7 @@ var addPinCmd = &cmds.Command{
8485
res.SetError(err, cmdkit.ErrNormal)
8586
return
8687
}
87-
res.SetOutput(&AddPinOutput{Pins: cidsToStrings(added)})
88+
res.SetOutput(&AddPinOutput{Pins: toAPICids(added)})
8889
return
8990
}
9091

@@ -117,7 +118,7 @@ var addPinCmd = &cmds.Command{
117118
if pv := v.Value(); pv != 0 {
118119
out <- &AddPinOutput{Progress: v.Value()}
119120
}
120-
out <- &AddPinOutput{Pins: cidsToStrings(val.pins)}
121+
out <- &AddPinOutput{Pins: toAPICids(val.pins)}
121122
return
122123
case <-ticker.C:
123124
out <- &AddPinOutput{Progress: v.Value()}
@@ -130,12 +131,16 @@ var addPinCmd = &cmds.Command{
130131
},
131132
Marshalers: cmds.MarshalerMap{
132133
cmds.Text: func(res cmds.Response) (io.Reader, error) {
134+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
135+
if err != nil {
136+
return nil, err
137+
}
133138
v, err := unwrapOutput(res.Output())
134139
if err != nil {
135140
return nil, err
136141
}
137142

138-
var added []string
143+
var added []apicid.Hash
139144

140145
switch out := v.(type) {
141146
case *AddPinOutput:
@@ -206,10 +211,15 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
206211
return
207212
}
208213

209-
res.SetOutput(&PinOutput{cidsToStrings(removed)})
214+
res.SetOutput(&PinOutput{toAPICids(removed)})
210215
},
211216
Marshalers: cmds.MarshalerMap{
212217
cmds.Text: func(res cmds.Response) (io.Reader, error) {
218+
_, err := NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
219+
if err != nil {
220+
return nil, err
221+
}
222+
213223
v, err := unwrapOutput(res.Output())
214224
if err != nil {
215225
return nil, err
@@ -345,6 +355,10 @@ Example:
345355
},
346356
}
347357

358+
type UpdatePinOutput struct {
359+
Pins []string // really paths
360+
}
361+
348362
var updatePinCmd = &cmds.Command{
349363
Helptext: cmdkit.HelpText{
350364
Tagline: "Update a recursive pin",
@@ -362,7 +376,7 @@ new pin and removing the old one.
362376
Options: []cmdkit.Option{
363377
cmdkit.BoolOption("unpin", "Remove the old pin.").WithDefault(true),
364378
},
365-
Type: PinOutput{},
379+
Type: UpdatePinOutput{},
366380
Run: func(req cmds.Request, res cmds.Response) {
367381
n, err := req.InvocContext().GetNode()
368382
if err != nil {
@@ -417,15 +431,15 @@ new pin and removing the old one.
417431
return
418432
}
419433

420-
res.SetOutput(&PinOutput{Pins: []string{from.String(), to.String()}})
434+
res.SetOutput(&UpdatePinOutput{Pins: []string{from.String(), to.String()}})
421435
},
422436
Marshalers: cmds.MarshalerMap{
423437
cmds.Text: func(res cmds.Response) (io.Reader, error) {
424438
v, err := unwrapOutput(res.Output())
425439
if err != nil {
426440
return nil, err
427441
}
428-
added, ok := v.(*PinOutput)
442+
added, ok := v.(*UpdatePinOutput)
429443
if !ok {
430444
return nil, e.TypeErr(added, v)
431445
}
@@ -680,10 +694,10 @@ func (r PinVerifyRes) Format(out io.Writer) {
680694
}
681695
}
682696

683-
func cidsToStrings(cs []cid.Cid) []string {
684-
out := make([]string, 0, len(cs))
697+
func toAPICids(cs []cid.Cid) []apicid.Hash {
698+
out := make([]apicid.Hash, 0, len(cs))
685699
for _, c := range cs {
686-
out = append(out, c.String())
700+
out = append(out, apicid.FromCid(c))
687701
}
688702
return out
689703
}

0 commit comments

Comments
 (0)