-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
70 lines (58 loc) · 1.99 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
package config
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"strings"
"github.com/practo/klog/v2"
"github.com/practo/tipoca-stream/pkg/kafka"
"github.com/practo/tipoca-stream/pkg/redshift"
"github.com/practo/tipoca-stream/pkg/redshiftloader"
"github.com/practo/tipoca-stream/pkg/s3sink"
)
type Config struct {
Loader redshiftloader.LoaderConfig `yaml:"loader"`
ConsumerGroups []kafka.ConsumerGroupConfig `yaml:"consumerGroups"`
S3Sink s3sink.Config `yaml:"s3sink"`
SchemaRegistryURL string `yaml:"schemaRegistryURL"`
PrometheusURL string `yaml:"prometheusURL"`
Redshift redshift.RedshiftConfig `yaml:"redshift"`
RedshiftGroup *string `yaml:"redshiftGroup,omitempty"`
RedshiftMetrics bool `yaml:"redshiftMetrics"`
Rsk string `yaml:"rsk,omitempty"`
SinkGroup string `yaml:"sinkGroup,omitempty"`
}
func LoadConfig(cmd *cobra.Command) (Config, error) {
c := Config{}
err := viper.BindPFlags(cmd.Flags())
if err != nil {
return c, err
}
configName := "config.yaml"
relativePath := "./cmd/redshiftloader/config"
viper.SetEnvPrefix("loader")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
viper.SetDefault("kafka.version", "2.5.0")
if configFile, _ := cmd.Flags().GetString("config"); configFile != "" {
viper.SetConfigFile(configFile)
configName = configFile
} else {
viper.AddConfigPath(relativePath)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
}
if err := viper.ReadInConfig(); err != nil {
return c, err
}
if err := viper.Unmarshal(&c); err != nil {
return c, err
}
ValidateConfig(c)
klog.Infof("Using config file: %s\n", configName)
klog.V(4).Infof("ConfigFilePath: %s\n", viper.ConfigFileUsed())
klog.V(5).Infof("Config: %+v\n", c)
return c, nil
}
func ValidateConfig(config Config) error {
return nil
}