/
common.go
165 lines (142 loc) · 4.5 KB
/
common.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
package commands
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/tenderly/tenderly-cli/config"
"github.com/tenderly/tenderly-cli/providers"
"github.com/tenderly/tenderly-cli/truffle"
"github.com/tenderly/tenderly-cli/userError"
"os"
"strconv"
)
func CheckLogin() {
if !config.IsLoggedIn() {
logrus.Error("In order to use the Tenderly CLI, you need to login first.\n\n",
"Please use the ", Colorizer.Bold(Colorizer.Green("tenderly login")), " command to get started.")
os.Exit(1)
}
}
func CheckProvider(deploymentProvider providers.DeploymentProvider) {
if deploymentProvider == nil {
logrus.Error("Brownie, Hardhat, Buidler, OpenZeppelin or Truffle configuration was not detected.\n\n",
"Please re-run this command in a folder where at least one of the frameworks is configured.")
os.Exit(1)
}
}
func WriteGlobalConfig() {
err := config.WriteGlobalConfig()
if err != nil {
userError.LogErrorf(
"write global config: %s",
userError.NewUserError(err, "Couldn't write global config file"),
)
os.Exit(1)
}
}
func WriteProjectConfig() {
err := config.WriteProjectConfig()
if err != nil {
userError.LogErrorf(
"write project config: %s",
userError.NewUserError(err, "Couldn't write project config file"),
)
os.Exit(1)
}
}
func DetectedProjectMessage(
printLoginSuccess bool,
action string,
commandFmt string,
) {
projectDirectories := truffle.FindDirectories()
projectsLen := len(projectDirectories)
if printLoginSuccess {
logrus.Info(Colorizer.Sprintf("Now that you are successfully logged in, you can use the %s command to initialize a new project.",
Colorizer.Bold(Colorizer.Green("tenderly init")),
))
}
if projectsLen == 0 {
return
}
format := fmt.Sprintf("\t%s", commandFmt)
projectWord := "project"
initializationSentence := Colorizer.Sprintf("You can %s it by running the following command:\n\n%s",
action,
Colorizer.Bold(fmt.Sprintf(format, projectDirectories[0])),
)
if projectsLen > 1 {
projectWord = "projects"
initializationSentence = fmt.Sprintf("You can %s them by running one of the following commands:", action)
}
logrus.Println()
logrus.Infof("We have detected %d Truffle %s on your system. %s",
projectsLen,
projectWord,
initializationSentence,
)
logrus.Println()
if len(projectDirectories) == 1 {
return
}
format = fmt.Sprintf("\t%s", commandFmt)
for _, project := range projectDirectories {
logrus.Info(Colorizer.Bold(fmt.Sprintf(format, project)))
}
logrus.Println()
}
func WrongFolderMessage(action string, commandFmt string) {
logrus.Info("Couldn't detect provider directory structure. This can be caused by:")
logrus.Println()
logrus.Info(Colorizer.Sprintf("\t• The directory is not set correctly. "+
"If this is the case, either check if you are in the right directory or pass an alternative directory by using the %s flag.",
Colorizer.Bold(Colorizer.Green("--project-dir")),
))
logrus.Info(Colorizer.Sprintf("\t• Tenderly is having trouble reading the directory correctly. "+
"If you think this is the case, rerun this command with the %s flag.",
Colorizer.Bold(Colorizer.Green("--force")),
))
DetectedProjectMessage(
false,
action,
commandFmt,
)
}
type ProjectConfiguration struct {
Networks []string
}
type ProjectConfigurationMap map[string]*ProjectConfiguration
func GetProjectConfiguration() (ProjectConfigurationMap, error) {
configMap := config.MaybeGetMap(config.Projects)
if configMap == nil {
return nil, nil
}
projectConfigurationMap := make(ProjectConfigurationMap)
for projectSlug, projectConfig := range configMap {
singleConfigMap, ok := projectConfig.(map[string]interface{})
if !ok {
projectConfigurationMap[projectSlug] = &ProjectConfiguration{}
logrus.Debugf("No configuration provided for project: %s", projectSlug)
continue
}
networks, ok := singleConfigMap["networks"].([]interface{})
if !ok {
logrus.Debugf("failed extracting networks for project: %s", projectSlug)
continue
}
projectConfig := &ProjectConfiguration{}
for _, network := range networks {
switch n := network.(type) {
case int:
projectConfig.Networks = append(projectConfig.Networks, strconv.Itoa(n))
case string:
projectConfig.Networks = append(projectConfig.Networks, n)
}
}
projectConfigurationMap[projectSlug] = projectConfig
}
oldProjectSlug := config.GetString(config.ProjectSlug)
if oldProjectSlug != "" && projectConfigurationMap[oldProjectSlug] == nil {
projectConfigurationMap[oldProjectSlug] = &ProjectConfiguration{}
}
return projectConfigurationMap, nil
}