Skip to content

Commit 2f75218

Browse files
committed
Add support for global --cid-base option to more commands.
Other related changes. License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 47f6949 commit 2f75218

18 files changed

+374
-174
lines changed

cmd/ipfs/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
oldcmds "github.com/ipfs/go-ipfs/commands"
2020
core "github.com/ipfs/go-ipfs/core"
2121
corecmds "github.com/ipfs/go-ipfs/core/commands"
22+
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
2223
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
2324
loader "github.com/ipfs/go-ipfs/plugin/loader"
2425
repo "github.com/ipfs/go-ipfs/repo"
@@ -28,6 +29,7 @@ import (
2829
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
2930
ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
3031
madns "gx/ipfs/QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m/go-multiaddr-dns"
32+
cidenc "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/cidenc"
3133
osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
3234
"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
3335
"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds/cli"
@@ -116,6 +118,12 @@ func mainRet() int {
116118
}
117119
log.Debugf("config path is %s", repoPath)
118120

121+
enc, err := cmdenv.ProcCidBase(req)
122+
if err != nil {
123+
return nil, err
124+
}
125+
cidenc.Default = enc
126+
119127
// this sets up the function that will initialize the node
120128
// this is so that we can construct the node lazily.
121129
return &oldcmds.Context{

core/commands/add.go

-5
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,6 @@ You can now check what blocks have been created by:
225225
outChan := make(chan interface{})
226226
req := res.Request()
227227

228-
err := cmdenv.ProcCidBaseClientSide(req)
229-
if err != nil {
230-
return err
231-
}
232-
233228
sizeFile, ok := req.Files.(files.SizeFile)
234229
if ok {
235230
// Could be slow.

core/commands/cmdenv/cidbase.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ func ProcCidBase(req *cmds.Request) (cidenc.Encoder, error) {
2424
if err != nil {
2525
return e, err
2626
}
27-
if !upgradeDefined {
28-
e.Upgrade = true
29-
}
27+
e.Upgrade = true
3028
}
3129

3230
if upgradeDefined {
@@ -36,13 +34,7 @@ func ProcCidBase(req *cmds.Request) (cidenc.Encoder, error) {
3634
return e, nil
3735
}
3836

39-
// ProcCidBaseClientSide processes the `cid-base` and `output-cidv1`
40-
// options and sets the default encoder based on those options
41-
func ProcCidBaseClientSide(req *cmds.Request) error {
42-
enc, err := ProcCidBase(req)
43-
if err != nil {
44-
return err
45-
}
46-
cidenc.Default = enc
47-
return nil
37+
func CidBaseDefined(req *cmds.Request) bool {
38+
base, _ := req.Options["cid-base"].(string)
39+
return base != ""
4840
}

core/commands/filestore.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
filestore "github.com/ipfs/go-ipfs/filestore"
1212

1313
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
14+
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
1415
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
1516
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1617
)
@@ -175,6 +176,11 @@ For ERROR entries the error will also be printed to stderr.
175176
Type: filestore.ListRes{},
176177
}
177178

179+
type FilestoreDupsOutput struct {
180+
Ref apicid.Hash
181+
Err string
182+
}
183+
178184
var dupsFileStore = &cmds.Command{
179185
Helptext: cmdkit.HelpText{
180186
Tagline: "List blocks that are both in the filestore and standard block storage.",
@@ -192,19 +198,28 @@ var dupsFileStore = &cmds.Command{
192198
for cid := range ch {
193199
have, err := fs.MainBlockstore().Has(cid)
194200
if err != nil {
195-
return res.Emit(&RefWrapper{Err: err.Error()})
201+
return res.Emit(&FilestoreDupsOutput{Err: err.Error()})
196202
}
197203
if have {
198-
if err := res.Emit(&RefWrapper{Ref: cid.String()}); err != nil {
204+
if err := res.Emit(&FilestoreDupsOutput{Ref: apicid.FromCid(cid)}); err != nil {
199205
return err
200206
}
201207
}
202208
}
203209

204210
return nil
205211
},
206-
Encoders: refsEncoderMap,
207-
Type: RefWrapper{},
212+
Encoders: cmds.EncoderMap{
213+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *FilestoreDupsOutput) error {
214+
if out.Err != "" {
215+
return fmt.Errorf(out.Err)
216+
}
217+
fmt.Fprintln(w, out.Ref)
218+
219+
return nil
220+
}),
221+
},
222+
Type: FilestoreDupsOutput{},
208223
}
209224

210225
func getFilestore(env cmds.Environment) (*core.IpfsNode, *filestore.Filestore, error) {

core/commands/ls.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
uio "gx/ipfs/QmUnHNqhSB1JgzVCxL1Kz3yb4bdyB4q1Z9AD5AUBVmt3fZ/go-unixfs/io"
1515
unixfspb "gx/ipfs/QmUnHNqhSB1JgzVCxL1Kz3yb4bdyB4q1Z9AD5AUBVmt3fZ/go-unixfs/pb"
1616
blockservice "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
17+
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
1718
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
1819
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
1920
merkledag "gx/ipfs/QmcGt25mrjuB2kKW2zhPbXVZNHc4yoTDQ65NA8m6auP2f1/go-merkledag"
@@ -23,9 +24,10 @@ import (
2324

2425
// LsLink contains printable data for a single ipld link in ls output
2526
type LsLink struct {
26-
Name, Hash string
27-
Size uint64
28-
Type unixfspb.Data_DataType
27+
Name string
28+
Hash apicid.Hash
29+
Size uint64
30+
Type unixfspb.Data_DataType
2931
}
3032

3133
// LsObject is an element of LsOutput
@@ -250,7 +252,7 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
250252
}
251253
return &LsLink{
252254
Name: link.Name,
253-
Hash: link.Cid.String(),
255+
Hash: apicid.FromCid(link.Cid),
254256
Size: link.Size,
255257
Type: t,
256258
}, nil

core/commands/pin.go

+31-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1919
bserv "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
20+
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
2021
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
2122
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
2223
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
@@ -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

@@ -92,7 +93,7 @@ var addPinCmd = &cmds.Command{
9293
if err != nil {
9394
return err
9495
}
95-
return cmds.EmitOnce(res, &AddPinOutput{Pins: cidsToStrings(added)})
96+
return cmds.EmitOnce(res, &AddPinOutput{Pins: toAPICids(added)})
9697
}
9798

9899
v := new(dag.ProgressTracker)
@@ -124,7 +125,7 @@ var addPinCmd = &cmds.Command{
124125
return err
125126
}
126127
}
127-
return res.Emit(&AddPinOutput{Pins: cidsToStrings(val.pins)})
128+
return res.Emit(&AddPinOutput{Pins: toAPICids(val.pins)})
128129
case <-ticker.C:
129130
if err := res.Emit(&AddPinOutput{Progress: v.Value()}); err != nil {
130131
return err
@@ -220,7 +221,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
220221
return err
221222
}
222223

223-
return cmds.EmitOnce(res, &PinOutput{cidsToStrings(removed)})
224+
return cmds.EmitOnce(res, &PinOutput{toAPICids(removed)})
224225
},
225226
Encoders: cmds.EncoderMap{
226227
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
@@ -311,7 +312,7 @@ Example:
311312
return err
312313
}
313314

314-
var keys map[string]RefKeyObject
315+
var keys map[apicid.Hash]RefKeyObject
315316

316317
if len(req.Arguments) > 0 {
317318
keys, err = pinLsKeys(req.Context, req.Arguments, typeStr, n, api)
@@ -347,6 +348,10 @@ const (
347348
pinUnpinOptionName = "unpin"
348349
)
349350

351+
type UpdatePinOutput struct {
352+
Pins []string // really paths
353+
}
354+
350355
var updatePinCmd = &cmds.Command{
351356
Helptext: cmdkit.HelpText{
352357
Tagline: "Update a recursive pin",
@@ -364,7 +369,7 @@ new pin and removing the old one.
364369
Options: []cmdkit.Option{
365370
cmdkit.BoolOption(pinUnpinOptionName, "Remove the old pin.").WithDefault(true),
366371
},
367-
Type: PinOutput{},
372+
Type: UpdatePinOutput{},
368373
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
369374
api, err := cmdenv.GetApi(env)
370375
if err != nil {
@@ -388,10 +393,10 @@ new pin and removing the old one.
388393
return err
389394
}
390395

391-
return cmds.EmitOnce(res, &PinOutput{Pins: []string{from.String(), to.String()}})
396+
return cmds.EmitOnce(res, &UpdatePinOutput{Pins: []string{from.String(), to.String()}})
392397
},
393398
Encoders: cmds.EncoderMap{
394-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
399+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *UpdatePinOutput) error {
395400
fmt.Fprintf(w, "updated %s to %s\n", out.Pins[0], out.Pins[1])
396401
return nil
397402
}),
@@ -452,17 +457,17 @@ type RefKeyObject struct {
452457
}
453458

454459
type RefKeyList struct {
455-
Keys map[string]RefKeyObject
460+
Keys map[apicid.Hash]RefKeyObject
456461
}
457462

458-
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[string]RefKeyObject, error) {
463+
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[apicid.Hash]RefKeyObject, error) {
459464

460465
mode, ok := pin.StringToMode(typeStr)
461466
if !ok {
462467
return nil, fmt.Errorf("invalid pin mode '%s'", typeStr)
463468
}
464469

465-
keys := make(map[string]RefKeyObject)
470+
keys := make(map[apicid.Hash]RefKeyObject)
466471

467472
for _, p := range args {
468473
pth, err := iface.ParsePath(p)
@@ -489,21 +494,21 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN
489494
default:
490495
pinType = "indirect through " + pinType
491496
}
492-
keys[c.Cid().String()] = RefKeyObject{
497+
keys[apicid.FromCid(c.Cid())] = RefKeyObject{
493498
Type: pinType,
494499
}
495500
}
496501

497502
return keys, nil
498503
}
499504

500-
func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[string]RefKeyObject, error) {
505+
func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[apicid.Hash]RefKeyObject, error) {
501506

502-
keys := make(map[string]RefKeyObject)
507+
keys := make(map[apicid.Hash]RefKeyObject)
503508

504509
AddToResultKeys := func(keyList []cid.Cid, typeStr string) {
505510
for _, c := range keyList {
506-
keys[c.String()] = RefKeyObject{
511+
keys[apicid.FromCid(c)] = RefKeyObject{
507512
Type: typeStr,
508513
}
509514
}
@@ -531,7 +536,7 @@ func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[string
531536

532537
// PinVerifyRes is the result returned for each pin checked in "pin verify"
533538
type PinVerifyRes struct {
534-
Cid string
539+
Cid apicid.Hash
535540
PinStatus
536541
}
537542

@@ -543,7 +548,7 @@ type PinStatus struct {
543548

544549
// BadNode is used in PinVerifyRes
545550
type BadNode struct {
546-
Cid string
551+
Cid apicid.Hash
547552
Err string
548553
}
549554

@@ -553,7 +558,7 @@ type pinVerifyOpts struct {
553558
}
554559

555560
func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan interface{} {
556-
visited := make(map[string]PinStatus)
561+
visited := make(map[cid.Cid]PinStatus)
557562

558563
bs := n.Blocks.Blockstore()
559564
DAG := dag.NewDAGService(bserv.New(bs, offline.Exchange(bs)))
@@ -562,15 +567,15 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan
562567

563568
var checkPin func(root cid.Cid) PinStatus
564569
checkPin = func(root cid.Cid) PinStatus {
565-
key := root.String()
570+
key := root
566571
if status, ok := visited[key]; ok {
567572
return status
568573
}
569574

570575
if err := verifcid.ValidateCid(root); err != nil {
571576
status := PinStatus{Ok: false}
572577
if opts.explain {
573-
status.BadNodes = []BadNode{BadNode{Cid: key, Err: err.Error()}}
578+
status.BadNodes = []BadNode{BadNode{Cid: apicid.FromCid(key), Err: err.Error()}}
574579
}
575580
visited[key] = status
576581
return status
@@ -580,7 +585,7 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan
580585
if err != nil {
581586
status := PinStatus{Ok: false}
582587
if opts.explain {
583-
status.BadNodes = []BadNode{BadNode{Cid: key, Err: err.Error()}}
588+
status.BadNodes = []BadNode{BadNode{Cid: apicid.FromCid(key), Err: err.Error()}}
584589
}
585590
visited[key] = status
586591
return status
@@ -606,7 +611,7 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan
606611
pinStatus := checkPin(cid)
607612
if !pinStatus.Ok || opts.includeOk {
608613
select {
609-
case out <- &PinVerifyRes{cid.String(), pinStatus}:
614+
case out <- &PinVerifyRes{apicid.FromCid(cid), pinStatus}:
610615
case <-ctx.Done():
611616
return
612617
}
@@ -629,10 +634,10 @@ func (r PinVerifyRes) Format(out io.Writer) {
629634
}
630635
}
631636

632-
func cidsToStrings(cs []cid.Cid) []string {
633-
out := make([]string, 0, len(cs))
637+
func toAPICids(cs []cid.Cid) []apicid.Hash {
638+
out := make([]apicid.Hash, 0, len(cs))
634639
for _, c := range cs {
635-
out = append(out, c.String())
640+
out = append(out, apicid.FromCid(c))
636641
}
637642
return out
638643
}

0 commit comments

Comments
 (0)