/
singlefile.go
114 lines (97 loc) · 2.13 KB
/
singlefile.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"bytes"
"encoding/json"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/state"
survey "gopkg.in/AlecAivazis/survey.v1"
)
func OpenBlock(block *state.WholeBlock) {
// the questions to ask
var qs = []*survey.Question{
{
Name: "block",
Prompt: &survey.Select{
Message: "Choose a block:",
Options: []string{"back", "Directory Block", "Admin Block", "Factoid Block", "Entry Credit Block"},
Default: "Directory Block",
},
},
}
var qs2 = []*survey.Question{
{
Name: "printtype",
Prompt: &survey.Select{
Message: "Choose a printing format:",
Options: []string{"json", "string", "binary"},
Default: "json",
},
},
}
// the answers will be written to this struct
answers := struct {
Block string `survey:"block"` // or you can tag fields to match a specific name
PrintType string `survery:"printtype"`
}{}
// perform the questions
err := survey.Ask(qs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
if answers.Block == "back" {
return
}
// perform the questions
err = survey.Ask(qs2, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
switch answers.Block {
case "Directory Block":
Print(block.DBlock, answers.PrintType)
case "Admin Block":
Print(block.ABlock, answers.PrintType)
case "Factoid Block":
Print(block.FBlock, answers.PrintType)
case "Entry Credit Block":
Print(block.ECBlock, answers.PrintType)
case "back":
return
}
OpenBlock(block)
}
func Print(obj interfaces.BinaryMarshallable, format string) {
switch format {
case "json":
PrintJson(obj)
case "string":
PrintString(obj)
case "binary":
PrintBinary(obj)
}
}
func PrintBinary(obj interfaces.BinaryMarshallable) {
data, err := obj.MarshalBinary()
if err != nil {
panic(err)
}
fmt.Printf("%x\n", data)
}
func PrintString(obj interface{}) {
fmt.Println(obj)
}
func PrintJson(obj interface{}) {
data, err := json.Marshal(obj)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = json.Indent(&buf, data, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(buf.Bytes()))
}