1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-14 11:47:33 +00:00

Avoid panic if user.Current() fails; add logging to "ntfy subscribe" to help figure out what's wrong

This commit is contained in:
binwiederhier 2024-03-07 15:56:20 -05:00
parent 7ab8ef73a3
commit ef302d22a9
5 changed files with 31 additions and 14 deletions

View file

@ -2,6 +2,7 @@ package client
import ( import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"heckel.io/ntfy/v2/log"
"os" "os"
) )
@ -44,6 +45,7 @@ func NewConfig() *Config {
// LoadConfig loads the Client config from a yaml file // LoadConfig loads the Client config from a yaml file
func LoadConfig(filename string) (*Config, error) { func LoadConfig(filename string) (*Config, error) {
log.Debug("Loading client config from %s", filename)
b, err := os.ReadFile(filename) b, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -310,28 +310,43 @@ func loadConfig(c *cli.Context) (*client.Config, error) {
if filename != "" { if filename != "" {
return client.LoadConfig(filename) return client.LoadConfig(filename)
} }
configFile := defaultClientConfigFile() 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 { if s, _ := os.Stat(configFile); s != nil {
return client.LoadConfig(configFile) return client.LoadConfig(configFile)
} }
log.Debug("Config file %s not found", configFile)
}
log.Debug("Loading default config")
return client.NewConfig(), nil return client.NewConfig(), nil
} }
//lint:ignore U1000 Conditionally used in different builds //lint:ignore U1000 Conditionally used in different builds
func defaultClientConfigFileUnix() string { func defaultClientConfigFileUnix() (string, error) {
u, _ := user.Current() u, err := user.Current()
if err != nil {
return "", fmt.Errorf("could not determine current user: %w", err)
}
configFile := clientRootConfigFileUnixAbsolute configFile := clientRootConfigFileUnixAbsolute
if u.Uid != "0" { if u.Uid != "0" {
homeDir, _ := os.UserConfigDir() homeDir, err := os.UserConfigDir()
return filepath.Join(homeDir, clientUserConfigFileUnixRelative) if err != nil {
return "", fmt.Errorf("could not determine user config dir: %w", err)
} }
return configFile return filepath.Join(homeDir, clientUserConfigFileUnixRelative), nil
}
return configFile, nil
} }
//lint:ignore U1000 Conditionally used in different builds //lint:ignore U1000 Conditionally used in different builds
func defaultClientConfigFileWindows() string { func defaultClientConfigFileWindows() (string, error) {
homeDir, _ := os.UserConfigDir() homeDir, err := os.UserConfigDir()
return filepath.Join(homeDir, clientUserConfigFileWindowsRelative) 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 { func logMessagePrefix(m *client.Message) string {

View file

@ -11,6 +11,6 @@ var (
scriptLauncher = []string{"sh", "-c"} scriptLauncher = []string{"sh", "-c"}
) )
func defaultClientConfigFile() string { func defaultClientConfigFile() (string, error) {
return defaultClientConfigFileUnix() return defaultClientConfigFileUnix()
} }

View file

@ -13,6 +13,6 @@ var (
scriptLauncher = []string{"sh", "-c"} scriptLauncher = []string{"sh", "-c"}
) )
func defaultClientConfigFile() string { func defaultClientConfigFile() (string, error) {
return defaultClientConfigFileUnix() return defaultClientConfigFileUnix()
} }

View file

@ -10,6 +10,6 @@ var (
scriptLauncher = []string{"cmd.exe", "/Q", "/C"} scriptLauncher = []string{"cmd.exe", "/Q", "/C"}
) )
func defaultClientConfigFile() string { func defaultClientConfigFile() (string, error) {
return defaultClientConfigFileWindows() return defaultClientConfigFileWindows()
} }