-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
226 lines (201 loc) · 5.5 KB
/
config.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
package main
import (
"cpm/generators"
_ "embed"
"fmt"
"io"
"os"
"strings"
"github.com/nspcc-dev/neo-go/pkg/util"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
//go:embed cpm.yaml.default
var defaultConfig []byte
var cfg = &CPMConfig{
Defaults: Defaults{
ContractGenerateSdk: false,
ContractDownload: true,
},
}
type ContractConfig struct {
Label string `yaml:"label"`
ScriptHash util.Uint160 `yaml:"script-hash"`
SourceNetwork *string `yaml:"source-network,omitempty"`
GenerateSdk *bool `yaml:"generate-sdk,omitempty"`
Download *bool `yaml:"download,omitempty"`
OnChain *GenerateConfig `yaml:"on-chain,omitempty"`
OffChain *GenerateConfig `yaml:"off-chain,omitempty"`
}
type GenerateConfig struct {
Languages []string `yaml:"languages"`
SdkDestinations SdkDestination `yaml:"destinations"`
}
type SdkDestination struct {
Csharp *string `yaml:"csharp,omitempty"`
Golang *string `yaml:"go,omitempty"`
Java *string `yaml:"java,omitempty"`
Python *string `yaml:"python,omitempty"`
TS *string `yaml:"ts,omitempty"`
}
type Defaults struct {
ContractSourceNetwork string `yaml:"contract-source-network"`
ContractDestination string `yaml:"contract-destination"`
ContractGenerateSdk bool `yaml:"contract-generate-sdk"`
ContractDownload bool `yaml:"contract-download,omitempty"`
OnChain *GenerateConfig `yaml:"on-chain,omitempty"`
OffChain *GenerateConfig `yaml:"off-chain,omitempty"`
}
type CPMConfig struct {
Defaults Defaults `yaml:"defaults"`
Contracts []ContractConfig `yaml:"contracts"`
Tools struct {
NeoExpress struct {
CanGenerateSDK bool `yaml:"canGenerateSDK"`
CanDownloadContract bool `yaml:"canDownloadContract"`
ExecutablePath *string `yaml:"executable-path,omitempty"`
ConfigPath string `yaml:"config-path"`
} `yaml:"neo-express"`
} `yaml:"tools"`
Networks []struct {
Label string `yaml:"label"`
Hosts []string `yaml:"hosts"`
} `yaml:"networks"`
}
func LoadConfig() {
f, err := os.Open(DEFAULT_CONFIG_FILE)
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("Config file %s not found. Run `cpm init` to create a default config", DEFAULT_CONFIG_FILE)
} else {
log.Fatal(err)
}
}
defer f.Close()
yamlData, _ := io.ReadAll(f)
if err := yaml.Unmarshal(yamlData, &cfg); err != nil {
log.Fatal(fmt.Errorf("failed to parse config file: %w", err))
}
// ensure all contract configs can be worked with directly
for i, c := range cfg.Contracts {
if c.SourceNetwork == nil {
cfg.Contracts[i].SourceNetwork = &cfg.Defaults.ContractSourceNetwork
}
if c.GenerateSdk == nil {
cfg.Contracts[i].GenerateSdk = &cfg.Defaults.ContractGenerateSdk
}
if c.Download == nil {
cfg.Contracts[i].Download = &cfg.Defaults.ContractDownload
}
}
}
func CreateDefaultConfig() {
if _, err := os.Stat(DEFAULT_CONFIG_FILE); os.IsNotExist(err) {
err = os.WriteFile(DEFAULT_CONFIG_FILE, defaultConfig, 0644)
if err != nil {
log.Fatal(err)
}
log.Infof("Written %s\n", DEFAULT_CONFIG_FILE)
} else {
log.Fatalf("%s already exists", DEFAULT_CONFIG_FILE)
}
}
func (c *CPMConfig) addContract(label string, scriptHash util.Uint160) {
for _, c := range cfg.Contracts {
if c.ScriptHash.Equals(scriptHash) {
return
}
}
cfg.Contracts = append(cfg.Contracts, ContractConfig{Label: label, ScriptHash: scriptHash})
}
func (c *CPMConfig) getHosts(networkLabel string) []string {
for _, network := range c.Networks {
if network.Label == networkLabel {
return network.Hosts
}
}
log.Fatalf("Could not find hosts for label: %s", networkLabel)
return nil
}
func (c *CPMConfig) getSdkDestination(forLanguage string, sdkType string) string {
defaultLocation := generators.OutputRoot + sdkType + "/" + forLanguage + "/"
if c == nil {
return defaultLocation
}
var sdkTypePath SdkDestination
if sdkType == generators.SDKOnChain {
if c.Defaults.OnChain == nil {
return defaultLocation
}
sdkTypePath = c.Defaults.OnChain.SdkDestinations
} else {
if c.Defaults.OffChain == nil {
return defaultLocation
}
sdkTypePath = c.Defaults.OffChain.SdkDestinations
}
switch forLanguage {
case LANG_PYTHON:
if path := sdkTypePath.Python; path != nil {
return EnsureSuffix(*path)
}
return defaultLocation
case LANG_GO:
if path := sdkTypePath.Golang; path != nil {
return EnsureSuffix(*path)
}
return defaultLocation
case LANG_JAVA:
if path := sdkTypePath.Java; path != nil {
return EnsureSuffix(*path)
}
return defaultLocation
case LANG_CSHARP:
if path := sdkTypePath.Csharp; path != nil {
return EnsureSuffix(*path)
}
return defaultLocation
case LANG_TYPESCRIPT:
if path := sdkTypePath.TS; path != nil {
return EnsureSuffix(*path)
}
return defaultLocation
default:
return defaultLocation
}
}
func (c *CPMConfig) saveToDisk() {
f, err := os.Create(DEFAULT_CONFIG_FILE)
if err != nil {
log.Fatal(err)
}
defer f.Close()
data, err := yaml.Marshal(cfg)
if err != nil {
return
}
_, err = f.Write(data)
if err != nil {
log.Fatal(err)
}
}
type EnumValue struct {
Enum []string
Default string
selected string
}
func (e *EnumValue) Set(value string) error {
for _, enum := range e.Enum {
if enum == value {
e.selected = value
return nil
}
}
return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", "))
}
func (e EnumValue) String() string {
if e.selected == "" {
return e.Default
}
return e.selected
}