提交 674174a6 作者: Kevin Atkinson

Add global option to specify the multibase encoding.

Use it in 'ipfs add' and 'ipfs ls'.

License: MIT
Signed-off-by: 's avatarKevin Atkinson <k@kevina.org>
上级 96bce0a4
...@@ -269,6 +269,13 @@ You can now check what blocks have been created by: ...@@ -269,6 +269,13 @@ You can now check what blocks have been created by:
return return
} }
baseStr := req.Options[MbaseOption].(string)
base, err := GetMultibase(baseStr, "", "")
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
fileAdder.Out = outChan fileAdder.Out = outChan
fileAdder.Chunker = chunker fileAdder.Chunker = chunker
fileAdder.Progress = progress fileAdder.Progress = progress
...@@ -280,6 +287,7 @@ You can now check what blocks have been created by: ...@@ -280,6 +287,7 @@ You can now check what blocks have been created by:
fileAdder.RawLeaves = rawblks fileAdder.RawLeaves = rawblks
fileAdder.NoCopy = nocopy fileAdder.NoCopy = nocopy
fileAdder.Prefix = &prefix fileAdder.Prefix = &prefix
fileAdder.Base = base
if hash { if hash {
md := dagtest.Mock() md := dagtest.Mock()
......
...@@ -77,6 +77,13 @@ The JSON output contains type information. ...@@ -77,6 +77,13 @@ The JSON output contains type information.
return return
} }
baseStr, _, _ := req.Option(MbaseOption).String()
base, err := GetMultibase(baseStr, "", "")
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
dserv := nd.DAG dserv := nd.DAG
if !resolve { if !resolve {
offlineexch := offline.Exchange(nd.Blockstore) offlineexch := offline.Exchange(nd.Blockstore)
...@@ -160,7 +167,7 @@ The JSON output contains type information. ...@@ -160,7 +167,7 @@ The JSON output contains type information.
} }
output[i].Links[j] = LsLink{ output[i].Links[j] = LsLink{
Name: link.Name, Name: link.Name,
Hash: link.Cid.String(), Hash: link.Cid.Encode(base),
Size: link.Size, Size: link.Size,
Type: t, Type: t,
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
e "github.com/ipfs/go-ipfs/core/commands/e" e "github.com/ipfs/go-ipfs/core/commands/e"
ocmd "github.com/ipfs/go-ipfs/core/commands/object" ocmd "github.com/ipfs/go-ipfs/core/commands/object"
unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs" unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs"
path "github.com/ipfs/go-ipfs/path"
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds" "gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
mbase "gx/ipfs/QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR/go-multibase" mbase "gx/ipfs/QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR/go-multibase"
...@@ -20,7 +21,8 @@ import ( ...@@ -20,7 +21,8 @@ import (
var log = logging.Logger("core/commands") var log = logging.Logger("core/commands")
const ( const (
ApiOption = "api" ApiOption = "api"
MbaseOption = "mbase"
) )
var Root = &cmds.Command{ var Root = &cmds.Command{
...@@ -92,6 +94,7 @@ The CLI will exit with one of the following values: ...@@ -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("h", "Show a short version of the command help text."),
cmdkit.BoolOption("local", "L", "Run the command locally, instead of using the daemon."), 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(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 // global options, added to every command
cmds.OptionEncodingType, cmds.OptionEncodingType,
...@@ -219,3 +222,23 @@ func MessageTextMarshaler(res oldcmds.Response) (io.Reader, error) { ...@@ -219,3 +222,23 @@ func MessageTextMarshaler(res oldcmds.Response) (io.Reader, error) {
return strings.NewReader(out.Message), nil 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)
}
...@@ -56,6 +56,7 @@ type AddedObject struct { ...@@ -56,6 +56,7 @@ type AddedObject struct {
// NewAdder Returns a new Adder used for a file add operation. // 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) { func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld.DAGService) (*Adder, error) {
b, _ := mbase.NewEncoder(mbase.Base58BTC)
return &Adder{ return &Adder{
ctx: ctx, ctx: ctx,
pinning: p, pinning: p,
...@@ -67,6 +68,7 @@ func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld ...@@ -67,6 +68,7 @@ func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds ipld
Trickle: false, Trickle: false,
Wrap: false, Wrap: false,
Chunker: "", Chunker: "",
Base: b,
}, nil }, nil
} }
...@@ -91,6 +93,7 @@ type Adder struct { ...@@ -91,6 +93,7 @@ type Adder struct {
unlocker bstore.Unlocker unlocker bstore.Unlocker
tempRoot *cid.Cid tempRoot *cid.Cid
Prefix *cid.Prefix Prefix *cid.Prefix
Base mbase.Encoder
liveNodes uint64 liveNodes uint64
} }
...@@ -277,7 +280,7 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error { ...@@ -277,7 +280,7 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
return err return err
} }
return outputDagnode(adder.Out, path, nd) return outputDagnode(adder.Out, path, nd, adder.Base)
default: default:
return fmt.Errorf("unrecognized fsn type: %#v", fsn) return fmt.Errorf("unrecognized fsn type: %#v", fsn)
} }
...@@ -399,7 +402,7 @@ func (adder *Adder) addNode(node ipld.Node, path string) error { ...@@ -399,7 +402,7 @@ func (adder *Adder) addNode(node ipld.Node, path string) error {
} }
if !adder.Silent { if !adder.Silent {
return outputDagnode(adder.Out, path, node) return outputDagnode(adder.Out, path, node, adder.Base)
} }
return nil return nil
} }
...@@ -534,12 +537,12 @@ func (adder *Adder) maybePauseForGC() error { ...@@ -534,12 +537,12 @@ func (adder *Adder) maybePauseForGC() error {
} }
// outputDagnode sends dagnode info over the output channel // 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 { if out == nil {
return nil return nil
} }
o, err := getOutput(dn) o, err := getOutput(dn, base)
if err != nil { if err != nil {
return err return err
} }
...@@ -554,7 +557,7 @@ func outputDagnode(out chan interface{}, name string, dn ipld.Node) error { ...@@ -554,7 +557,7 @@ func outputDagnode(out chan interface{}, name string, dn ipld.Node) error {
} }
// from core/commands/object.go // 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() c := dagnode.Cid()
s, err := dagnode.Size() s, err := dagnode.Size()
if err != nil { if err != nil {
...@@ -562,7 +565,7 @@ func getOutput(dagnode ipld.Node) (*Object, error) { ...@@ -562,7 +565,7 @@ func getOutput(dagnode ipld.Node) (*Object, error) {
} }
output := &Object{ output := &Object{
Hash: c.String(), Hash: c.Encode(base),
Size: strconv.FormatUint(s, 10), Size: strconv.FormatUint(s, 10),
Links: make([]Link, len(dagnode.Links())), Links: make([]Link, len(dagnode.Links())),
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论