-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
161 lines (143 loc) · 3.52 KB
/
utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/incognitochain/go-incognito-sdk-v2/incclient"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
"log"
"os"
"reflect"
"strings"
"time"
)
var (
network string
host string
debug int
cache int
askUser = true
isMainNet = false
clientVersion = 2
)
func defaultBeforeFunc(_ *cli.Context) error {
return initNetWork()
}
func initNetWork() error {
if cache != 0 {
incclient.MaxGetCoinThreads = 20
}
if debug != 0 {
incclient.Logger.IsEnable = true
}
if host != "" {
fmt.Printf("host: %v, version: %v\n", host, clientVersion)
return initClient(host, clientVersion)
}
switch network {
case "mainnet":
return NewMainNetConfig(nil)
case "testnet":
return NewTestNetConfig(nil)
case "testnet1":
return NewTestNet1Config(nil)
case "local":
return NewLocalConfig(nil)
}
return fmt.Errorf("network not found")
}
func initClient(rpcHost string, version int) error {
ethNode := incclient.MainNetETHHost
var err error
switch network {
case "testnet":
ethNode = incclient.TestNetETHHost
err = NewTestNetConfig(nil)
case "testnet1":
ethNode = incclient.TestNet1ETHHost
err = NewTestNet1Config(nil)
case "local":
ethNode = incclient.LocalETHHost
err = NewLocalConfig(nil)
default:
err = NewMainNetConfig(nil)
}
if err != nil {
return err
}
incClient, err := incclient.NewIncClient(rpcHost, ethNode, version, network)
if cache != 0 {
incClient, err = incclient.NewIncClientWithCache(rpcHost, ethNode, version, network)
}
if err != nil {
return err
}
cfg.incClient = incClient
return nil
}
// checkSufficientIncBalance checks if the Incognito balance is not less than the requiredAmount.
func checkSufficientIncBalance(privateKey, tokenIDStr string, requiredAmount uint64) (balance uint64, err error) {
balance, err = cfg.incClient.GetBalance(privateKey, tokenIDStr)
if err != nil {
return
}
if balance < requiredAmount {
err = fmt.Errorf("need at least %v of token %v to continue", requiredAmount, tokenIDStr)
}
return
}
// promptInput asks for input from the user and saves input to `response`.
// If isSecret is `true`, it will not echo user's input on the terminal.
func promptInput(message string, response interface{}, isSecret ...bool) ([]byte, error) {
fmt.Printf("%v %v: ", time.Now().Format("2006/01/02 15:04:05"), message)
var input []byte
var err error
if len(isSecret) > 0 && isSecret[0] {
input, err = terminal.ReadPassword(0)
if err != nil {
return nil, err
}
fmt.Println()
} else {
reader := bufio.NewReader(os.Stdin)
tmpInput, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
tmpInput = parseInput(tmpInput)
input = []byte(tmpInput)
}
switch reflect.TypeOf(response).String() {
case "*string", "string":
response = string(input)
default:
err = json.Unmarshal(input, response)
if err != nil {
return nil, err
}
}
return input, nil
}
// yesNoPrompt asks for a yes/no decision from the user.
func yesNoPrompt(message string) {
fmt.Printf("%v %v (y/n): ", time.Now().Format("2006/01/02 15:04:05"), message)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
input = parseInput(input)
if !strings.Contains(input, "y") && !strings.Contains(input, "Y") {
log.Fatal("Abort!!")
}
}
func parseInput(text string) string {
if len(text) == 0 {
return text
}
if text[len(text)-1] == 13 || text[len(text)-1] == 10 {
text = text[:len(text)-1]
}
return text
}