Skip to content

Commit a8830da

Browse files
committed
Add global --cid-base option and enable it for most commands.
This does it on ther server side for most commands. This also adds a global --output-cidv1 option. License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent b056a3d commit a8830da

21 files changed

+532
-151
lines changed

core/commands/add.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1212

1313
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
14+
//cidenc "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/cidenc"
1415
files "gx/ipfs/QmZMWMvWMVKCbHetJ4RgndbuEF1io2UpUxwQwtNjtYPzSC/go-ipfs-files"
1516
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
1617
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
@@ -167,6 +168,11 @@ You can now check what blocks have been created by:
167168
return fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr))
168169
}
169170

171+
enc, err := cmdenv.ProcCidBase(req)
172+
if err != nil {
173+
return err
174+
}
175+
170176
events := make(chan interface{}, adderOutChanSize)
171177

172178
opts := []options.UnixfsAddOption{
@@ -212,9 +218,13 @@ You can now check what blocks have been created by:
212218
_, err = api.Unixfs().Add(req.Context, req.Files, opts...)
213219
}()
214220

215-
err = res.Emit(events)
216-
if err != nil {
217-
return err
221+
for event := range events {
222+
output := event.(*coreiface.AddEvent)
223+
output.Hash, _ = enc.Recode(output.Hash)
224+
err = res.Emit(event)
225+
if err != nil {
226+
return err
227+
}
218228
}
219229

220230
return <-errCh

core/commands/cmdenv/cidbase.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmdenv
2+
3+
import (
4+
cidenc "gx/ipfs/QmVjZoEZg2oxXGFGjbD28x3gGN6ALHAW6BN2LKRUcaJ21i/go-cidutil/cidenc"
5+
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
6+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
7+
mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase"
8+
)
9+
10+
var OptionCidBase = cmdkit.StringOption("cid-base", "Multi-base encoding used for version 1 CIDs in output.")
11+
var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.")
12+
13+
// ProcCidBase processes the `cid-base` and `output-cidv1` options and
14+
// returns a encoder to use based on those parameters.
15+
func ProcCidBase(req *cmds.Request) (cidenc.Encoder, error) {
16+
base, _ := req.Options["cid-base"].(string)
17+
upgrade, upgradeDefined := req.Options["output-cidv1"].(bool)
18+
19+
var e cidenc.Encoder = cidenc.Default
20+
21+
if base != "" {
22+
var err error
23+
e.Base, err = mbase.EncoderByName(base)
24+
if err != nil {
25+
return e, err
26+
}
27+
e.Upgrade = true
28+
}
29+
30+
if upgradeDefined {
31+
e.Upgrade = upgrade
32+
}
33+
34+
return e, nil
35+
}
36+
37+
func CidBaseDefined(req *cmds.Request) bool {
38+
base, _ := req.Options["cid-base"].(string)
39+
return base != ""
40+
}
41+
42+
// EnableCidBaseGlobal is a prerun function...
43+
func EnableCidBaseGlobal(req *cmds.Request, env cmds.Environment) error {
44+
enc, err := ProcCidBase(req)
45+
if err != nil {
46+
return err
47+
}
48+
cidenc.Default = enc
49+
return nil
50+
}

core/commands/files.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
1818
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1919
bservice "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
20+
cidenc "gx/ipfs/QmVjZoEZg2oxXGFGjbD28x3gGN6ALHAW6BN2LKRUcaJ21i/go-cidutil/cidenc"
2021
ft "gx/ipfs/QmXAFxWtAB9YAMzMy9op6m95hWYu2CC5rmTsijkYL12Kvu/go-unixfs"
2122
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
2223
mfs "gx/ipfs/QmZw3dco7GvZkuZ9pEHTHJ2DNXFxTtquraF3d2JYa5vP6q/go-mfs"
@@ -136,6 +137,11 @@ var filesStatCmd = &cmds.Command{
136137

137138
withLocal, _ := req.Options[filesWithLocalOptionName].(bool)
138139

140+
enc, err := cmdenv.ProcCidBase(req)
141+
if err != nil {
142+
return err
143+
}
144+
139145
var dagserv ipld.DAGService
140146
if withLocal {
141147
// an offline DAGService will not fetch from the network
@@ -152,7 +158,7 @@ var filesStatCmd = &cmds.Command{
152158
return err
153159
}
154160

155-
o, err := statNode(nd)
161+
o, err := statNode(nd, enc)
156162
if err != nil {
157163
return err
158164
}
@@ -217,7 +223,7 @@ func statGetFormatOptions(req *cmds.Request) (string, error) {
217223
}
218224
}
219225

220-
func statNode(nd ipld.Node) (*statOutput, error) {
226+
func statNode(nd ipld.Node, enc cidenc.Interface) (*statOutput, error) {
221227
c := nd.Cid()
222228

223229
cumulsize, err := nd.Size()
@@ -243,15 +249,15 @@ func statNode(nd ipld.Node) (*statOutput, error) {
243249
}
244250

245251
return &statOutput{
246-
Hash: c.String(),
252+
Hash: enc.Encode(c),
247253
Blocks: len(nd.Links()),
248254
Size: d.FileSize(),
249255
CumulativeSize: cumulsize,
250256
Type: ndtype,
251257
}, nil
252258
case *dag.RawNode:
253259
return &statOutput{
254-
Hash: c.String(),
260+
Hash: enc.Encode(c),
255261
Blocks: 0,
256262
Size: cumulsize,
257263
CumulativeSize: cumulsize,
@@ -433,6 +439,11 @@ Examples:
433439

434440
long, _ := req.Options[longOptionName].(bool)
435441

442+
enc, err := cmdenv.ProcCidBase(req)
443+
if err != nil {
444+
return err
445+
}
446+
436447
switch fsn := fsn.(type) {
437448
case *mfs.Directory:
438449
if !long {
@@ -470,7 +481,7 @@ Examples:
470481
if err != nil {
471482
return err
472483
}
473-
out.Entries[0].Hash = nd.Cid().String()
484+
out.Entries[0].Hash = enc.Encode(nd.Cid())
474485
}
475486
return cmds.EmitOnce(res, out)
476487
default:

core/commands/filestore.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The output is:
5050
Options: []cmdkit.Option{
5151
cmdkit.BoolOption(fileOrderOptionName, "sort the results based on the path of the backing file"),
5252
},
53+
PreRun: cmdenv.EnableCidBaseGlobal,
5354
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
5455
_, fs, err := getFilestore(env)
5556
if err != nil {
@@ -121,6 +122,7 @@ For ERROR entries the error will also be printed to stderr.
121122
Options: []cmdkit.Option{
122123
cmdkit.BoolOption(fileOrderOptionName, "verify the objects based on the order of the backing file"),
123124
},
125+
PreRun: cmdenv.EnableCidBaseGlobal,
124126
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
125127
_, fs, err := getFilestore(env)
126128
if err != nil {
@@ -184,6 +186,12 @@ var dupsFileStore = &cmds.Command{
184186
if err != nil {
185187
return err
186188
}
189+
190+
enc, err := cmdenv.ProcCidBase(req)
191+
if err != nil {
192+
return err
193+
}
194+
187195
ch, err := fs.FileManager().AllKeysChan(req.Context)
188196
if err != nil {
189197
return err
@@ -195,7 +203,7 @@ var dupsFileStore = &cmds.Command{
195203
return res.Emit(&RefWrapper{Err: err.Error()})
196204
}
197205
if have {
198-
if err := res.Emit(&RefWrapper{Ref: cid.String()}); err != nil {
206+
if err := res.Emit(&RefWrapper{Ref: enc.Encode(cid)}); err != nil {
199207
return err
200208
}
201209
}

core/commands/ls.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1313
blockservice "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
14+
cidenc "gx/ipfs/QmVjZoEZg2oxXGFGjbD28x3gGN6ALHAW6BN2LKRUcaJ21i/go-cidutil/cidenc"
1415
unixfs "gx/ipfs/QmXAFxWtAB9YAMzMy9op6m95hWYu2CC5rmTsijkYL12Kvu/go-unixfs"
1516
uio "gx/ipfs/QmXAFxWtAB9YAMzMy9op6m95hWYu2CC5rmTsijkYL12Kvu/go-unixfs/io"
1617
unixfspb "gx/ipfs/QmXAFxWtAB9YAMzMy9op6m95hWYu2CC5rmTsijkYL12Kvu/go-unixfs/pb"
@@ -91,9 +92,13 @@ The JSON output contains type information.
9192
if err != nil {
9293
return err
9394
}
94-
9595
paths := req.Arguments
9696

97+
enc, err := cmdenv.ProcCidBase(req)
98+
if err != nil {
99+
return err
100+
}
101+
97102
var dagnodes []ipld.Node
98103
for _, fpath := range paths {
99104
p, err := iface.ParsePath(fpath)
@@ -131,7 +136,7 @@ The JSON output contains type information.
131136
}
132137
outputLinks := make([]LsLink, len(links))
133138
for j, link := range links {
134-
lsLink, err := makeLsLink(req, dserv, resolve, link)
139+
lsLink, err := makeLsLink(req, dserv, resolve, link, enc)
135140
if err != nil {
136141
return err
137142
}
@@ -165,7 +170,7 @@ The JSON output contains type information.
165170
return linkResult.Err
166171
}
167172
link := linkResult.Link
168-
lsLink, err := makeLsLink(req, dserv, resolve, link)
173+
lsLink, err := makeLsLink(req, dserv, resolve, link, enc)
169174
if err != nil {
170175
return err
171176
}
@@ -224,7 +229,7 @@ func makeDagNodeLinkResults(req *cmds.Request, dagnode ipld.Node) <-chan unixfs.
224229
return linkResults
225230
}
226231

227-
func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ipld.Link) (*LsLink, error) {
232+
func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ipld.Link, enc cidenc.Interface) (*LsLink, error) {
228233
t := unixfspb.Data_DataType(-1)
229234

230235
switch link.Cid.Type() {
@@ -250,7 +255,7 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
250255
}
251256
return &LsLink{
252257
Name: link.Name,
253-
Hash: link.Cid.String(),
258+
Hash: enc.Encode(link.Cid),
254259
Size: link.Size,
255260
Type: t,
256261
}, nil

0 commit comments

Comments
 (0)