Skip to content

Commit

Permalink
Handle missing config gracefully but other issues with err
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Stigsson committed Jan 13, 2024
1 parent 7e1a563 commit 911d36b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ type Config struct {
FullName string `json:"FULL_NAME"`
}

func configPath() string {
if os.Getenv("XDG_CONFIG_HOME") != "" {
return os.Getenv("XDG_CONFIG_HOME") + "/config.json"
}
return os.Getenv("HOME") + "/.config/mpw/config.json"
}
func readConfig() (Config, error) {
b, err := os.ReadFile(os.Getenv("HOME") + "/.config/mpw/config.json")
b, err := os.ReadFile(configPath())
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
return Config{}, nil
}
return Config{}, err
}
var c Config
Expand All @@ -114,7 +123,7 @@ type Flags struct {
}

func parseFlags() Flags {
fullName := flag.String("full-name", "", "Specify the full name of the user, can be configured in json at " + os.Getenv("HOME") + "/.config/mpw/config.json")
fullName := flag.String("full-name", "", "Specify the full name of the user, can be configured in json FULL_NAME at " + configPath())
fullNameShorthand := flag.String("u", "", "Specify the full name of the user")
counter := flag.Int("counter", 1, "Specify the full name of the user")
counterShorthand := flag.Int("c", 1, "Specify the full name of the user")
Expand Down

0 comments on commit 911d36b

Please sign in to comment.