reflector.go/cmd/decode.go

56 lines
1.1 KiB
Go
Raw Normal View History

2019-09-11 20:35:25 +02:00
package cmd
import (
"encoding/hex"
"fmt"
"github.com/lbryio/lbry.go/v2/schema/stake"
2019-09-11 20:35:25 +02:00
"github.com/davecgh/go-spew/spew"
2021-07-24 00:08:13 +02:00
"github.com/gogo/protobuf/jsonpb"
2019-09-11 20:35:25 +02:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
var cmd = &cobra.Command{
Use: "decode VALUE",
Short: "Decode a claim value",
Args: cobra.ExactArgs(1),
Run: decodeCmd,
}
rootCmd.AddCommand(cmd)
}
func decodeCmd(cmd *cobra.Command, args []string) {
c, err := stake.DecodeClaimHex(args[0], "")
2019-09-11 20:35:25 +02:00
if err != nil {
log.Fatal(err)
}
m := jsonpb.Marshaler{Indent: " "}
2019-09-11 20:35:25 +02:00
if stream := c.Claim.GetStream(); stream != nil {
json, err := m.MarshalToString(stream)
if err != nil {
log.Fatal(err)
}
fmt.Println(json)
fmt.Printf("SD hash as hex: %s\n", hex.EncodeToString(stream.GetSource().GetSdHash()))
2019-09-11 20:35:25 +02:00
} else if channel := c.Claim.GetChannel(); channel != nil {
json, err := m.MarshalToString(channel)
if err != nil {
log.Fatal(err)
}
fmt.Println(json)
} else if repost := c.Claim.GetRepost(); repost != nil {
json, err := m.MarshalToString(repost)
if err != nil {
log.Fatal(err)
}
fmt.Println(json)
2019-09-11 20:35:25 +02:00
} else {
spew.Dump(c)
}
}