lotus/cmd/lotus-shed/base32.go

53 lines
912 B
Go
Raw Normal View History

2019-12-09 15:09:23 +00:00
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/multiformats/go-base32"
2022-06-14 15:00:51 +00:00
"github.com/urfave/cli/v2"
2019-12-09 15:09:23 +00:00
)
var base32Cmd = &cli.Command{
Name: "base32",
Description: "multiformats base32",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "decode",
Value: false,
Usage: "Decode the multiformats base32",
},
},
Action: func(cctx *cli.Context) error {
var input io.Reader
if cctx.Args().Len() == 0 {
input = os.Stdin
} else {
input = strings.NewReader(cctx.Args().First())
}
bytes, err := ioutil.ReadAll(input)
if err != nil {
return nil
}
if cctx.Bool("decode") {
decoded, err := base32.RawStdEncoding.DecodeString(strings.TrimSpace(string(bytes)))
if err != nil {
return err
}
fmt.Println(string(decoded))
} else {
encoded := base32.RawStdEncoding.EncodeToString(bytes)
fmt.Println(encoded)
}
return nil
},
}