allow CLI consumer to specify CID encoding for output

This commit is contained in:
laser 2020-06-05 13:33:45 -07:00
parent 28e454fd4a
commit b167e73acb
5 changed files with 44 additions and 13 deletions

View File

@ -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
},

View File

@ -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.

View File

@ -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...),

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=