lotus/cmd/lotus-shed/base32.go

54 lines
911 B
Go
Raw Permalink Normal View History

2019-12-09 15:09:23 +00:00
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/urfave/cli.v2"
"github.com/multiformats/go-base32"
)
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
},
}