From ef302d22a981b2fab8b47777b8ebf068507e5091 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Thu, 7 Mar 2024 15:56:20 -0500 Subject: [PATCH] Avoid panic if user.Current() fails; add logging to "ntfy subscribe" to help figure out what's wrong --- client/config.go | 2 ++ cmd/subscribe.go | 37 ++++++++++++++++++++++++++----------- cmd/subscribe_darwin.go | 2 +- cmd/subscribe_unix.go | 2 +- cmd/subscribe_windows.go | 2 +- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/client/config.go b/client/config.go index bc46ab89..870c835b 100644 --- a/client/config.go +++ b/client/config.go @@ -2,6 +2,7 @@ package client import ( "gopkg.in/yaml.v2" + "heckel.io/ntfy/v2/log" "os" ) @@ -44,6 +45,7 @@ func NewConfig() *Config { // LoadConfig loads the Client config from a yaml file func LoadConfig(filename string) (*Config, error) { + log.Debug("Loading client config from %s", filename) b, err := os.ReadFile(filename) if err != nil { return nil, err diff --git a/cmd/subscribe.go b/cmd/subscribe.go index 1a0a7a6f..5ebf9627 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -310,28 +310,43 @@ func loadConfig(c *cli.Context) (*client.Config, error) { if filename != "" { return client.LoadConfig(filename) } - configFile := defaultClientConfigFile() - if s, _ := os.Stat(configFile); s != nil { - return client.LoadConfig(configFile) + configFile, err := defaultClientConfigFile() + if err != nil { + log.Warn("Could not determine default client config file: %s", err.Error()) + } else { + if s, _ := os.Stat(configFile); s != nil { + return client.LoadConfig(configFile) + } + log.Debug("Config file %s not found", configFile) } + log.Debug("Loading default config") return client.NewConfig(), nil } //lint:ignore U1000 Conditionally used in different builds -func defaultClientConfigFileUnix() string { - u, _ := user.Current() +func defaultClientConfigFileUnix() (string, error) { + u, err := user.Current() + if err != nil { + return "", fmt.Errorf("could not determine current user: %w", err) + } configFile := clientRootConfigFileUnixAbsolute if u.Uid != "0" { - homeDir, _ := os.UserConfigDir() - return filepath.Join(homeDir, clientUserConfigFileUnixRelative) + homeDir, err := os.UserConfigDir() + if err != nil { + return "", fmt.Errorf("could not determine user config dir: %w", err) + } + return filepath.Join(homeDir, clientUserConfigFileUnixRelative), nil } - return configFile + return configFile, nil } //lint:ignore U1000 Conditionally used in different builds -func defaultClientConfigFileWindows() string { - homeDir, _ := os.UserConfigDir() - return filepath.Join(homeDir, clientUserConfigFileWindowsRelative) +func defaultClientConfigFileWindows() (string, error) { + homeDir, err := os.UserConfigDir() + if err != nil { + return "", fmt.Errorf("could not determine user config dir: %w", err) + } + return filepath.Join(homeDir, clientUserConfigFileWindowsRelative), nil } func logMessagePrefix(m *client.Message) string { diff --git a/cmd/subscribe_darwin.go b/cmd/subscribe_darwin.go index 0372a79f..487f0641 100644 --- a/cmd/subscribe_darwin.go +++ b/cmd/subscribe_darwin.go @@ -11,6 +11,6 @@ var ( scriptLauncher = []string{"sh", "-c"} ) -func defaultClientConfigFile() string { +func defaultClientConfigFile() (string, error) { return defaultClientConfigFileUnix() } diff --git a/cmd/subscribe_unix.go b/cmd/subscribe_unix.go index 8b91fed9..3f5f526f 100644 --- a/cmd/subscribe_unix.go +++ b/cmd/subscribe_unix.go @@ -13,6 +13,6 @@ var ( scriptLauncher = []string{"sh", "-c"} ) -func defaultClientConfigFile() string { +func defaultClientConfigFile() (string, error) { return defaultClientConfigFileUnix() } diff --git a/cmd/subscribe_windows.go b/cmd/subscribe_windows.go index e8f1a271..22c07d81 100644 --- a/cmd/subscribe_windows.go +++ b/cmd/subscribe_windows.go @@ -10,6 +10,6 @@ var ( scriptLauncher = []string{"cmd.exe", "/Q", "/C"} ) -func defaultClientConfigFile() string { +func defaultClientConfigFile() (string, error) { return defaultClientConfigFileWindows() }