diff --git a/internal/config/config.go b/internal/config/config.go index 29cae4a..b74cad9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,7 +35,10 @@ type Integrations struct { } func NewConfigFromFile(configDir string) (*Config, error) { - path := configFile(configDir) + path, err := configPath(configDir) + if err != nil { + return nil, fmt.Errorf("unable to calculate the path to your config file: %w", err) + } file, err := os.Open(path) if err != nil { @@ -53,13 +56,19 @@ func NewConfigFromFile(configDir string) (*Config, error) { } func FileExists(configDir string) (bool, error) { - path := configFile(configDir) + path, err := configPath(configDir) + if err != nil { + return false, fmt.Errorf("unable to calculate the path to your config file: %w", err) + } return utilities.FileExists(path) } func SaveDefaultConfigToFile(configDir string) error { - path := configFile(configDir) + path, err := configPath(configDir) + if err != nil { + return fmt.Errorf("unable to calculate the path to your config file: %w", err) + } file, err := os.Create(path) if err != nil { @@ -69,7 +78,7 @@ func SaveDefaultConfigToFile(configDir string) error { config := defaultConfig() - credentialsFilePath, err := utilities.AbsolutePath(defaultCredentialsConfigFile(configDir)) + credentialsFilePath, err := defaultCredentialsConfigFile(configDir) if err != nil { return fmt.Errorf("unable to calculate the path to the credentials file: %w", err) } @@ -86,8 +95,13 @@ func SaveDefaultConfigToFile(configDir string) error { return nil } -func configFile(configDir string) string { - return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName) +func configPath(configDir string) (string, error) { + configDir, err := utilities.CalculateConfigDir(configDir) + if err != nil { + return "", fmt.Errorf("unable to get the config directory: %w", err) + } + + return filepath.Join(configDir, configFileName), nil } func defaultConfig() Config { diff --git a/internal/config/credentials.go b/internal/config/credentials.go index 57e8ee5..4d99e67 100644 --- a/internal/config/credentials.go +++ b/internal/config/credentials.go @@ -38,9 +38,14 @@ func (e CredentialsNotFoundError) Error() string { // directory. If the directory is not specified then the default directory is used. If the directory // is not present, it will be created. func SaveCredentials(filePath, username string, credentials Credentials) (string, error) { - directory := filepath.Dir(filePath) + part := filepath.Dir(filePath) - if err := utilities.EnsureDirectory(utilities.CalculateConfigDir(directory)); err != nil { + credentialsDir, err := utilities.CalculateConfigDir(part) + if err != nil { + fmt.Errorf("unable to calculate the directory to your credentials file: %w", err) + } + + if err := utilities.EnsureDirectory(credentialsDir); err != nil { return "", fmt.Errorf("unable to ensure the configuration directory: %w", err) } @@ -128,6 +133,16 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, filePath string) er return nil } -func defaultCredentialsConfigFile(configDir string) string { - return filepath.Join(utilities.CalculateConfigDir(configDir), defaultCredentialsFileName) +func defaultCredentialsConfigFile(configDir string) (string, error) { + dir, err := utilities.CalculateConfigDir(configDir) + if err != nil { + return "", fmt.Errorf("unable to calculate the config directory: %w", err) + } + + path, err := utilities.AbsolutePath(filepath.Join(dir, defaultCredentialsFileName)) + if err != nil { + return "", fmt.Errorf("unable to get the absolute path to the credentials config file: %w", err) + } + + return path, nil } diff --git a/internal/utilities/directories.go b/internal/utilities/directories.go index f3cd194..eddecfd 100644 --- a/internal/utilities/directories.go +++ b/internal/utilities/directories.go @@ -14,17 +14,17 @@ const ( cacheStatusesDir = "statuses" ) -func CalculateConfigDir(configDir string) string { +func CalculateConfigDir(configDir string) (string, error) { if configDir != "" { - return configDir + return configDir, nil } configRoot, err := os.UserConfigDir() if err != nil { - return filepath.Join(os.Getenv("HOME"), "."+internal.ApplicationName, "config") + return "", fmt.Errorf("unable to get your default config diretory: %w", err) } - return filepath.Join(configRoot, internal.ApplicationName) + return filepath.Join(configRoot, internal.ApplicationName), nil } func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {