Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global option to specify the multibase encoding. #5464

Closed
wants to merge 13 commits into from
Closed
18 changes: 12 additions & 6 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs"
cidutil "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil"
bstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore"
)

Expand Down Expand Up @@ -367,6 +367,11 @@ You can now check what blocks have been created by:
log.Warning("cannot determine size of input file")
}

_, err := cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()
if err != nil {
return err
}

progressBar := func(wait chan struct{}) {
defer close(wait)

Expand Down Expand Up @@ -402,8 +407,9 @@ You can now check what blocks have been created by:
break LOOP
}
output := out.(*coreunix.AddedObject)
if len(output.Hash) > 0 {
lastHash = output.Hash
hash := output.Hash.String()
if len(hash) > 0 {
lastHash = hash
if quieter {
continue
}
Expand All @@ -413,9 +419,9 @@ You can now check what blocks have been created by:
fmt.Fprintf(os.Stderr, "\033[2K\r")
}
if quiet {
fmt.Fprintf(os.Stdout, "%s\n", output.Hash)
fmt.Fprintf(os.Stdout, "%s\n", hash)
} else {
fmt.Fprintf(os.Stdout, "added %s %s\n", output.Hash, output.Name)
fmt.Fprintf(os.Stdout, "added %s %s\n", hash, output.Name)
}

} else {
Expand Down
119 changes: 119 additions & 0 deletions core/commands/cmdenv/cidbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cmdenv

import (
oldcmds "github.com/ipfs/go-ipfs/commands"

cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
cidenc "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/cidenc"
mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase"
)

var OptionCidBase = cmdkit.StringOption("cid-base", "mbase", "Multi-base to use to encode version 1 CIDs in output.")
kevina marked this conversation as resolved.
Show resolved Hide resolved
var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.")

// CidBaseHandler is a helper class to process the `--cid-base` and
// `--output-cidv1` options. In the future it may also be used to
// process relevant config settings.
//
// Several of its methods return the class itself in order to allow
// easy chaining, a typical usage would be
// `cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()` or
// `cmdenv.NewCidBaseHandlerLegacy(req).Proc()`.
type CidBaseHandler struct {
base string
upgrade bool
upgradeDefined bool
args []string
enc *cidenc.Encoder
}

// NewCidBaseHandler created a a CidBaseHandler from a request
kevina marked this conversation as resolved.
Show resolved Hide resolved
func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler {
h := &CidBaseHandler{}
h.base, _ = req.Options["cid-base"].(string)
h.upgrade, h.upgradeDefined = req.Options["output-cidv1"].(bool)
h.args = req.Arguments
return h
}

// NewCidBaseHandlerLegacy created a a CidBaseHandler from a request
kevina marked this conversation as resolved.
Show resolved Hide resolved
// using the old commands library
func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler {
h := &CidBaseHandler{}
h.base, _, _ = req.Option("cid-base").String()
h.upgrade, h.upgradeDefined, _ = req.Option("output-cidv1").Bool()
h.args = req.Arguments()
return h
}

// UseGlobal enables the use of the global default. This is somewhat
// of a hack and should be used with care. In particular it should
// only be used on the client side and not the server side.
func (h *CidBaseHandler) UseGlobal() *CidBaseHandler {
h.enc = &cidenc.Default
return h
}

// Proc processes the `--cid-base` and `--output-cidv1` options. If
// UseGlobal was enabled it will change the value of the global
kevina marked this conversation as resolved.
Show resolved Hide resolved
// default.
func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) {
kevina marked this conversation as resolved.
Show resolved Hide resolved
var e cidenc.Encoder = cidenc.Default
if h.base != "" {
var err error
e.Base, err = mbase.EncoderByName(h.base)
if err != nil {
return h, err
}
}

e.Upgrade = h.upgrade
if h.base != "" && !h.upgradeDefined {
e.Upgrade = true
}

if h.enc == nil {
h.enc = &cidenc.Encoder{}
}
*h.enc = e
return h, nil
}

// Encoder returns a copy of the underlying Encoder
func (h *CidBaseHandler) Encoder() cidenc.Encoder {
return *h.enc
}

// EncoderFromPath returns a a new Encoder that will format Cid like
kevina marked this conversation as resolved.
Show resolved Hide resolved
// the one in the path if the `--cid-base` option is not used. (If
// the `--cid-base` is used then a copy of the base encoder will be
// returned.) In particular: if the path contains a version 1 CID
// then all CIDs will be outputting using the same multibase. if the
// path contains a version 0 CID then version 0 CIDs will be outputted
// as is and version1 cids will use the multibase from the base
kevina marked this conversation as resolved.
Show resolved Hide resolved
// encoder
func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder {
if h.base == "" {
enc, _ := cidenc.FromPath(*h.enc, p)
return enc
} else {
return *h.enc
}
}

// EncoderWithOverride returns a new encoder that will use the setting
// from the base encoder unless it is a CID that was specified on the
// command line and the `--cid-base` option was not used. (If the
// `--cid-base` is used then a copy of the base encoder will be
// returned.) In that case the same CID string as specified on the
// command line will be used.
func (h *CidBaseHandler) EncoderWithOverride() cidenc.Interface {
if h.base == "" {
enc := cidenc.NewOverride(*h.enc)
enc.Add(h.args...)
return enc
} else {
return *h.enc
}
}
2 changes: 1 addition & 1 deletion core/commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ The resolver can recursively resolve:
cmdkit.BoolOption("recursive", "r", "Resolve until the result is not a DNS link."),
},
Run: func(req cmds.Request, res cmds.Response) {

kevina marked this conversation as resolved.
Show resolved Hide resolved
recursive, _, _ := req.Option("recursive").Bool()
name := req.Arguments()[0]
resolver := namesys.NewDNSResolver()
Expand All @@ -77,6 +76,7 @@ The resolver can recursively resolve:
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {

v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
Expand Down
23 changes: 17 additions & 6 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid"
)

var flog = logging.Logger("cmds/files")
Expand Down Expand Up @@ -77,7 +78,7 @@ var hashOption = cmdkit.StringOption("hash", "Hash function to use. Will set Cid
var errFormat = errors.New("format was set by multiple options. Only one format option is allowed")

type statOutput struct {
Hash string
Hash apicid.Hash
Size uint64
CumulativeSize uint64
Blocks int
Expand Down Expand Up @@ -162,13 +163,18 @@ var filesStatCmd = &cmds.Command{
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
_, err := cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()
if err != nil {
return err
}

out, ok := v.(*statOutput)
if !ok {
return e.TypeErr(out, v)
}

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

return &statOutput{
Hash: c.String(),
Hash: apicid.FromCid(c),
Blocks: len(nd.Links()),
Size: d.FileSize(),
CumulativeSize: cumulsize,
Type: ndtype,
}, nil
case *dag.RawNode:
return &statOutput{
Hash: c.String(),
Hash: apicid.FromCid(c),
Blocks: 0,
Size: cumulsize,
CumulativeSize: cumulsize,
Expand Down Expand Up @@ -476,7 +482,7 @@ Examples:
res.SetError(err, cmdkit.ErrNormal)
return
}
out.Entries[0].Hash = nd.Cid().String()
out.Entries[0].Hash = apicid.FromCid(nd.Cid())
}
res.SetOutput(out)
return
Expand All @@ -486,6 +492,11 @@ Examples:
},
Marshalers: oldcmds.MarshalerMap{
oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
_, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
if err != nil {
return nil, err
}

v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
Expand Down
19 changes: 18 additions & 1 deletion core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ The output is:
},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
_, err := cmdenv.NewCidBaseHandler(res.Request()).UseGlobal().Proc()
if err != nil {
return err
}

var errors bool
for {
v, err := res.Next()
Expand Down Expand Up @@ -162,6 +167,11 @@ For ERROR entries the error will also be printed to stderr.
},
Marshalers: oldCmds.MarshalerMap{
oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) {
_, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
if err != nil {
return nil, err
}

v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
Expand Down Expand Up @@ -198,6 +208,13 @@ var dupsFileStore = &oldCmds.Command{
return
}

h, err := cmdenv.NewCidBaseHandlerLegacy(req).Proc()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
enc := h.Encoder()

out := make(chan interface{}, 128)
res.SetOutput((<-chan interface{})(out))

Expand All @@ -214,7 +231,7 @@ var dupsFileStore = &oldCmds.Command{
}
if have {
select {
case out <- &RefWrapper{Ref: cid.String()}:
case out <- &RefWrapper{Ref: enc.Encode(cid)}:
case <-req.Context().Done():
return
}
Expand Down
15 changes: 11 additions & 4 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e"
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
Expand All @@ -21,12 +22,14 @@ import (
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid"
)

type LsLink struct {
Name, Hash string
Size uint64
Type unixfspb.Data_DataType
Name string
Hash apicid.Hash
Size uint64
Type unixfspb.Data_DataType
}

type LsObject struct {
Expand Down Expand Up @@ -160,7 +163,7 @@ The JSON output contains type information.
}
output[i].Links[j] = LsLink{
Name: link.Name,
Hash: link.Cid.String(),
Hash: apicid.FromCid(link.Cid),
Size: link.Size,
Type: t,
}
Expand All @@ -171,6 +174,10 @@ The JSON output contains type information.
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
_, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc()
if err != nil {
return nil, err
}

v, err := unwrapOutput(res.Output())
if err != nil {
Expand Down
Loading