lotus/cmd/lotus-shed/base16.go

52 lines
811 B
Go
Raw Permalink Normal View History

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