-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
102 lines (91 loc) · 2.42 KB
/
Copy pathconfig.go
File metadata and controls
102 lines (91 loc) · 2.42 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
package bqin
import (
"os"
goconfig "github.com/kayac/go-config"
"github.com/pkg/errors"
)
type Config struct {
QueueName string `yaml:"queue_name"`
Cloud *Cloud `yaml:"cloud"`
Rules []*Rule `yaml:"rules"`
Rule `yaml:",inline"`
}
type Cloud struct {
AWS *AWS `yaml:"aws,omitempty"`
GCP *GCP `yaml:"gcp,omitempty"`
}
type AWS struct {
Region string `yaml:"region,omitempty"`
DisableSSL bool `yaml:"disable_ssl,omitempty"`
S3ForcePathStyle bool `yaml:"s3_force_path_style,omitempty"`
S3Endpoint string `yaml:"s3_endpoint,omitempty"`
SQSEndpoint string `yaml:"sqs_endpoint,omitempty"`
AccessKeyID string `yaml:"access_key_id,omitempty"`
SecretAccessKey string `yaml:"secret_access_key,omitempty"`
DisableShardConfigState bool `yaml:"disable_shard_config_state,omitempty"`
}
type GCP struct {
WithoutAuthentication bool `yaml:"without_authentication,omitempty"`
BigQueryEndpoint string `yaml:"big_query_endpoint,omitempty"`
CloudStorageEndpoint string `yaml:"cloud_storage_endpoint,omitempty"`
Base64Credential Base64String `yaml:"base64_credential"`
}
func NewDefaultConfig() *Config {
return &Config{
Cloud: &Cloud{
AWS: &AWS{
Region: os.Getenv("AWS_REGION"),
DisableSSL: false,
S3ForcePathStyle: false,
S3Endpoint: "",
SQSEndpoint: "",
},
GCP: &GCP{
WithoutAuthentication: false,
},
},
}
}
func LoadConfig(path string) (*Config, error) {
conf := NewDefaultConfig()
err := goconfig.LoadWithEnv(conf, path)
if err != nil {
return nil, err
}
if err := conf.Validate(); err != nil {
return nil, err
}
return conf, nil
}
func (c *Config) Validate() error {
if c.QueueName == "" {
return errors.New("queue_name is not defined")
}
if err := c.Cloud.Validate(); err != nil {
return errors.Wrap(err, "cloud is invalid")
}
if len(c.Rules) == 0 {
return errors.New("rules is not defined")
}
for i, dst := range c.Rules {
other := c.Rule.Clone()
dst.MergeIn(other)
if err := dst.Validate(); err != nil {
return errors.Wrapf(err, "rule[%d]", i)
}
c.Rules[i] = dst
}
return nil
}
func (c *Cloud) Validate() error {
if c == nil {
return errors.New("not defined")
}
if c.AWS == nil {
return errors.New("aws config is not defined")
}
if c.GCP == nil {
return errors.New("gcp config is not defined")
}
return nil
}