-
Notifications
You must be signed in to change notification settings - Fork 205
/
config.go
52 lines (41 loc) · 1.17 KB
/
config.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
package config
import (
"errors"
"fmt"
"os"
"gopkg.in/yaml.v3"
ptypes "github.com/akash-network/akash-api/go/node/provider/v1beta3"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
)
var (
ErrDuplicatedAttribute = errors.New("provider: duplicated attribute")
)
// Config is the struct that stores provider config
type Config struct {
Host string `json:"host" yaml:"host"`
Info ptypes.ProviderInfo `json:"info" yaml:"info"`
Attributes types.Attributes `json:"attributes" yaml:"attributes"`
}
// GetAttributes returns config attributes into key value pairs
func (c Config) GetAttributes() types.Attributes {
return c.Attributes
}
// ReadConfigPath reads and parses file
func ReadConfigPath(path string) (Config, error) {
buf, err := os.ReadFile(path)
if err != nil {
return Config{}, err
}
var val Config
if err := yaml.Unmarshal(buf, &val); err != nil {
return Config{}, err
}
dups := make(map[string]string)
for _, attr := range val.Attributes {
if _, exists := dups[attr.Key]; exists {
return Config{}, fmt.Errorf("%w: %s", ErrDuplicatedAttribute, attr.Key)
}
dups[attr.Key] = attr.Value
}
return val, err
}