Skip to content

Commit

Permalink
fix(config): mkdir before writing config
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 4394217 commit 4727a5d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func ParseConfig(path string) (*Config, error) {

// WriteConfig writes the configuration to the given file.
func WriteConfig(path string, cfg *Config) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
return os.WriteFile(path, []byte(newConfigFile(cfg)), 0o600) // nolint: errcheck
}

Expand Down Expand Up @@ -162,25 +165,30 @@ func DefaultConfig() *Config {
ListenAddr: ":8081",
},
}

cp := filepath.Join(cfg.DataPath, "config.yaml")
f, err := os.Open(cp)
if err == nil {
defer f.Close() // nolint: errcheck
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
log.Error("failed to decode config", "err", err)
}
} else {
defer func() {
os.WriteFile(cp, []byte(newConfigFile(cfg)), 0o600) // nolint: errcheck
}()
}

// Override with environment variables
if err := env.Parse(cfg, env.Options{
Prefix: "SOFT_SERVE_",
}); err != nil {
log.Fatal(err)
}

// Write config if it doesn't exist
if _, err := os.Stat(cp); os.IsNotExist(err) {
if err := WriteConfig(cp, cfg); err != nil {
log.Fatal("failed to write config", "err", err)
}
}

if err := cfg.init(); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 4727a5d

Please sign in to comment.