lotus/cmd/lotus-shed/base32.go
2022-09-14 14:33:29 -04:00

53 lines
906 B
Go

package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/multiformats/go-base32"
"github.com/urfave/cli/v2"
)
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.NArg() == 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
},
}