-
Notifications
You must be signed in to change notification settings - Fork 55
/
output.go
216 lines (197 loc) · 6.12 KB
/
output.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ux
import (
"fmt"
"io"
"sort"
"strings"
"time"
"golang.org/x/exp/maps"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-network-runner/rpcpb"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
var Logger *UserLog
type UserLog struct {
log logging.Logger
Writer io.Writer
}
func NewUserLog(log logging.Logger, userwriter io.Writer) {
if Logger == nil {
Logger = &UserLog{
log: log,
Writer: userwriter,
}
}
}
// PrintToUser prints msg directly on the screen, but also to log file
func (ul *UserLog) PrintToUser(msg string, args ...interface{}) {
fmt.Print("\r\033[K") // Clear the line from the cursor position to the end
formattedMsg := fmt.Sprintf(msg, args...) + "\n"
ul.print(formattedMsg)
}
func (ul *UserLog) print(msg string) {
if ul != nil {
fmt.Fprint(ul.Writer, msg)
ul.log.Info(msg)
} else {
fmt.Print(msg)
}
}
// Info prints to the log file
func (ul *UserLog) Info(msg string, args ...interface{}) {
formattedMsg := fmt.Sprintf(msg, args...)
ul.log.Info(formattedMsg)
}
// Error prints to the log file
func (ul *UserLog) Error(msg string, args ...interface{}) {
formattedMsg := fmt.Sprintf(msg, args...)
ul.log.Error(formattedMsg)
}
// GreenCheckmarkToUser prints a green checkmark to the user before the message
func (ul *UserLog) GreenCheckmarkToUser(msg string, args ...interface{}) {
checkmark := "\u2713" // Unicode for checkmark symbol
green := color.New(color.FgHiGreen).SprintFunc()
ul.PrintToUser(green(checkmark)+" "+msg, args...)
}
func (ul *UserLog) RedXToUser(msg string, args ...interface{}) {
xmark := "\u2717" // Unicode for X symbol
red := color.New(color.FgHiRed).SprintFunc()
ul.PrintToUser(red(xmark)+" "+msg, args...)
}
func (ul *UserLog) PrintLineSeparator() {
ul.PrintToUser("==============================================")
}
// PrintWait does some dot printing to entertain the user
func PrintWait(cancel chan struct{}) {
for {
select {
case <-time.After(1 * time.Second):
fmt.Print(".")
case <-cancel:
return
}
}
}
// PrintLocalNetworkEndpointsInfo prints the endpoints coming from the status call
func PrintLocalNetworkEndpointsInfo(clusterInfo *rpcpb.ClusterInfo) error {
for _, chainInfo := range clusterInfo.CustomChains {
if err := PrintSubnetEndpoints(clusterInfo, chainInfo, utils.InsideCodespace()); err != nil {
return err
}
Logger.PrintToUser("")
}
if err := PrintNetworkEndpoints(clusterInfo, utils.InsideCodespace()); err != nil {
return err
}
return nil
}
func PrintSubnetEndpoints(clusterInfo *rpcpb.ClusterInfo, chainInfo *rpcpb.CustomChainInfo, codespaceURLs bool) error {
nodeInfos := maps.Values(clusterInfo.NodeInfos)
nodeUris := utils.Map(nodeInfos, func(nodeInfo *rpcpb.NodeInfo) string { return nodeInfo.GetUri() })
if len(nodeUris) == 0 {
return fmt.Errorf("network has no nodes")
}
sort.Strings(nodeUris)
refNodeURI := nodeUris[0]
nodeInfo := utils.Find(nodeInfos, func(nodeInfo *rpcpb.NodeInfo) bool { return nodeInfo.GetUri() == refNodeURI })
if nodeInfo == nil {
return fmt.Errorf("unexpected nil nodeInfo")
}
strBuilder := strings.Builder{}
table := tablewriter.NewWriter(&strBuilder)
table.SetRowLine(true)
table.SetAutoMergeCellsByColumnIndex([]int{0})
aliasedURL := fmt.Sprintf("%s/ext/bc/%s/rpc", (*nodeInfo).GetUri(), chainInfo.ChainName)
blockchainIDURL := fmt.Sprintf("%s/ext/bc/%s/rpc", (*nodeInfo).GetUri(), chainInfo.ChainId)
table.Append([]string{fmt.Sprintf("%s RPC URLs", chainInfo.ChainName)})
table.ClearRows()
table.Append([]string{"Localhost", aliasedURL})
table.Append([]string{"Localhost", blockchainIDURL})
if codespaceURLs {
var err error
blockchainIDURL, err = utils.GetCodespaceURL(blockchainIDURL)
if err != nil {
return err
}
aliasedURL, err = utils.GetCodespaceURL(aliasedURL)
if err != nil {
return err
}
table.Append([]string{"Codespace", aliasedURL})
table.Append([]string{"Codespace", blockchainIDURL})
}
table.Render()
tableStr := strBuilder.String()
var err error
tableStr, err = addTitleToTable(tableStr, fmt.Sprintf("%s RPC URLs", chainInfo.ChainName))
if err != nil {
return err
}
Logger.print(tableStr)
return nil
}
func addTitleToTable(tableStr string, title string) (string, error) {
newLineIdx := strings.Index(tableStr, "\n")
if newLineIdx == -1 {
return "", fmt.Errorf("expected to found newline in table output")
}
titleStr := tableStr[:newLineIdx] + "\n"
availableLen := newLineIdx - 2
if availableLen < len(title) {
title = title[:availableLen-1]
}
spacesCount := availableLen - len(title)
spaces1 := spacesCount / 2
spaces2 := spacesCount - spaces1
titleStr = titleStr + "|" + strings.Repeat(" ", spaces1) + title + strings.Repeat(" ", spaces2) + "|" + "\n"
return titleStr + tableStr, nil
}
func PrintNetworkEndpoints(clusterInfo *rpcpb.ClusterInfo, codespaceURLs bool) error {
strBuilder := strings.Builder{}
table := tablewriter.NewWriter(&strBuilder)
table.SetRowLine(true)
header := []string{"Name", "Node ID", "Localhost Endpoint"}
if codespaceURLs {
header = append(header, "Codespace Endpoint")
}
table.Append(header)
nodeNames := clusterInfo.NodeNames
sort.Strings(nodeNames)
nodeInfos := map[string]*rpcpb.NodeInfo{}
for _, nodeInfo := range clusterInfo.NodeInfos {
nodeInfos[nodeInfo.Name] = nodeInfo
}
var err error
for _, nodeName := range nodeNames {
nodeInfo := nodeInfos[nodeName]
nodeURL := nodeInfo.GetUri()
row := []string{nodeInfo.Name, nodeInfo.Id, nodeURL}
if codespaceURLs {
nodeURL, err = utils.GetCodespaceURL(nodeURL)
if err != nil {
return err
}
row = append(row, nodeURL)
}
table.Append(row)
}
table.Render()
tableStr := strBuilder.String()
tableStr, err = addTitleToTable(tableStr, "Nodes")
if err != nil {
return err
}
Logger.print(tableStr)
return nil
}
func ConvertToStringWithThousandSeparator(input uint64) string {
p := message.NewPrinter(language.English)
s := p.Sprintf("%d", input)
return strings.ReplaceAll(s, ",", "_")
}