enbas/internal/utilities/absolute.go
Dan Anglin b03b3c31cc
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.
2024-06-29 16:02:19 +01:00

30 lines
598 B
Go

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