add encoding flag
This commit is contained in:
parent
3e143cac4b
commit
0439e95aa3
@ -5,38 +5,55 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
mh "github.com/multiformats/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cidCmd = &cli.Command{
|
var cidCmd = &cli.Command{
|
||||||
Name: "cid",
|
Name: "cid",
|
||||||
|
Usage: "Cid command",
|
||||||
Subcommands: cli.Commands{
|
Subcommands: cli.Commands{
|
||||||
cidIdCmd,
|
cidIdCmd,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var cidIdCmd = &cli.Command{
|
var cidIdCmd = &cli.Command{
|
||||||
Name: "id",
|
Name: "id",
|
||||||
Usage: "create identity CID from hex or base64 data",
|
Usage: "Create identity CID from hex or base64 data",
|
||||||
|
ArgsUsage: "[data]",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "encoding",
|
||||||
|
Value: "base64",
|
||||||
|
Usage: "specify input encoding to parse",
|
||||||
|
},
|
||||||
|
},
|
||||||
Action: func(cctx *cli.Context) error {
|
Action: func(cctx *cli.Context) error {
|
||||||
if !cctx.Args().Present() {
|
if !cctx.Args().Present() {
|
||||||
return fmt.Errorf("must specify data")
|
return fmt.Errorf("must specify data")
|
||||||
}
|
}
|
||||||
|
|
||||||
dec, err := hex.DecodeString(cctx.Args().First())
|
var dec []byte
|
||||||
if err != nil {
|
switch cctx.String("encoding") {
|
||||||
dec, err = base64.StdEncoding.DecodeString(cctx.Args().First())
|
case "base64":
|
||||||
|
data, err := base64.StdEncoding.DecodeString(cctx.Args().First())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return xerrors.Errorf("decoding base64 value: %w", err)
|
||||||
}
|
}
|
||||||
|
dec = data
|
||||||
|
case "hex":
|
||||||
|
data, err := hex.DecodeString(cctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return xerrors.Errorf("decoding hex value: %w", err)
|
||||||
|
}
|
||||||
|
dec = data
|
||||||
|
default:
|
||||||
|
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
|
||||||
}
|
}
|
||||||
|
|
||||||
builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
|
builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
|
||||||
|
|
||||||
c, err := builder.Sum(dec)
|
c, err := builder.Sum(dec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user