forked from NethermindEth/sedge
/
generation.go
90 lines (78 loc) · 2.92 KB
/
generation.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
/*
Copyright 2022 Nethermind
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actions
import (
"os"
"path/filepath"
"github.com/Melvillian/sedge/configs"
"github.com/Melvillian/sedge/internal/pkg/generate"
log "github.com/sirupsen/logrus"
)
type GenerateOptions struct {
GenerationData generate.GenData
GenerationPath string
}
func (s *sedgeActions) Generate(options GenerateOptions) (generate.GenData, error) {
// Create scripts directory if not exists
if _, err := os.Stat(options.GenerationPath); os.IsNotExist(err) {
err = os.MkdirAll(options.GenerationPath, 0o755)
if err != nil {
return options.GenerationData, err
}
}
// Setup custom configs files if needed
customConfigsPaths, err := generate.CustomNetworkConfigs(options.GenerationPath, options.GenerationData.Network, generate.CustomConfigsSources{
ChainSpecSrc: options.GenerationData.CustomChainSpecPath,
NetworkConfigSrc: options.GenerationData.CustomNetworkConfigPath,
GenesisSrc: options.GenerationData.CustomGenesisPath,
DeployBlockSrc: options.GenerationData.CustomDeployBlockPath,
})
if err != nil {
return options.GenerationData, err
}
options.GenerationData.CustomChainSpecPath = customConfigsPaths.ChainSpecPath
options.GenerationData.CustomNetworkConfigPath = customConfigsPaths.NetworkConfigPath
options.GenerationData.CustomGenesisPath = customConfigsPaths.GenesisPath
options.GenerationData.CustomDeployBlockPath = customConfigsPaths.DeployBlockPath
log.Info(configs.GeneratingDockerComposeScript)
// open output file
out, err := os.Create(filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName))
if err != nil {
return options.GenerationData, err
}
defer out.Close()
err = generate.ComposeFile(&options.GenerationData, out)
if err != nil {
return options.GenerationData, err
}
log.Info(configs.GeneratedDockerComposeScript)
log.Info(configs.GeneratingEnvFile)
// open output file
outEnv, err := os.Create(filepath.Join(options.GenerationPath, configs.DefaultEnvFileName))
if err != nil {
return options.GenerationData, err
}
defer outEnv.Close()
err = generate.EnvFile(&options.GenerationData, outEnv)
if err != nil {
return options.GenerationData, err
}
log.Info(configs.GeneratedEnvFile)
log.Info(configs.CleaningGeneratedFiles)
err = generate.CleanGenerated(options.GenerationPath)
if err != nil {
return options.GenerationData, err
}
log.Info(configs.CleanedGeneratedFiles)
return options.GenerationData, nil
}