package config import ( "fmt" "os" "github.com/goccy/go-yaml" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) const DefaultConfigFileLocation = "/etc/sys-exporter/config.yml" type logConfigJson struct { Level string `yaml:"level"` } func (c logConfigJson) logConfig() (LogConfig, error) { lvl, err := zap.ParseAtomicLevel(c.Level) if err != nil { return LogConfig{}, err } return LogConfig{ Level: lvl, }, nil } type appConfigJson struct { LogConfig logConfigJson `yaml:"logging"` } func (c appConfigJson) appConfig() (AppConfig, error) { lc, err := c.LogConfig.logConfig() if err != nil { return AppConfig{}, err } return AppConfig{ LogConfig: lc, }, nil } type LogConfig struct { Level zap.AtomicLevel `yaml:"level"` } func (c *LogConfig) ZapConfig() zap.Config { fmt.Println(c.Level) zapC := zap.Config{ Level: c.Level, Development: true, // TODO: remove later Encoding: "console", EncoderConfig: zap.NewDevelopmentEncoderConfig(), OutputPaths: []string{"stdout"}, ErrorOutputPaths: []string{"stderr"}, } zapC.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder return zapC } type AppConfig struct { LogConfig `yaml:"logging"` } func New(filepath string) (*AppConfig, error) { fileContent, err := os.ReadFile(filepath) if err != nil { return nil, fmt.Errorf("failed to open config file: %w", err) } var v appConfigJson if err := yaml.Unmarshal(fileContent, &v); err != nil { return nil, fmt.Errorf("failed to parse config file: %w", err) } trueConf, err := v.appConfig() if err != nil { return nil, fmt.Errorf("failed to validate config: %w", err) } return &trueConf, nil }