diff --git a/.golangci.yml b/.golangci.yml index 074313d..ba8615c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,3 +18,9 @@ linters-settings: - $gostd - fyne.io/fyne - github.com/tfpf/riven + gosec: + excludes: + - G306 + mnd: + ignored-numbers: + - '0644' diff --git a/config/config.go b/config/config.go index 32c7abc..836e293 100644 --- a/config/config.go +++ b/config/config.go @@ -7,42 +7,61 @@ import ( "path/filepath" ) +// Config stores Riven configuration parameters. type Config struct { - FyneFont string `json:"fyneFont,omitempty"` + configFile string + FyneFont string `json:"fyneFont,omitempty"` } -// Read looks for the Riven configuration JSON file in the OS-specific user -// configuration directory and creates a configuration object from it. -func Read() *Config { - cfg := &Config{} +// locate sets the location where the Riven configuration JSON file should be +// on this OS if it is not already set. +func (cfg *Config) locate() error { + if cfg.configFile != "" { + return nil + } configDir, err := os.UserConfigDir() if err != nil { - slog.Warn("Could not find user configuration directory", slog.Any("err", err)) - return cfg + slog.Error("Could not find user configuration directory", slog.Any("err", err)) + return err + } + cfg.configFile = filepath.Join(configDir, "riven", "config.json") + return nil +} + +// Read reads Riven configuration parameters from the Riven configuration JSON +// file in the OS-specific user configuration directory. +func (cfg *Config) Read() error { + if err := cfg.locate(); err != nil { + return err } - configFile := filepath.Join(configDir, "riven", "config.json") - configFileContents, err := os.ReadFile(configFile) + configFileContents, err := os.ReadFile(cfg.configFile) if err != nil { - slog.Warn("Could not read Riven configuration file", slog.Any("err", err), slog.String("file", configFile)) - return cfg + slog.Error("Could not read Riven configuration file", slog.Any("err", err), slog.String("file", cfg.configFile)) + return err } if err := json.Unmarshal(configFileContents, cfg); err != nil { - slog.Warn("Could not parse Riven configuration file", slog.Any("err", err), slog.String("file", configFile)) - return cfg + slog.Error("Could not decode Riven configuration", slog.Any("err", err), slog.String("file", cfg.configFile)) + return err } - slog.Info("Read Riven configuration file", slog.String("file", configFile), slog.Any("cfg", cfg)) - return cfg + slog.Info("Read Riven configuration file", slog.String("file", cfg.configFile), slog.Any("cfg", cfg)) + return nil } -// Load loads the Riven configuration. -func (cfg *Config) Load() { - var err error - if cfg.FyneFont != "" { - if err = os.Setenv("FYNE_FONT", cfg.FyneFont); err != nil { - slog.Warn("Could not set Fyne font", slog.Any("err", err)) - } +// Write writes Riven configuration parameters to the Riven configuration JSON +// file in the OS-specific user configuration directory. +func (cfg *Config) Write() error { + if err := cfg.locate(); err != nil { + return err + } + configFileContents, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + slog.Error("Could not encode Riven configuration", slog.Any("err", err), slog.Any("cfg", cfg)) + return err } - if err == nil { - slog.Info("Loaded Riven configuration") + if err := os.WriteFile(cfg.configFile, configFileContents, 0644); err != nil { + slog.Error("Could not write Riven configuration file", slog.Any("err", err), slog.String("file", cfg.configFile)) + return err } + slog.Info("Wrote Riven configuration file", slog.String("file", cfg.configFile), slog.Any("cfg", cfg)) + return nil } diff --git a/main.go b/main.go index e83aac5..0da86c3 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,8 @@ import ( func main() { slog.SetDefault(logging.NewJSONLogger()) - cfg := config.Read() - cfg.Load() + cfg := &config.Config{} + _ = cfg.Read() a := app.New() w := a.NewWindow("Hello")