settings: Use pointer

This commit is contained in:
nemunaire 2022-05-01 21:32:19 +02:00
parent dc64eb549f
commit 15afbb8b87
5 changed files with 13 additions and 13 deletions

View file

@ -92,24 +92,24 @@ func ExistsSettings(settingsPath string) bool {
}
// ReadSettings parses the file at the given location.
func ReadSettings(path string) (FICSettings, error) {
func ReadSettings(path string) (*FICSettings, error) {
var s FICSettings
if fd, err := os.Open(path); err != nil {
return s, err
return nil, err
} else {
defer fd.Close()
jdec := json.NewDecoder(fd)
if err := jdec.Decode(&s); err != nil {
return s, err
return &s, err
}
return s, nil
return &s, nil
}
}
// SaveSettings saves settings at the given location.
func SaveSettings(path string, s FICSettings) error {
func SaveSettings(path string, s *FICSettings) error {
if fd, err := os.Create(path); err != nil {
return err
} else {
@ -139,7 +139,7 @@ func ForceRegeneration() error {
// Giving the location and a callback, this function will first call your reload function
// before returning (if the file can be parsed); then it starts watching modifications made to
// this file. Your callback is then run each time the file is modified.
func LoadAndWatchSettings(settingsPath string, reload func(FICSettings)) {
func LoadAndWatchSettings(settingsPath string, reload func(*FICSettings)) {
// First load of configuration if it exists
if _, err := os.Stat(settingsPath); !os.IsNotExist(err) {
if config, err := ReadSettings(settingsPath); err != nil {
@ -184,7 +184,7 @@ func LoadAndWatchSettings(settingsPath string, reload func(FICSettings)) {
}
}
func tryReload(settingsPath string, reload func(FICSettings)) {
func tryReload(settingsPath string, reload func(*FICSettings)) {
if config, err := ReadSettings(settingsPath); err != nil {
log.Println("ERROR: Unable to read challenge settings:", err)
} else if time.Until(config.ActivateTime) > 0 {