Skip to content

Commit

Permalink
Enhance API for getting the CidBase, rename option to "cid-base".
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Aug 3, 2018
1 parent 674174a commit 324d75a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
3 changes: 1 addition & 2 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ You can now check what blocks have been created by:
return
}

baseStr := req.Options[MbaseOption].(string)
base, err := GetMultibase(baseStr, "", "")
base, _, err := HandleCidBase(req, env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
3 changes: 1 addition & 2 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ The JSON output contains type information.
return
}

baseStr, _, _ := req.Option(MbaseOption).String()
base, err := GetMultibase(baseStr, "", "")
base, _, _, err := HandleCidBaseOld(req)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
52 changes: 33 additions & 19 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package commands
import (
"io"
"strings"
"context"

oldcmds "github.com/ipfs/go-ipfs/commands"
lgc "github.com/ipfs/go-ipfs/commands/legacy"
dag "github.com/ipfs/go-ipfs/core/commands/dag"
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 @@ -22,7 +22,6 @@ var log = logging.Logger("core/commands")

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

var Root = &cmds.Command{
Expand Down Expand Up @@ -94,7 +93,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."),
cmdkit.StringOption("cid-base", "mbase", "Multi-base to use to encode version 1 CIDs in output."),

// global options, added to every command
cmds.OptionEncodingType,
Expand Down Expand Up @@ -223,22 +222,37 @@ 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
// HandleCidBase handles processing of the "cid-base" flag. It
// currently checks for the "cid-base" flag and replacesing the
// requests context with a new one that adds a "cid-base" vaue.
func HandleCidBase(req *cmds.Request, env cmds.Environment) (mbase.Encoder, bool, error) {
baseStr, _ := req.Options["cid-base"].(string)
if baseStr != "" {
encoder, err := mbase.EncoderByName(baseStr)
if err != nil {
return encoder, false, err
}
req.Context = context.WithValue(req.Context, "cid-base", encoder)
return encoder, true, err
}
if baseStr == "" {
baseStr = def
}
if baseStr == "" {
baseStr = "base58btc"
encoder, _ := mbase.NewEncoder(mbase.Base58BTC)
return encoder, false, nil
}

// HandleCidBaseFlagOld is like HandleCidBase but works with the old
// commands interface. Since it is not possible to change the context
// using this interface and new context is returned instead.
func HandleCidBaseOld(req oldcmds.Request) (mbase.Encoder, bool, context.Context, error) {
baseStr, _, _ := req.Option("cid-base").String()
ctx := req.Context()
if baseStr != "" {
encoder, err := mbase.EncoderByName(baseStr)
if err != nil {
return encoder, false, ctx, err
}
ctx = context.WithValue(ctx, "cid-base", encoder)
return encoder, true, ctx, err
}
return mbase.EncoderByName(baseStr)
encoder, _ := mbase.NewEncoder(mbase.Base58BTC)
return encoder, false, ctx, nil
}

0 comments on commit 324d75a

Please sign in to comment.