-
Notifications
You must be signed in to change notification settings - Fork 38
/
infra_as_code.go
66 lines (59 loc) · 1.17 KB
/
infra_as_code.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
package core
import (
"os"
"path/filepath"
)
type InfraFiles struct {
Name string
Files ConcurrentMap[string, File]
}
var InfraAsCodeKind = "infra_as_code"
func (iac *InfraFiles) Key() ResourceKey {
return ResourceKey{
Name: iac.Name,
Kind: InfraAsCodeKind,
}
}
func (iac *InfraFiles) OutputTo(dest string) error {
errs := make(chan error)
files := iac.Files.Values()
for idx := range files {
go func(f File) {
path := filepath.Join(dest, f.Path())
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0777)
if err != nil {
errs <- err
return
}
file, err := os.OpenFile(path, os.O_RDWR, 0777)
if os.IsNotExist(err) {
file, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0777)
} else if err == nil {
ovr, ok := f.(NonOverwritable)
if ok && !ovr.Overwrite(file) {
errs <- nil
return
}
err = file.Truncate(0)
}
if err != nil {
errs <- err
return
}
_, err = f.WriteTo(file)
file.Close()
errs <- err
}(files[idx])
}
for i := 0; i < len(files); i++ {
err := <-errs
if err != nil {
return err
}
}
return nil
}
func (unit *InfraFiles) Add(f File) {
unit.Files.Set(f.Path(), f)
}