Skip to content

Commit

Permalink
feat: sync config fields with actual config
Browse files Browse the repository at this point in the history
  • Loading branch information
ViRb3 committed Feb 27, 2021
1 parent aee619b commit 877fca2
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"io/ioutil"
"ios-signer-service/util"
"log"
"path/filepath"
Expand Down Expand Up @@ -52,12 +54,16 @@ func createDefaultConfig() *Config {
var Current *Config

func Load(fileName string) {
viper.SetConfigName(strings.TrimSuffix(fileName, filepath.Ext(fileName)))
// toml bug: https://github.com/spf13/viper/issues/488
allowedExts := []string{".yml", ".yaml"}
if !isAllowedExt(allowedExts, fileName) {
log.Fatalf("config extension not allowed: %v\n", allowedExts)
}
viper.SetConfigName(strings.TrimSuffix(fileName, filepath.Ext(fileName)))
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

cfg, err := getConfig()
cfg, err := getConfig(fileName)
if err != nil {
log.Fatalln(errors.WithMessage(err, "get config"))
}
Expand All @@ -69,28 +75,38 @@ func Load(fileName string) {
Current = cfg
}

func getConfig() (*Config, error) {
cfg := createDefaultConfig()
func isAllowedExt(allowedExts []string, fileName string) bool {
fileExt := filepath.Ext(fileName)
for _, ext := range allowedExts {
if fileExt == ext {
return true
}
}
return false
}

func getConfig(fileName string) (*Config, error) {
var lateError error
if err := viper.ReadInConfig(); err != nil {
return nil, handleNoConfigFile(cfg)
lateError = errors.New("no file found, generating one")
}
cfg := createDefaultConfig()
// don't use viper.Unmarshal because it doesn't support nested structs: https://github.com/spf13/viper/issues/488
if err := util.Restructure(viper.AllSettings(), cfg); err != nil {
return nil, errors.WithMessage(err, "config restructure")
return nil, errors.WithMessage(err, "restructure")
}
return cfg, nil
}

func handleNoConfigFile(config *Config) error {
defaultMap := make(map[string]interface{})
if err := util.Restructure(config, &defaultMap); err != nil {
return errors.WithMessage(err, "restructure")
cfgBytes, err := yaml.Marshal(cfg)
if err != nil {
return nil, errors.WithMessage(err, "marshal")
}
if err := viper.MergeConfigMap(defaultMap); err != nil {
return errors.WithMessage(err, "merge default config")
if err := ioutil.WriteFile(fileName, cfgBytes, 0666); err != nil {
return nil, errors.WithMessage(err, "write")
}
if err := viper.SafeWriteConfig(); err != nil {
return errors.WithMessage(err, "save config")
if err := viper.ReadInConfig(); err != nil {
return nil, errors.WithMessage(err, "read")
}
if lateError != nil {
return nil, lateError
}
return errors.New("file not present, template generated")
return cfg, nil
}

0 comments on commit 877fca2

Please sign in to comment.