-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
126 lines (107 loc) · 3.4 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
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
117
118
119
120
121
122
123
124
125
126
package config
import (
"bytes"
"os"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
// Config
type Config struct {
Version string `yaml:"version" validate:"required"`
Database Database `yaml:"database" validate:"required"`
DataSources map[string]DataSource `yaml:"datasources,omitempty"`
Contracts map[string]Contract `yaml:"contracts,omitempty"`
Hasura *Hasura `yaml:"hasura,omitempty" validate:"omitempty"`
Prometheus *Prometheus `yaml:"prometheus,omitempty" validate:"omitempty"`
}
// Substitute -
func (c *Config) Substitute() error {
return nil
}
// DataSource -
type DataSource struct {
Kind string `yaml:"kind"`
URL string `yaml:"url" validate:"required,url"`
Credentials *Credentials `yaml:"credentials,omitempty" validate:"omitempty"`
Timeout uint `yaml:"timeout" validate:"omitempty"`
}
// Contracts -
type Contract struct {
Address string `yaml:"address" validate:"required,len=36"`
TypeName string `yaml:"typename"`
}
// Database
type Database struct {
Path string `yaml:"path,omitempty"`
Kind string `yaml:"kind" validate:"required,oneof=sqlite postgres mysql clickhouse elasticsearch"`
Host string `yaml:"host" validate:"required_with=Port User Database"`
Port int `yaml:"port" validate:"required_with=Host User Database,gt=-1,lt=65535"`
User string `yaml:"user" validate:"required_with=Host Port Database"`
Password string `yaml:"password"`
Database string `yaml:"database" validate:"required_with=Host Port User"`
SchemaName string `yaml:"schema_name"`
}
// Hasura -
type Hasura struct {
URL string `yaml:"url" validate:"required,url"`
Secret string `yaml:"admin_secret" validate:"required"`
Source string `yaml:"source" validate:"omitempty"`
RowsLimit uint64 `yaml:"select_limit" validate:"gt=0"`
EnableAggregations bool `yaml:"allow_aggregation"`
AddSource bool `yaml:"add_source"`
Rest *bool `yaml:"rest"`
}
// UnmarshalYAML -
func (h *Hasura) UnmarshalYAML(unmarshal func(interface{}) error) error {
h.Source = "default"
type plain Hasura
return unmarshal((*plain)(h))
}
// Prometheus -
type Prometheus struct {
URL string `yaml:"url" validate:"required"`
}
// Load - load default config from `filename`
func Load(filename string) (*Config, error) {
buf, err := readFile(filename)
if err != nil {
return nil, err
}
var c Config
if err := yaml.NewDecoder(buf).Decode(&c); err != nil {
return nil, err
}
if err := c.Substitute(); err != nil {
return nil, errors.Wrap(err, "Substitute")
}
return &c, validator.New().Struct(c)
}
// Parse -
func Parse(filename string, output Configurable) error {
buf, err := readFile(filename)
if err != nil {
return err
}
if err := yaml.NewDecoder(buf).Decode(output); err != nil {
return err
}
if err := output.Substitute(); err != nil {
return err
}
return validator.New().Struct(output)
}
func readFile(filename string) (*bytes.Buffer, error) {
if filename == "" {
return nil, errors.Errorf("you have to provide configuration filename")
}
data, err := os.ReadFile(filename)
if err != nil {
return nil, errors.Wrapf(err, "reading file %s", filename)
}
expanded, err := expandVariables(data)
if err != nil {
return nil, err
}
return bytes.NewBuffer(expanded), nil
}