From 674174a610c8683c95214106a52bc0125880deb8 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 2 Aug 2018 13:45:43 -0400 Subject: [PATCH] Add global option to specify the multibase encoding. Use it in 'ipfs add' and 'ipfs ls'. License: MIT Signed-off-by: Kevin Atkinson --- core/commands/add.go | 8 ++++++++ core/commands/ls.go | 9 ++++++++- core/commands/root.go | 25 ++++++++++++++++++++++++- core/coreunix/add.go | 15 +++++++++------ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index 3925c81d44c..cee1d331004 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -269,6 +269,13 @@ You can now check what blocks have been created by: return } + baseStr := req.Options[MbaseOption].(string) + base, err := GetMultibase(baseStr, "", "") + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + fileAdder.Out = outChan fileAdder.Chunker = chunker fileAdder.Progress = progress @@ -280,6 +287,7 @@ You can now check what blocks have been created by: fileAdder.RawLeaves = rawblks fileAdder.NoCopy = nocopy fileAdder.Prefix = &prefix + fileAdder.Base = base if hash { md := dagtest.Mock() diff --git a/core/commands/ls.go b/core/commands/ls.go index 332d3b85125..0333b93ba78 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -77,6 +77,13 @@ The JSON output contains type information. return } + baseStr, _, _ := req.Option(MbaseOption).String() + base, err := GetMultibase(baseStr, "", "") + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + dserv := nd.DAG if !resolve { offlineexch := offline.Exchange(nd.Blockstore) @@ -160,7 +167,7 @@ The JSON output contains type information. } output[i].Links[j] = LsLink{ Name: link.Name, - Hash: link.Cid.String(), + Hash: link.Cid.Encode(base), Size: link.Size, Type: t, } diff --git a/core/commands/root.go b/core/commands/root.go index 73c347e96ef..583c5a5249a 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -10,6 +10,7 @@ import ( e "github.com/ipfs/go-ipfs/core/commands/e" ocmd "github.com/ipfs/go-ipfs/core/commands/object" unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs" + path "github.com/ipfs/go-ipfs/path" "gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds" mbase "gx/ipfs/QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR/go-multibase" @@ -20,7 +21,8 @@ import ( var log = logging.Logger("core/commands") const ( - ApiOption = "api" + ApiOption = "api" + MbaseOption = "mbase" ) var Root = &cmds.Command{ @@ -92,6 +94,7 @@ The CLI will exit with one of the following values: cmdkit.BoolOption("h", "Show a short version of the command help text."), cmdkit.BoolOption("local", "L", "Run the command locally, instead of using the daemon."), cmdkit.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"), + cmdkit.StringOption(MbaseOption, "Multi-base to use to encode version 1 CIDs in output."), // global options, added to every command cmds.OptionEncodingType, @@ -219,3 +222,23 @@ func MessageTextMarshaler(res oldcmds.Response) (io.Reader, error) { return strings.NewReader(out.Message), nil } + +// GetMultibase extracts the multibase. If the first argument is +// defined that is used as the multibase. It can be either a single +// letter or a string. If it is not defined attemt to use the same +// base as any CIDs definded in the second path argument. Finally use +// the third argument as the default if it is defined. As a last +// restort use the default base (currently Base58BTC) +func GetMultibase(mbaseStr string, path path.Path, def string) (mbase.Encoder, error) { + baseStr := mbaseStr + if baseStr == "" { + // FIXME: extract from path + } + if baseStr == "" { + baseStr = def + } + if baseStr == "" { + baseStr = "base58btc" + } + return mbase.EncoderByName(baseStr) +} diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 6b464b6e676..b8af76b506a 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -56,6 +56,7 @@ type AddedObject struct { // NewAdder Returns a new Adder used for a file add operation. func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld.DAGService) (*Adder, error) { + b, _ := mbase.NewEncoder(mbase.Base58BTC) return &Adder{ ctx: ctx, pinning: p, @@ -67,6 +68,7 @@ func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld Trickle: false, Wrap: false, Chunker: "", + Base: b, }, nil } @@ -91,6 +93,7 @@ type Adder struct { unlocker bstore.Unlocker tempRoot *cid.Cid Prefix *cid.Prefix + Base mbase.Encoder liveNodes uint64 } @@ -277,7 +280,7 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error { return err } - return outputDagnode(adder.Out, path, nd) + return outputDagnode(adder.Out, path, nd, adder.Base) default: return fmt.Errorf("unrecognized fsn type: %#v", fsn) } @@ -399,7 +402,7 @@ func (adder *Adder) addNode(node ipld.Node, path string) error { } if !adder.Silent { - return outputDagnode(adder.Out, path, node) + return outputDagnode(adder.Out, path, node, adder.Base) } return nil } @@ -534,12 +537,12 @@ func (adder *Adder) maybePauseForGC() error { } // outputDagnode sends dagnode info over the output channel -func outputDagnode(out chan interface{}, name string, dn ipld.Node) error { +func outputDagnode(out chan interface{}, name string, dn ipld.Node, base mbase.Encoder) error { if out == nil { return nil } - o, err := getOutput(dn) + o, err := getOutput(dn, base) if err != nil { return err } @@ -554,7 +557,7 @@ func outputDagnode(out chan interface{}, name string, dn ipld.Node) error { } // from core/commands/object.go -func getOutput(dagnode ipld.Node) (*Object, error) { +func getOutput(dagnode ipld.Node, base mbase.Encoder) (*Object, error) { c := dagnode.Cid() s, err := dagnode.Size() if err != nil { @@ -562,7 +565,7 @@ func getOutput(dagnode ipld.Node) (*Object, error) { } output := &Object{ - Hash: c.String(), + Hash: c.Encode(base), Size: strconv.FormatUint(s, 10), Links: make([]Link, len(dagnode.Links())), }