Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parameter to disable configuration file automatic creation #45

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading