-
Notifications
You must be signed in to change notification settings - Fork 7
/
generator.go
118 lines (95 loc) · 3.26 KB
/
generator.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
package wallet
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"text/template"
vspaths "code.vegaprotocol.io/vega/paths"
vwconfig "code.vegaprotocol.io/vega/wallet/network"
"code.vegaprotocol.io/vegacapsule/config"
"code.vegaprotocol.io/vegacapsule/types"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
)
type ConfigTemplateContext struct {
TendermintNodePrefix string
VegaNodePrefix string
DataNodePrefix string
WalletPrefix string
Validators []types.NodeSet
NonValidators []types.NodeSet
}
func NewConfigTemplate(templateRaw string) (*template.Template, error) {
t, err := template.New("config.toml").Funcs(sprig.TxtFuncMap()).Parse(templateRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse template config: %w", err)
}
return t, nil
}
type ConfigGenerator struct {
conf *config.Config
homeDir string
}
func NewConfigGenerator(conf *config.Config) (*ConfigGenerator, error) {
homeDir, err := filepath.Abs(path.Join(*conf.OutputDir, conf.WalletPrefix))
if err != nil {
return nil, err
}
return &ConfigGenerator{
conf: conf,
homeDir: homeDir,
}, nil
}
func (cg *ConfigGenerator) InitiateWithNetworkConfig(conf *config.WalletConfig, validators, nonValidators []types.NodeSet, configTemplate *template.Template) (*types.Wallet, error) {
if err := os.MkdirAll(cg.homeDir, os.ModePerm); err != nil {
return nil, err
}
err := cg.initiateWallet(conf)
if err != nil {
return nil, fmt.Errorf("failed to initiate wallet %s: %w", conf.Name, err)
}
if err := cg.generateNetworkConfig(validators, nonValidators, configTemplate); err != nil {
return nil, fmt.Errorf("failed to generate network config %q: %w", cg.configFilePath(), err)
}
importOut, err := cg.importNetworkConfig(conf)
if err != nil {
return nil, fmt.Errorf("failed to import network to wallet %s: %w", conf.Name, err)
}
return &types.Wallet{
GeneratedService: types.GeneratedService{
Name: fmt.Sprintf("%s-vegawallet", cg.conf.Network.Name),
HomeDir: cg.homeDir,
ConfigFilePath: cg.configFilePath(),
},
Network: importOut.Name,
BinaryPath: *cg.conf.VegaBinary,
}, nil
}
func (cg ConfigGenerator) generateNetworkConfig(validators, nonValidators []types.NodeSet, configTemplate *template.Template) error {
templateCtx := ConfigTemplateContext{
TendermintNodePrefix: cg.conf.TendermintNodePrefix,
VegaNodePrefix: cg.conf.VegaNodePrefix,
DataNodePrefix: cg.conf.DataNodePrefix,
WalletPrefix: cg.conf.VegaNodePrefix,
Validators: validators,
NonValidators: nonValidators,
}
buff := bytes.NewBuffer([]byte{})
if err := configTemplate.Execute(buff, templateCtx); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
overrideConfig := vwconfig.Network{}
if _, err := toml.NewDecoder(buff).Decode(&overrideConfig); err != nil {
return fmt.Errorf("failed decode override config: %w", err)
}
configFilePath := cg.configFilePath()
if err := vspaths.WriteStructuredFile(configFilePath, overrideConfig); err != nil {
return fmt.Errorf("failed to write configuration file at %s: %w", configFilePath, err)
}
return nil
}
func (cg ConfigGenerator) configFilePath() string {
return filepath.Join(cg.homeDir, "config.toml")
}