/
provider.go
57 lines (47 loc) · 1.24 KB
/
provider.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
package config
import (
"fmt"
"os"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/ghodss/yaml"
"github.com/oliveagle/jsonpath"
"github.com/pkg/errors"
)
// NewProvider missing godoc
func NewProvider(fileName string) *Provider {
return &Provider{
fileName: fileName,
}
}
// Provider missing godoc
type Provider struct {
fileName string
cachedConfig map[string]interface{}
}
// Load missing godoc
func (p *Provider) Load() error {
b, err := os.ReadFile(p.fileName)
if err != nil {
return errors.Wrapf(err, "while reading file %s", p.fileName)
}
out := map[string]interface{}{}
if err := yaml.Unmarshal(b, &out); err != nil {
return errors.Wrap(err, "while unmarshalling YAML")
}
p.cachedConfig = out
return nil
}
func (p *Provider) getValueForJSONPath(path string) (interface{}, error) {
if p.cachedConfig == nil {
return nil, apperrors.NewInternalError("required configuration not loaded")
}
jPath := fmt.Sprintf("$.%s", path)
res, err := jsonpath.JsonPathLookup(p.cachedConfig, jPath)
if err != nil {
return nil, errors.Wrapf(err, "while searching configuration using path %s", jPath)
}
if res == nil {
return nil, apperrors.NewValueNotFoundInConfigurationError()
}
return res, nil
}