Skip to content

Commit

Permalink
Add a parameter to disable configuration file automatic creation (#45)
Browse files Browse the repository at this point in the history
* added server ConfigurationFile flag "create_file"

* add IsNotExist error handling to file open

* Changed to use "Open" instead of "Touch" with flags
  • Loading branch information
superdarki authored Jan 4, 2025
1 parent c335010 commit cb6f528
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 15 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func (cp ConfigurationParser) String() string {
// ConfigurationFile defines a configuration file for the server startup. These
// will be looped over and modified before the server finishes booting.
type ConfigurationFile struct {
FileName string `json:"file"`
Parser ConfigurationParser `json:"parser"`
Replace []ConfigurationFileReplacement `json:"replace"`
FileName string `json:"file"`
Parser ConfigurationParser `json:"parser"`
Replace []ConfigurationFileReplacement `json:"replace"`
AllowCreateFile bool `json:"create_file"` // assumed true by unmarshal as it was the original behaviour

// Tracks Wings' configuration so that we can quickly get values
// out of it when variables request it.
Expand Down Expand Up @@ -137,6 +138,17 @@ func (f *ConfigurationFile) UnmarshalJSON(data []byte) error {
f.Replace = []ConfigurationFileReplacement{}
}

// test if "create_file" exists, if not just assume true
if val, exists := m["create_file"]; exists && val != nil {
if err := json.Unmarshal(*val, &f.AllowCreateFile); err != nil {
log.WithField("file", f.FileName).WithField("error", err).Warn("create_file unmarshal failed")
f.AllowCreateFile = true
}
} else {
log.WithField("file", f.FileName).Debug("create_file not specified assumed true")
f.AllowCreateFile = true
}

return nil
}

Expand Down
15 changes: 13 additions & 2 deletions server/config_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"os"
"runtime"

"fmt"
Expand Down Expand Up @@ -43,9 +44,19 @@ func (s *Server) UpdateConfigurationFiles() {

pool.Submit(func() {
filename := replaceParserConfigPathVariables(f.FileName, s.Config().EnvVars)
file, err := s.Filesystem().UnixFS().Touch(filename, ufs.O_RDWR|ufs.O_CREATE, 0o644)
file, err := func() (ufs.File, error) {
if f.AllowCreateFile {
return s.Filesystem().UnixFS().Touch(filename, ufs.O_RDWR|ufs.O_CREATE, 0o644)
}
return s.Filesystem().UnixFS().Open(filename)
}()
if err != nil {
s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration")
log := s.Log().WithField("file_name", filename)
if os.IsNotExist(err) && !f.AllowCreateFile {
log.Debug("file not created")
} else {
log.WithField("error", err).Error("failed to open file for configuration")
}
return
}
defer file.Close()
Expand Down

0 comments on commit cb6f528

Please sign in to comment.