-
Notifications
You must be signed in to change notification settings - Fork 322
/
format.go
233 lines (207 loc) · 6.2 KB
/
format.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 output
import (
"encoding/json"
"fmt"
"log"
)
// Format is the target of output-format flag
var Format string
// ErrorCode is the code of error
type ErrorCode int
const (
// UndefinedError used when an error cat't be classified
UndefinedError ErrorCode = iota
// UpdateError used when an error occurs when running update command
UpdateError
// RuntimeError used when an error occurs in runtime
RuntimeError
// NetworkError used when an network error is happened
NetworkError
// APIError used when an API error is happened
APIError
// ValidationError used when validation is not passed
ValidationError
// SerializationError used when marshal or unmarshal meets error
SerializationError
// ReadFileError used when error occurs during reading a file
ReadFileError
// WriteFileError used when error occurs during writing a file
WriteFileError
// FlagError used when invalid flag is set
FlagError
// ConvertError used when fail to converting data
ConvertError
// CryptoError used when crypto error occurs
CryptoError
// AddressError used if an error is related to address
AddressError
// InputError used when error about input occurs
InputError
// KeystoreError used when an error related to keystore
KeystoreError
// ConfigError used when an error about config occurs
ConfigError
// InstantiationError used when an error during instantiation
InstantiationError
// CompilerError used when an error occurs when using the solidity compiler
CompilerError
)
// MessageType marks the type of output message
type MessageType int
const (
// Result represents the result of a command
Result MessageType = iota
// Confirmation represents request for confirmation
Confirmation
// Query represents request for answer of certain question
Query
// Error represents error occurred when running a command
Error
// Warn represents non-fatal mistake occurred when running a command
Warn
)
// Output is used for format output
type Output struct {
MessageType MessageType `json:"messageType"`
Message Message `json:"message"`
MessageWithTranslation MessageWithTranslation `json:"messageWithTranslation"`
}
// Message is the message part of output
type Message interface {
String() string
}
// MessageWithTranslation is the message part of output supporting multi languages
type MessageWithTranslation interface {
String(args ...string) string
}
// ConfirmationMessage is the struct of an Confirmation output
type ConfirmationMessage struct {
Info string `json:"info"`
Options []string `json:"options"`
}
func (m *ConfirmationMessage) String() string {
if Format == "" {
line := fmt.Sprintf("%s\nOptions:", m.Info)
for _, option := range m.Options {
line += " " + option
}
line += "\nQuit for anything else."
return line
}
return FormatString(Confirmation, m)
}
// ErrorMessage is the struct of an Error output
type ErrorMessage struct {
Code ErrorCode `json:"code"`
Info string `json:"info"`
}
func (m *ErrorMessage) String() string {
if Format == "" {
return fmt.Sprintf("%d, %s", m.Code, m.Info)
}
return FormatString(Error, m)
}
// Error implements error interface
func (m ErrorMessage) Error() string {
return m.Info
}
// StringMessage is the Message for string
type StringMessage string
func (m StringMessage) String() string {
if Format == "" {
return string(m)
}
return FormatString(Result, m)
}
// Query prints query message
func (m StringMessage) Query() string {
if Format == "" {
return string(m)
}
return FormatString(Query, m)
}
// Warn prints warn message
func (m StringMessage) Warn() string {
if Format == "" {
return fmt.Sprintf("Warn: %s\n", string(m))
}
return FormatString(Warn, m)
}
// FormatString returns Output as string in certain format
func FormatString(t MessageType, m Message) string {
out := Output{
MessageType: t,
Message: m,
}
switch Format {
default: // default is json
return JSONString(out)
}
}
// FormatStringWithTrans returns Output as string in certain format supporting multi languages
func FormatStringWithTrans(t MessageType, m MessageWithTranslation) string {
out := Output{
MessageType: t,
MessageWithTranslation: m,
}
switch Format {
default: // default is json
return JSONString(out)
}
}
// JSONString returns json string for message
func JSONString(out interface{}) string {
byteAsJSON, err := json.MarshalIndent(out, "", " ")
if err != nil {
log.Panic(err)
}
return fmt.Sprint(string(byteAsJSON))
}
// NewError and returns golang error that contains Error Message
// ErrorCode can pass zero only when previous error is always a format error
// that contains non-zero error code. ErrorCode passes 0 means that I want to
// use previous error's code rather than override it.
// If there is no previous error, newInfo should not be empty.
func NewError(code ErrorCode, info string, pre error) error {
if pre == nil {
return ErrorMessage{Code: code, Info: info}
}
message, ok := pre.(ErrorMessage)
if ok {
if code != 0 {
// override error code
message.Code = code
}
if len(info) != 0 {
message.Info = fmt.Sprintf("%s: %s", info, message.Info)
}
} else {
message = ErrorMessage{Code: code, Info: fmt.Sprintf("%s: %s", info, pre.Error())}
}
return message
}
// PrintError prints Error Message in format, only used at top layer of a command
func PrintError(err error) error {
if err == nil || Format == "" {
return err
}
newErr := NewError(0, "", err)
message := newErr.(ErrorMessage)
fmt.Println(message.String())
return nil
}
// PrintResult prints result message in format
func PrintResult(result string) {
message := StringMessage(result)
fmt.Println(message.String())
}
// PrintQuery prints query message in format
func PrintQuery(query string) {
message := StringMessage(query)
fmt.Println(message.Query())
}