This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
readers.go
99 lines (81 loc) · 2.35 KB
/
readers.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
package util
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/logging"
hex "github.com/tmthrgd/go-hex"
)
// This is a closer function which is called by most of the tx_run functions
func ReadTxSignAndBroadcast(txe *exec.TxExecution, err error, logger *logging.Logger) error {
// if there's an error just return.
if err != nil {
return err
}
// if there is nothing to unpack then just return.
if txe == nil {
return nil
}
// Unpack and display for the user.
height := fmt.Sprintf("%d", txe.Height)
if txe.Receipt.CreatesContract {
logger.InfoMsg("Tx Return",
"addr", txe.Receipt.ContractAddress.String(),
"Transaction Hash", hex.EncodeToString(txe.TxHash))
} else {
logger.InfoMsg("Tx Return",
"Transaction Hash", hex.EncodeToString(txe.TxHash),
"Block Height", height)
ret := txe.GetResult().GetReturn()
if len(ret) != 0 {
logger.InfoMsg("Return",
"Return Value", hex.EncodeUpperToString(ret),
"Exception", txe.Exception)
}
}
return nil
}
func GetStringResponse(question string, defaultAnswer string, reader *os.File, logger *logging.Logger) (string, error) {
readr := bufio.NewReader(reader)
logger.InfoMsg(question)
text, _ := readr.ReadString('\n')
text = strings.Replace(text, "\n", "", 1)
if text == "" {
return defaultAnswer, nil
}
return text, nil
}
func GetIntResponse(question string, defaultAnswer int64, reader *os.File, logger *logging.Logger) (int64, error) {
readr := bufio.NewReader(reader)
text, _ := readr.ReadString('\n')
text = strings.Replace(text, "\n", "", 1)
if text == "" {
return defaultAnswer, nil
}
result, err := strconv.ParseInt(text, 10, 64)
if err != nil {
return 0, nil
}
return result, nil
}
// displays the question, scans for the response, if the response is an empty
// string will return default, otherwise will parseBool and return the result.
func GetBoolResponse(question string, defaultAnswer bool, reader *os.File, logger *logging.Logger) (bool, error) {
var result bool
readr := bufio.NewReader(reader)
logger.InfoMsg(question)
text, _ := readr.ReadString('\n')
text = strings.Replace(text, "\n", "", 1)
if text == "" {
return defaultAnswer, nil
}
if text == "Yes" || text == "YES" || text == "Y" || text == "y" {
result = true
} else {
result = false
}
return result, nil
}