fix: set absolute path to the credentials file
When creating the configuration file during the initialisation process calculate the absolute path to the credentials file instead of the relative path.
This commit is contained in:
parent
c8892a6535
commit
b03b3c31cc
2 changed files with 41 additions and 5 deletions
|
@ -71,7 +71,15 @@ func SaveDefaultConfigToFile(configDir string) error {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
config := defaultConfig(configDir)
|
||||
config := defaultConfig()
|
||||
|
||||
credentialsFilePath, err := utilities.AbsolutePath(defaultCredentialsConfigFile(configDir))
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to calculate the path to the credentials file: %w", err)
|
||||
}
|
||||
|
||||
config.CredentialsFile = credentialsFilePath
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
|
@ -86,11 +94,9 @@ func configFile(configDir string) string {
|
|||
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
|
||||
}
|
||||
|
||||
func defaultConfig(configDir string) Config {
|
||||
credentialsFilePath := defaultCredentialsConfigFile(configDir)
|
||||
|
||||
func defaultConfig() Config {
|
||||
return Config{
|
||||
CredentialsFile: credentialsFilePath,
|
||||
CredentialsFile: "",
|
||||
CacheDirectory: "",
|
||||
HTTP: HTTPConfig{
|
||||
Timeout: 5,
|
||||
|
|
30
internal/utilities/absolute.go
Normal file
30
internal/utilities/absolute.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func AbsolutePath(path string) (string, error) {
|
||||
if strings.HasPrefix(path, "~") {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get user's home directory; %w", err)
|
||||
}
|
||||
|
||||
path = filepath.Join(homeDir, path[1:])
|
||||
}
|
||||
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get the absolute path: %w", err)
|
||||
}
|
||||
|
||||
return absPath, nil
|
||||
}
|
Loading…
Reference in a new issue