checkpoint: remove default config dir; replace with err if user's congig home not found

This commit is contained in:
Dan Anglin 2024-08-18 02:22:39 +01:00
parent 15b9761497
commit 9acf562ccc
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) {
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 {

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
// 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
}

View file

@ -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) {