forked from joselitofilho/aws-terraform-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
84 lines (61 loc) · 1.99 KB
/
s3.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
package s3
import (
_ "embed"
"fmt"
"os"
"path"
"strings"
"github.com/joselitofilho/aws-terraform-generator/internal/fmtcolor"
"github.com/joselitofilho/aws-terraform-generator/internal/generators"
"github.com/joselitofilho/aws-terraform-generator/internal/generators/config"
generatorserrs "github.com/joselitofilho/aws-terraform-generator/internal/generators/errors"
"github.com/joselitofilho/aws-terraform-generator/internal/utils"
)
type Data struct {
Name string
ExpirationDays int
}
type S3 struct {
configFileName string
output string
}
func NewS3(configFileName, output string) *S3 {
return &S3{configFileName: configFileName, output: output}
}
func (s *S3) Build() error {
yamlParser := config.NewYAML(s.configFileName)
yamlConfig, err := yamlParser.Parse()
if err != nil {
return fmt.Errorf("%w: %w", generatorserrs.ErrYAMLParser, err)
}
modPath := path.Join(s.output, "mod")
_ = os.MkdirAll(modPath, os.ModePerm)
result := make([]string, 0, len(yamlConfig.Buckets))
templates := utils.MergeStringMap(defaultTfTemplateFiles,
generators.CreateTemplatesMap(yamlConfig.OverrideDefaultTemplates.S3Bucket))
tg := generators.NewGenerator()
for i := range yamlConfig.Buckets {
conf := yamlConfig.Buckets[i]
data := Data{
Name: conf.Name,
ExpirationDays: conf.ExpirationDays,
}
if len(conf.Files) > 0 {
filesConf := generators.CreateFilesMap(conf.Files)
generators.MustGenerateFiles(tg, nil, filesConf, data, modPath)
fmtcolor.White.Printf("S3 '%s' has been generated successfully\n", conf.Name)
continue
}
output, err := tg.Build(data, "s3-tf-template", templates[filenameS3tf])
if err != nil {
return fmt.Errorf("%w", err)
}
result = append(result, output)
}
if len(result) > 0 {
outputFile := path.Join(modPath, filenameS3tf)
generators.MustGenerateFile(tg, nil, filenameS3tf, strings.Join(result, "\n"), outputFile, Data{})
fmtcolor.White.Println("S3 has been generated successfully")
}
return nil
}