Skip to content

Commit

Permalink
Add global option to specify the multibase encoding.
Browse files Browse the repository at this point in the history
Use it in 'ipfs add' and 'ipfs ls'.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Aug 3, 2018
1 parent 96bce0a commit 674174a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
8 changes: 8 additions & 0 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
Expand Down
25 changes: 24 additions & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +21,8 @@ import (
var log = logging.Logger("core/commands")

const (
ApiOption = "api"
ApiOption = "api"
MbaseOption = "mbase"
)

var Root = &cmds.Command{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
15 changes: 9 additions & 6 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -91,6 +93,7 @@ type Adder struct {
unlocker bstore.Unlocker
tempRoot *cid.Cid
Prefix *cid.Prefix
Base mbase.Encoder
liveNodes uint64
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -554,15 +557,15 @@ 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 {
return nil, err
}

output := &Object{
Hash: c.String(),
Hash: c.Encode(base),
Size: strconv.FormatUint(s, 10),
Links: make([]Link, len(dagnode.Links())),
}
Expand Down

0 comments on commit 674174a

Please sign in to comment.