Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ linters-settings:
- $gostd
- fyne.io/fyne
- github.com/tfpf/riven
gosec:
excludes:
- G306
mnd:
ignored-numbers:
- '0644'
67 changes: 43 additions & 24 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading