checkpoint: remove default config dir; replace with err if user's congig home not found
All checks were successful
Tests / test (pull_request) Has been skipped

This commit is contained in:
Dan Anglin 2024-08-18 02:22:39 +01:00
parent 3c8633ff04
commit 986407f728
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 43 additions and 14 deletions

View file

@ -35,7 +35,10 @@ type Integrations struct {
} }
func NewConfigFromFile(configDir string) (*Config, error) { 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) file, err := os.Open(path)
if err != nil { if err != nil {
@ -53,13 +56,19 @@ func NewConfigFromFile(configDir string) (*Config, error) {
} }
func FileExists(configDir string) (bool, 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) return utilities.FileExists(path)
} }
func SaveDefaultConfigToFile(configDir string) error { 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) file, err := os.Create(path)
if err != nil { if err != nil {
@ -69,7 +78,7 @@ func SaveDefaultConfigToFile(configDir string) error {
config := defaultConfig() config := defaultConfig()
credentialsFilePath, err := utilities.AbsolutePath(defaultCredentialsConfigFile(configDir)) credentialsFilePath, err := defaultCredentialsConfigFile(configDir)
if err != nil { if err != nil {
return fmt.Errorf("unable to calculate the path to the credentials file: %w", err) 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 return nil
} }
func configFile(configDir string) string { func configPath(configDir string) (string, error) {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName) 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 { func defaultConfig() Config {

View file

@ -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 // directory. If the directory is not specified then the default directory is used. If the directory
// is not present, it will be created. // is not present, it will be created.
func SaveCredentials(filePath, username string, credentials Credentials) (string, error) { 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) return "", fmt.Errorf("unable to ensure the configuration directory: %w", err)
} }
@ -128,6 +133,16 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, filePath string) er
return nil return nil
} }
func defaultCredentialsConfigFile(configDir string) string { func defaultCredentialsConfigFile(configDir string) (string, error) {
return filepath.Join(utilities.CalculateConfigDir(configDir), defaultCredentialsFileName) 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
} }

View file

@ -14,17 +14,17 @@ const (
cacheStatusesDir = "statuses" cacheStatusesDir = "statuses"
) )
func CalculateConfigDir(configDir string) string { func CalculateConfigDir(configDir string) (string, error) {
if configDir != "" { if configDir != "" {
return configDir return configDir, nil
} }
configRoot, err := os.UserConfigDir() configRoot, err := os.UserConfigDir()
if err != nil { 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) { func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {