/
pyo
/
builder
Обзор
Документация
Войти
/
pyo
/
builder
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
develop
pkg/config/config.go
85 строк
3 KB
ilovepitsa
draft version
25 июн 2025, 18:29
25 июн 2025, 18:29
581515a
Код
Авторство
О чём код?
package config import ( "fmt" "path/filepath" "strings" "time" "github.com/spf13/viper" "go.uber.org/zap" ) type Config struct { ToBuildComponentConfig `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"` BuildKitConfig `json:"buildkit" env:"buildkit" yaml:"buildkit" toml:"buildkit" mapstructure:"buildkit" jsonschema:"buildkit"` Logger *zap.Config `json:"logger" env:"logger" yaml:"logger" toml:"logger" mapstructure:"logger" jsonschema:"logger"` } 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 ToBuildComponentConfig 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"` UnpackDir string `json:"unpack" env:"unpack" yaml:"unpack" toml:"unpack" mapstructure:"unpack" jsonschema:"unpack"` } type BuildKitConfig struct { Conn string `json:"conn" env:"conn" yaml:"conn" toml:"conn" mapstructure:"conn" jsonschema:"conn"` } 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"` } 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 }