|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/caarlos0/env/v11" |
| 7 | + shapp "github.com/flant/shell-operator/pkg/app" |
| 8 | +) |
| 9 | + |
| 10 | +type appConfig struct { |
| 11 | + TmpDir string `env:"TMP_DIR"` |
| 12 | + PrometheusMetricsPrefix string `env:"PROMETHEUS_METRICS_PREFIX"` |
| 13 | +} |
| 14 | + |
| 15 | +func newAppConfig() *appConfig { |
| 16 | + return &appConfig{} |
| 17 | +} |
| 18 | + |
| 19 | +type Config struct { |
| 20 | + SOp *shapp.Config `env:"-"` |
| 21 | + AppConfig *appConfig `envPrefix:"ADDON_OPERATOR_"` |
| 22 | + |
| 23 | + ready bool |
| 24 | +} |
| 25 | + |
| 26 | +func newConfig() *Config { |
| 27 | + return &Config{ |
| 28 | + AppConfig: newAppConfig(), |
| 29 | + SOp: shapp.MustGetConfig(), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (cfg *Config) Parse() error { |
| 34 | + if cfg.ready { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + opts := env.Options{ |
| 39 | + Prefix: "", |
| 40 | + } |
| 41 | + |
| 42 | + err := env.ParseWithOptions(cfg, opts) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("failed to parse config: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (cfg *Config) InitConfig() { |
| 51 | + cfg.OverrideDefaults() |
| 52 | + cfg.SetupGlobalVars() |
| 53 | + |
| 54 | + cfg.SetReady() |
| 55 | +} |
| 56 | + |
| 57 | +func (cfg *Config) OverrideDefaults() { |
| 58 | + if cfg.IsReady() { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + setIfNotEmpty(&shapp.TempDir, DefaultTempDir) |
| 63 | + setIfNotEmpty(&shapp.PrometheusMetricsPrefix, DefaultPrometheusMetricsPrefix) |
| 64 | + setIfNotEmpty(&shapp.DebugUnixSocket, DefaultDebugUnixSocket) |
| 65 | +} |
| 66 | + |
| 67 | +func (cfg *Config) SetupGlobalVars() { |
| 68 | + if cfg.IsReady() { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + cfg.SOp.SetupGlobalVars() |
| 73 | + |
| 74 | + setIfNotEmpty(&shapp.TempDir, cfg.AppConfig.TmpDir) |
| 75 | + setIfNotEmpty(&shapp.PrometheusMetricsPrefix, cfg.AppConfig.PrometheusMetricsPrefix) |
| 76 | +} |
| 77 | + |
| 78 | +func (cfg *Config) IsReady() bool { |
| 79 | + return cfg.ready |
| 80 | +} |
| 81 | + |
| 82 | +func (cfg *Config) SetReady() { |
| 83 | + cfg.SOp.SetReady() |
| 84 | + cfg.ready = true |
| 85 | +} |
| 86 | + |
| 87 | +var configInstance *Config |
| 88 | + |
| 89 | +func MustGetConfig() *Config { |
| 90 | + cfg, err := GetConfig() |
| 91 | + if err != nil { |
| 92 | + panic(err) |
| 93 | + } |
| 94 | + |
| 95 | + return cfg |
| 96 | +} |
| 97 | + |
| 98 | +func MustGetAndInitConfig() *Config { |
| 99 | + cfg, err := GetAndInitConfig() |
| 100 | + if err != nil { |
| 101 | + panic(err) |
| 102 | + } |
| 103 | + |
| 104 | + return cfg |
| 105 | +} |
| 106 | + |
| 107 | +func GetConfig() (*Config, error) { |
| 108 | + if configInstance != nil { |
| 109 | + return configInstance, nil |
| 110 | + } |
| 111 | + |
| 112 | + cfg := newConfig() |
| 113 | + err := cfg.Parse() |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + configInstance = cfg |
| 119 | + |
| 120 | + return configInstance, nil |
| 121 | +} |
| 122 | + |
| 123 | +func GetAndInitConfig() (*Config, error) { |
| 124 | + cfg, err := GetConfig() |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + |
| 129 | + cfg.InitConfig() |
| 130 | + |
| 131 | + return cfg, nil |
| 132 | +} |
| 133 | + |
| 134 | +func setIfNotEmpty[T comparable](v *T, env T) { |
| 135 | + if !isZero(env) { |
| 136 | + *v = env |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func setSliceIfNotEmpty[T any](v *[]T, env []T) { |
| 141 | + if len(env) != 0 { |
| 142 | + *v = env |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func isZero[T comparable](v T) bool { |
| 147 | + return v == *new(T) |
| 148 | +} |
0 commit comments