Skip to content

Commit

Permalink
feat: ensure read config can handle when the file doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
yiblet committed Feb 13, 2024
1 parent ca584ba commit 496f001
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ func ReadConfig() (config, error) {
// A common use case is to get a private config folder for your app to
// place its settings files into, that are specific to the local user.
configPath := configdir.LocalConfig("hlp")
err := configdir.MakePath(configPath) // Ensure it exists.

err := os.MkdirAll(configPath, 0755) // Ensure it exists.
if err != nil {
return config{}, fmt.Errorf("cannot read path: %w", err)
}

// Deal with a JSON configuration file in that folder.
configFile := filepath.Join(configPath, "configuration.json")
if _, err = os.Stat(configFile); err != nil {
if os.IsNotExist(err) {
return config{}, nil
}
return config{}, err
}

Expand Down

0 comments on commit 496f001

Please sign in to comment.