forked from iotexproject/iotex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
201 lines (176 loc) · 5.16 KB
/
client.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package ioctl
import (
"context"
"crypto/tls"
"fmt"
"os/exec"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/ioctl/validator"
)
type (
// Client defines the interface of an ioctl client
Client interface {
// Start starts the client
Start(context.Context) error
// Stop stops the client
Stop(context.Context) error
// Config returns the config of the client
Config() config.Config
// APIServiceClient returns an API service client
APIServiceClient(APIServiceConfig) (iotexapi.APIServiceClient, error)
// SelectTranslation select a translation based on UILanguage
SelectTranslation(map[config.Language]string) (string, config.Language)
// AskToConfirm asks user to confirm from terminal, true to continue
AskToConfirm() bool
// ReadSecret reads password from terminal
ReadSecret() (string, error)
// Execute a bash command
Execute(string) error
// doing
GetAddress(in string) (string, error)
// doing
Address(in string) (string, error)
// doing
NewKeyStore(string, int, int) *keystore.KeyStore
// doing
GetAliasMap() map[string]string
}
// APIServiceConfig defines a config of APIServiceClient
APIServiceConfig struct {
Endpoint string
Insecure bool
}
client struct {
cfg config.Config
conn *grpc.ClientConn
// TODO: merge into config
lang config.Language
}
)
// NewClient creates a new ioctl client
func NewClient() Client {
return &client{
cfg: config.ReadConfig,
}
}
var confirmMessages = map[config.Language]string{
config.English: "Do you want to continue? [yes/NO]",
config.Chinese: "是否继续?【是/否】",
}
func (c *client) Start(context.Context) error {
return nil
}
func (c *client) Stop(context.Context) error {
if c.conn != nil {
if err := c.conn.Close(); err != nil {
return err
}
c.conn = nil
}
return nil
}
func (c *client) Config() config.Config {
return c.cfg
}
func (c *client) AskToConfirm() bool {
msg, lang := c.SelectTranslation(confirmMessages)
fmt.Println(msg)
var confirm string
fmt.Scanf("%s", &confirm)
switch lang {
case config.Chinese:
return strings.EqualFold(confirm, "是")
default: // config.English
return strings.EqualFold(confirm, "yes")
}
}
func (c *client) SelectTranslation(trls map[config.Language]string) (string, config.Language) {
trl, ok := trls[c.lang]
if ok {
return trl, c.lang
}
trl, ok = trls[config.English]
if !ok {
panic("failed to pick a translation")
}
return trl, config.English
}
func (c *client) ReadSecret() (string, error) {
// TODO: delete util.ReadSecretFromStdin, and move code to here
return util.ReadSecretFromStdin()
}
func (c *client) APIServiceClient(cfg APIServiceConfig) (iotexapi.APIServiceClient, error) {
if c.conn != nil {
if err := c.conn.Close(); err != nil {
return nil, err
}
}
var err error
if cfg.Insecure {
c.conn, err = grpc.Dial(cfg.Endpoint, grpc.WithInsecure())
} else {
c.conn, err = grpc.Dial(cfg.Endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}
if err != nil {
return nil, err
}
return iotexapi.NewAPIServiceClient(c.conn), nil
}
func (c *client) Execute(cmd string) error {
return exec.Command("bash", "-c", cmd).Run()
}
func (c *client) GetAddress(in string) (string, error) {
addr, err := config.GetAddressOrAlias(in)
if err != nil {
return "", output.NewError(output.AddressError, "", err)
}
return address(addr)
}
func (c *client) Address(in string) (string, error) {
if len(in) >= validator.IoAddrLen {
if err := validator.ValidateAddress(in); err != nil {
return "", output.NewError(output.ValidationError, "", err)
}
return in, nil
}
addr, ok := config.ReadConfig.Aliases[in]
if ok {
return addr, nil
}
return "", output.NewError(output.ConfigError, "cannot find address from "+in, nil)
}
func (c *client) NewKeyStore(keydir string, scryptN, scryptP int) *keystore.KeyStore {
return keystore.NewKeyStore(keydir, scryptN, scryptP)
}
func (c *client) GetAliasMap() map[string]string {
aliases := make(map[string]string)
for name, addr := range config.ReadConfig.Aliases {
aliases[addr] = name
}
return aliases
}
func address(in string) (string, error) {
if len(in) >= validator.IoAddrLen {
if err := validator.ValidateAddress(in); err != nil {
return "", output.NewError(output.ValidationError, "", err)
}
return in, nil
}
addr, ok := config.ReadConfig.Aliases[in]
if ok {
return addr, nil
}
return "", output.NewError(output.ConfigError, "cannot find address from "+in, nil)
}