-
Notifications
You must be signed in to change notification settings - Fork 14
/
files.go
45 lines (37 loc) · 968 Bytes
/
files.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
package common
import (
"fmt"
"os"
"path/filepath"
"github.com/anz-bank/sysl-go/validator"
"gopkg.in/yaml.v2"
)
func FindConfigFilename(cfgDir, prefix string) string {
for _, ext := range []string{".json", ".yaml"} {
filename := filepath.Join(cfgDir, prefix+ext)
if _, err := os.Stat(filename); err == nil {
return filename
}
}
return ""
}
func LoadAndValidateFromYAMLFileName(filename string, out validator.Validator) error {
cfgBytes, err := os.ReadFile(filename)
if err != nil {
return err
}
err = loadAndValidateFromYAML(cfgBytes, out)
if err != nil {
return fmt.Errorf("error encountered with YAML file %v: %v", filename, err)
}
return nil
}
func loadAndValidateFromYAML(b []byte, out validator.Validator) error {
if err := yaml.UnmarshalStrict(b, out); err != nil {
return fmt.Errorf("failed to parse: %v", err)
}
if err := out.Validate(); err != nil {
return fmt.Errorf("failed to validate: %v", err)
}
return nil
}