-
Notifications
You must be signed in to change notification settings - Fork 0
/
cad_prometh.go
117 lines (104 loc) · 3.17 KB
/
cad_prometh.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
// Copyright 2018 The go-hpb Authors
// Modified based on go-ethereum, which Copyright (C) 2014 The go-ethereum Authors.
//
// The go-hpb is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-hpb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-hpb. If not, see <http://wwp.gnu.org/licenses/>.
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strings"
"github.com/hpb-project/go-hpb/common"
"github.com/hpb-project/go-hpb/common/log"
)
type config struct {
path string // File containing the configuration values
genesis []CadWinner // Genesis block to cache for node deploys
}
// save configs to file
func (c config) flush() {
os.MkdirAll(filepath.Dir(c.path), 0755)
out, _ := json.MarshalIndent(c, "", " ")
if err := ioutil.WriteFile(c.path, out, 0644); err != nil {
log.Warn("Failed to save prometh configs", "file", c.path, "err", err)
}
}
type prometh struct {
network string // Network name to manage
conf config // Configurations from previous runs
in *bufio.Reader //
}
// read reads a single line from stdin, trimming if from spaces.
func (p *prometh) read() string {
fmt.Printf("> ")
text, err := p.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)
}
return strings.TrimSpace(text)
}
// readAddress reads a single line from stdin, trimming if from spaces and converts
// it to an Hpb address.
func (p *prometh) readAddress() *common.Address {
for {
// Read the address from the user
fmt.Printf("> hpb")
text, err := p.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)
}
if text = strings.TrimSpace(text); text == "" {
return nil
}
// Make sure it looks ok and return it if so
if len(text) != 40 {
log.Error("Invalid address length, please retry")
continue
}
bigaddr, _ := new(big.Int).SetString(text, 16)
address := common.BigToAddress(bigaddr)
return &address
}
}
// readString reads a single line from stdin, trimming if from spaces, enforcing
// non-emptyness.
func (p *prometh) readString() string {
for {
fmt.Printf("> ")
text, err := p.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)
}
if text = strings.TrimSpace(text); text != "" {
return text
}
}
}
// readDefaultString reads a single line from stdin, trimming if from spaces. If
// an empty line is entered, the default value is returned.
func (p *prometh) readDefaultString(def string) string {
fmt.Printf("> ")
text, err := p.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)
}
if text = strings.TrimSpace(text); text != "" {
return text
}
return def
}