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"
2019-09-11 20:35:25 +02:00
"github.com/lbryio/lbryschema.go/claim"
"github.com/davecgh/go-spew/spew"
"github.com/golang/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 := claim.DecodeClaimHex(args[0], "")
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)
}
}