1
This commit is contained in:
53
internal/config/config.go
Normal file
53
internal/config/config.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInternal = errors.New("can not load config")
|
||||
ErrMarshaling = errors.New("can not marshal config path")
|
||||
)
|
||||
|
||||
func MustLoad(version, commit, date string) *Config {
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg.Build.Version = version
|
||||
cfg.Build.Commit = commit
|
||||
cfg.Build.Date = date
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func loadConfig() (*Config, error) {
|
||||
if _, err := os.Stat(ConfigBasePath); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("no config file found")
|
||||
}
|
||||
|
||||
var localConfig LocalDeploy
|
||||
|
||||
if err := cleanenv.ReadConfig(ConfigBasePath, &localConfig); err != nil {
|
||||
return nil, fmt.Errorf("error reading local config: %w", err)
|
||||
}
|
||||
|
||||
for _, env := range localConfig.Deploy.Env {
|
||||
if err := os.Setenv(env.Name, env.Value); err != nil {
|
||||
return nil, fmt.Errorf("error setting env %s: %w", env.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
|
||||
if err := cleanenv.ReadConfig(ConfigBasePath, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
Reference in New Issue
Block a user