/
pyo
/
updater
Обзор
Документация
Войти
/
pyo
/
updater
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
develop
pkg/config/config.go
103 строки
4 KB
ilovepitsa
restore from another repo
16 июн 2025, 13:25
16 июн 2025, 13:25
d7455bb
Код
Авторство
О чём код?
package config import ( "fmt" "path/filepath" "strings" "time" "github.com/spf13/viper" "go.uber.org/zap" ) type DurationJSON struct { time.Duration } func (d *DurationJSON) UnmarshalText(text []byte) error { parsed, err := time.ParseDuration(string(text)) if err != nil { return err } d.Duration = parsed return nil } type Config struct { BuildComponentConfig `json:"pre-build" env:"pre-build" yaml:"pre-build" toml:"pre-build" mapstructure:"pre-build" jsonschema:"pre-build"` BuildedComponentConfig `json:"post-build" env:"post-build" yaml:"post-build" toml:"post-build" mapstructure:"post-build" jsonschema:"post-build"` DockerBuilderConfig `json:"builder" env:"builder" yaml:"builder" toml:"builder" mapstructure:"builder" jsonschema:"builder"` PostgresConfig `json:"pg" env:"pg" yaml:"pg" toml:"pg" mapstructure:"pg" jsonschema:"pg"` ServerConfig `json:"server" env:"server" yaml:"server" toml:"server" mapstructure:"server" jsonschema:"server"` Logger *zap.Config `json:"logger" env:"logger" yaml:"logger" toml:"logger" mapstructure:"logger" jsonschema:"logger"` } type ServerConfig struct { Host string `json:"host" env:"host" yaml:"host" toml:"host" mapstructure:"host" jsonschema:"host"` Port int `json:"port" env:"port" yaml:"port" toml:"port" mapstructure:"port" jsonschema:"port"` } type BuildedComponentConfig struct { Dir string `json:"dir" env:"dir" yaml:"dir" toml:"dir" mapstructure:"dir" jsonschema:"dir"` Ext string `json:"ext" env:"ext" yaml:"ext" toml:"ext" mapstructure:"ext" jsonschema:"ext"` } type BuildComponentConfig struct { TempDir string `json:"temp" env:"temp" yaml:"temp" toml:"temp" mapstructure:"temp" jsonschema:"temp"` FileMode uint32 `json:"mode" env:"mode" yaml:"mode" toml:"mode" mapstructure:"mode" jsonschema:"mode"` } type DockerBuilderConfig struct { BuildInterval time.Duration `json:"interval" env:"interval" yaml:"interval" toml:"interval" mapstructure:"interval" jsonschema:"interval"` TmplPattern string `json:"tmpl_pattern" env:"tmpl_pattern" yaml:"tmpl_pattern" toml:"tmpl_pattern" mapstructure:"tmpl_pattern" jsonschema:"tmpl_pattern"` TmplName string `json:"tmpl_name" env:"tmpl_name" yaml:"tmpl_name" toml:"tmpl_name" mapstructure:"tmpl_name" jsonschema:"tmpl_name"` BaseImage string `json:"image" env:"image" yaml:"image" toml:"image" mapstructure:"image" jsonschema:"image"` PackageManager string `json:"pm" env:"pm" yaml:"pm" toml:"pm" mapstructure:"pm" jsonschema:"pm"` DockFileMode uint32 `json:"mode" env:"mode" yaml:"mode" toml:"mode" mapstructure:"mode" jsonschema:"mode"` BuildedDir string `json:"dir" env:"dir" yaml:"dir" toml:"dir" mapstructure:"dir" jsonschema:"dir"` } type PostgresConfig struct { ConnString string `json:"conn" env:"conn" yaml:"conn" toml:"conn" mapstructure:"conn" jsonschema:"conn"` } func (cfg *Config) SetLoggerDefaults() { cfg.Logger = &zap.Config{ Level: zap.NewAtomicLevelAt(zap.WarnLevel), Development: false, DisableCaller: true, DisableStacktrace: true, EncoderConfig: zap.NewProductionEncoderConfig(), Encoding: "json", OutputPaths: []string{"stdout"}, ErrorOutputPaths: []string{"stderr"}, } } func ParseConfig(path string) (*Config, error) { v := viper.New() abs, err := filepath.Abs(path) if err != nil { return nil, err } dir := filepath.Dir(abs) file := filepath.Base(abs) ext := filepath.Ext(abs) fileWithoutExt := strings.TrimSuffix(file, ext) ext = strings.TrimPrefix(ext, ".") v.SetConfigName(fileWithoutExt) v.SetConfigType(ext) v.AddConfigPath(dir) v.AutomaticEnv() if err := v.ReadInConfig(); err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) } var config Config if err := v.Unmarshal(&config); err != nil { return nil, fmt.Errorf("failed to unmarshal config: %v", err) } return &config, nil }