-
Notifications
You must be signed in to change notification settings - Fork 7
/
generator.go
137 lines (110 loc) · 3.81 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package faucet
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"text/template"
"code.vegaprotocol.io/shared/paths"
"code.vegaprotocol.io/vega/core/faucet"
"code.vegaprotocol.io/vegacapsule/config"
"code.vegaprotocol.io/vegacapsule/types"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"github.com/imdario/mergo"
)
type ConfigTemplateContext struct {
// description: Path to home directory of the Faucet.
HomeDir string
// description: Public key of the Faucet.
PublicKey string
}
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 the faucet's 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.FaucetPrefix))
if err != nil {
return nil, err
}
return &ConfigGenerator{
conf: conf,
homeDir: homeDir,
}, nil
}
func (cg *ConfigGenerator) InitiateAndConfigure(conf *config.FaucetConfig) (*types.Faucet, error) {
initFaucet, err := cg.Initiate(conf)
if err != nil {
return nil, fmt.Errorf("failed to init faucet for %q: %w", conf.Name, err)
}
if conf.Template == "" {
return initFaucet, nil
}
configTemplate, err := NewConfigTemplate(conf.Template)
if err != nil {
return nil, fmt.Errorf("failed to create new template config for %q: %w", conf.Name, err)
}
if err := cg.OverwriteConfig(initFaucet, configTemplate); err != nil {
return nil, fmt.Errorf("failed to override config for %q: %w", conf.Name, err)
}
return initFaucet, nil
}
func (cg *ConfigGenerator) Initiate(conf *config.FaucetConfig) (*types.Faucet, error) {
if err := os.MkdirAll(cg.homeDir, os.ModePerm); err != nil {
return nil, err
}
walletPassFilePath := path.Join(cg.homeDir, "faucet-wallet-pass.txt")
if err := os.WriteFile(walletPassFilePath, []byte(conf.Pass), 0644); err != nil {
return nil, fmt.Errorf("failed to write faucet wallet passphrase to file: %w", err)
}
initOut, err := cg.initiateFaucet(cg.homeDir, walletPassFilePath)
if err != nil {
return nil, fmt.Errorf("failed to initiated faucet %q: %w", conf.Name, err)
}
return &types.Faucet{
GeneratedService: types.GeneratedService{
Name: fmt.Sprintf("%s-faucet", cg.conf.Network.Name),
ConfigFilePath: initOut.FaucetConfigFilePath,
HomeDir: cg.homeDir,
},
PublicKey: initOut.PublicKey,
WalletFilePath: initOut.FaucetWalletFilePath,
WalletPassFilePath: walletPassFilePath,
BinaryPath: *cg.conf.VegaBinary,
}, nil
}
func (cg ConfigGenerator) OverwriteConfig(fc *types.Faucet, configTemplate *template.Template) error {
templateCtx := ConfigTemplateContext{
HomeDir: cg.homeDir,
PublicKey: fc.PublicKey,
}
buff := bytes.NewBuffer([]byte{})
if err := configTemplate.Execute(buff, templateCtx); err != nil {
return fmt.Errorf("failed to execute template for faucet: %w", err)
}
overrideConfig := faucet.Config{}
if _, err := toml.NewDecoder(buff).Decode(&overrideConfig); err != nil {
return fmt.Errorf("failed decode override config: %w", err)
}
configFilePath := fc.ConfigFilePath
defaultConfig := faucet.NewDefaultConfig()
if err := paths.ReadStructuredFile(configFilePath, &defaultConfig); err != nil {
return fmt.Errorf("failed to read configuration file at %s: %w", configFilePath, err)
}
if err := mergo.Merge(&overrideConfig, defaultConfig); err != nil {
return fmt.Errorf("failed to merge configs: %w", err)
}
if err := paths.WriteStructuredFile(configFilePath, overrideConfig); err != nil {
return fmt.Errorf("failed to write configuration file for faucet at %s: %w", configFilePath, err)
}
return nil
}