...
1 package dsdssandboxes
2
3 import (
4 "io/fs"
5 "os"
6 "path/filepath"
7 "slices"
8
9 "gopkg.in/yaml.v3"
10 )
11
12 type BillingConf struct {
13 Spec Spec `yaml:"spec"`
14 Metadata Metadata `yaml:"metadata"`
15 }
16
17 type Metadata struct {
18 Labels Labels `yaml:"labels"`
19 Name string `yaml:"name"`
20 }
21
22 type Labels struct {
23 BillingRef string `yaml:"billing-reference"`
24 }
25
26 type Spec struct {
27 Name string `yaml:"name"`
28 BillingAccountRef BillingAccountRef `yaml:"billingAccountRef"`
29 }
30
31 type BillingAccountRef struct {
32 External string `yaml:"external"`
33 }
34
35 func (b *BillingConf) Load(configPath string) error {
36 yamlFile, err := os.ReadFile(configPath)
37 if err != nil {
38 return err
39 }
40
41 return yaml.Unmarshal(yamlFile, b)
42 }
43
44
45
46 func GetBillingConfs(projectsFilePath string) (map[string]*BillingConf, error) {
47 configDirs, err := os.ReadDir(projectsFilePath)
48 if err != nil {
49 return nil, err
50 }
51
52 configDirs = filterDirsOnly(configDirs)
53 configDirs = filterNotExcluded(configDirs)
54
55 return iterateConfigDirs(projectsFilePath, configDirs)
56 }
57
58 func filterDirsOnly(fileList []fs.DirEntry) (ret []fs.DirEntry) {
59 for _, file := range fileList {
60 if file.IsDir() {
61 ret = append(ret, file)
62 }
63 }
64 return ret
65 }
66
67 func filterNotExcluded(fileList []fs.DirEntry) (ret []fs.DirEntry) {
68 for _, file := range fileList {
69 if !slices.Contains(IgnoreDirectories, file.Name()) {
70 ret = append(ret, file)
71 }
72 }
73 return ret
74 }
75
76 func iterateConfigDirs(projectsDir string, fileList []fs.DirEntry) (map[string]*BillingConf, error) {
77 ret := make(map[string]*BillingConf)
78 for _, dir := range fileList {
79 path := filepath.Join(projectsDir, dir.Name(), ProjectYamlFilename)
80 conf := &BillingConf{}
81 err := conf.Load(path)
82
83 if err != nil {
84 return nil, err
85 }
86 ret[conf.Metadata.Name] = conf
87 }
88 return ret, nil
89 }
90
View as plain text