-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
116 lines (105 loc) · 2.81 KB
/
parse.go
File metadata and controls
116 lines (105 loc) · 2.81 KB
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
package iac
import (
"bytes"
"fmt"
"github.com/nullstone-io/iac/config"
yaml2 "github.com/nullstone-io/iac/yaml"
"gopkg.in/yaml.v3"
"io"
"os"
"path"
"path/filepath"
"strings"
)
type InvalidYamlError struct {
ParseContext string
FileName string
Err error
}
func (e InvalidYamlError) Error() string {
return fmt.Sprintf("Error parsing YAML file (%s) from (%s): %v", e.FileName, e.ParseContext, e.Err)
}
func (e InvalidYamlError) Unwrap() error {
return e.Err
}
func ParseMap(repoUrl, repoName string, files map[string]string) (ConfigFiles, error) {
result := ConfigFiles{
RepoUrl: repoUrl,
RepoName: repoName,
Config: nil,
Overrides: map[string]*config.EnvConfiguration{},
}
for filename, raw := range files {
desc, isOverrides := getConfigFileDescription(filename)
parsed, err := ParseConfig(repoUrl, repoName, filename, isOverrides, bytes.NewBufferString(raw))
if err != nil {
return result, err
}
if isOverrides {
result.Overrides[desc] = parsed
} else {
result.Config = parsed
}
}
return result, nil
}
func ParseConfig(repoUrl, repoName, filename string, isOverrides bool, r io.Reader) (*config.EnvConfiguration, error) {
decoder := yaml.NewDecoder(r)
var obj yaml2.EnvConfiguration
if err := decoder.Decode(&obj); err != nil {
return nil, InvalidYamlError{ParseContext: repoName, FileName: filename, Err: err}
}
return config.ConvertConfiguration(repoUrl, repoName, filename, isOverrides, obj), nil
}
func ParseConfigFile(repoUrl, repoName, filename string, isOverrides bool) (*config.EnvConfiguration, error) {
raw, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseConfig(repoUrl, repoName, filename, isOverrides, bytes.NewReader(raw))
}
func ParseConfigDir(repoUrl, repoName, dir string) (*ConfigFiles, error) {
pmr := &ConfigFiles{
RepoUrl: repoUrl,
RepoName: repoName,
Overrides: map[string]*config.EnvConfiguration{},
}
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return pmr, nil
}
if err != nil {
return nil, err
}
for _, entry := range entries {
filename := entry.Name()
if entry.IsDir() || !isYmlFile(filename) {
continue
}
desc, isOverrides := getConfigFileDescription(filename)
ec, err := ParseConfigFile(repoUrl, repoName, filepath.Join(dir, filename), isOverrides)
if err != nil {
return nil, fmt.Errorf("cannot parse config file: %w", err)
}
if isOverrides {
pmr.Overrides[desc] = ec
} else {
pmr.Config = ec
}
}
return pmr, nil
}
func getConfigFileDescription(filepath string) (string, bool) {
_, filename := path.Split(filepath)
woExt := strings.TrimSuffix(filename, path.Ext(filename))
return woExt, woExt != "config"
}
func isYmlFile(filename string) bool {
switch filepath.Ext(filename) {
case ".yml":
return true
case ".yaml":
return true
}
return false
}