generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 119
/
debug_extensions.go
61 lines (52 loc) 路 1.42 KB
/
debug_extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/proto/tendermint/types"
)
func ExtendDebugCmd(getCmd func(name string) (*cobra.Command, error)) error {
debugCmd, err := getCmd("debug")
if err != nil {
return err
}
debugCmd.AddCommand(DecodeBlocksCmd())
return nil
}
func DecodeBlocksCmd() *cobra.Command {
return &cobra.Command{
Use: "decode-blocks",
Short: "Decode base64 protobuf encoded blocks in JSON.",
Long: "Read base64 protobuf encoded blocks from stdin and write the corresponding JSON representation on stdout.",
RunE: func(cmd *cobra.Command, args []string) error {
scanner := bufio.NewScanner(cmd.InOrStdin())
for scanner.Scan() {
strB64 := scanner.Text()
if strB64[0] == '"' {
strB64 = strB64[1:]
}
if strB64[len(strB64)-1] == '"' {
strB64 = strB64[:len(strB64)-1]
}
bytes, err := base64.StdEncoding.DecodeString(strB64)
if err != nil {
cmd.PrintErrln("could not decode base64 block:", err.Error())
continue
}
block := new(types.Block)
if err := block.Unmarshal(bytes); err != nil {
cmd.PrintErrln("could not decode block:", err.Error())
continue
}
json, err := json.Marshal(block)
if err != nil {
cmd.PrintErrln("could not marshal block in JSON:", err.Error())
continue
}
cmd.Println(string(json))
}
return nil
},
}
}