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
96 lines (79 loc) · 2.25 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
package util
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/hyperledger/burrow/execution/exec"
log "github.com/sirupsen/logrus"
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) 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 {
log.WithField("addr", txe.Receipt.ContractAddress).Warn()
log.WithField("txHash", txe.TxHash).Info()
} else {
log.WithField("=>", txe.TxHash).Warn("Transaction Hash")
log.WithField("=>", height).Debug("Block height")
ret := txe.GetResult().GetReturn()
if len(ret) != 0 {
log.WithField("=>", hex.EncodeUpperToString(ret)).Warn("Return Value")
log.WithField("=>", txe.Exception).Debug("Exception")
}
}
return nil
}
func GetStringResponse(question string, defaultAnswer string, reader *os.File) (string, error) {
readr := bufio.NewReader(reader)
log.Warn(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) (int64, error) {
readr := bufio.NewReader(reader)
log.Warn(question)
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) (bool, error) {
var result bool
readr := bufio.NewReader(reader)
log.Warn(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
}