From b167e73acb0c0aeb0f88e90b21f8747ae432dd7d Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:33:45 -0700 Subject: [PATCH] allow CLI consumer to specify CID encoding for output --- cli/client.go | 26 +++++++++++++------------- cli/cmd.go | 21 +++++++++++++++++++++ cmd/lotus/main.go | 7 +++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/cli/client.go b/cli/client.go index afd133523..49002d67e 100644 --- a/cli/client.go +++ b/cli/client.go @@ -9,7 +9,6 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" - "github.com/multiformats/go-multibase" "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" @@ -69,12 +68,12 @@ var clientImportCmd = &cli.Command{ return err } - encoded, err := c.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println(encoded) + fmt.Println(encoder.Encode(c)) return nil }, @@ -106,12 +105,12 @@ var clientCommPCmd = &cli.Command{ return err } - encoded, err := ret.Root.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println("CID: ", encoded) + fmt.Println("CID: ", encoder.Encode(ret.Root)) fmt.Println("Piece size: ", ret.Size) return nil }, @@ -162,13 +161,14 @@ var clientLocalCmd = &cli.Command{ if err != nil { return err } - for _, v := range list { - encoded, err := v.Key.StringOfBase(multibase.Base32) - if err != nil { - return err - } - fmt.Printf("%s %s %d %s\n", encoded, v.FilePath, v.Size, v.Status) + encoder, err := GetCidEncoder(cctx) + if err != nil { + return err + } + + for _, v := range list { + fmt.Printf("%s %s %d %s\n", encoder.Encode(v.Key), v.FilePath, v.Size, v.Status) } return nil }, @@ -281,12 +281,12 @@ var clientDealCmd = &cli.Command{ return err } - encoded, err := proposal.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println(encoded) + fmt.Println(encoder.Encode(*proposal)) return nil }, diff --git a/cli/cmd.go b/cli/cmd.go index fd0ab1e78..2d4ff7a75 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -9,6 +9,9 @@ import ( "strings" "syscall" + "github.com/multiformats/go-multibase" + + cidenc "github.com/ipfs/go-cidutil/cidenc" logging "github.com/ipfs/go-log/v2" "github.com/mitchellh/go-homedir" "github.com/multiformats/go-multiaddr" @@ -193,6 +196,24 @@ func DaemonContext(cctx *cli.Context) context.Context { return context.Background() } +// GetCidEncoder returns an encoder using the cid-base flag if provided, or +// the default (Base32) encoder if not. +func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) { + val := cctx.String("cid-base") + + e := cidenc.Default() + + if val != "" { + var err error + e.Base, err = multibase.EncoderByName(val) + if err != nil { + return e, err + } + } + + return e, nil +} + // ReqContext returns context for cli execution. Calling it for the first time // installs SIGTERM handler that will close returned context. // Not safe for concurrent execution. diff --git a/cmd/lotus/main.go b/cmd/lotus/main.go index 33ff56ac3..22d121364 100644 --- a/cmd/lotus/main.go +++ b/cmd/lotus/main.go @@ -61,6 +61,13 @@ func main() { Hidden: true, Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME }, + &cli.StringFlag{ + Name: "cid-base", + Hidden: true, + Value: "base32", + Usage: "Multibase encoding used for version 1 CIDs in output.", + DefaultText: "base32", + }, }, Commands: append(local, lcli.Commands...), diff --git a/go.mod b/go.mod index d83e6a845..a41fcec7d 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,7 @@ require ( github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-blockservice v0.1.3 github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00 + github.com/ipfs/go-cidutil v0.0.2 github.com/ipfs/go-datastore v0.4.4 github.com/ipfs/go-ds-badger2 v0.1.0 github.com/ipfs/go-filestore v1.0.0 diff --git a/go.sum b/go.sum index 4c4ddf12f..f78d58bd7 100644 --- a/go.sum +++ b/go.sum @@ -345,6 +345,8 @@ github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00 h1:QN88Q0kT2QiDaLxpR/SDsqOBtNIEF/F3n96gSDUimkA= github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= +github.com/ipfs/go-cidutil v0.0.2 h1:CNOboQf1t7Qp0nuNh8QMmhJs0+Q//bRL1axtCnIB1Yo= +github.com/ipfs/go-cidutil v0.0.2/go.mod h1:ewllrvrxG6AMYStla3GD7Cqn+XYSLqjK0vc+086tB6s= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.0.5/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=