all: unify handling user home directory logic

This commit is contained in:
Cuong Manh Le
2025-05-05 23:28:49 +07:00
committed by Cuong Manh Le
parent 6286a71f2a
commit 47c04bf0f6
4 changed files with 10 additions and 43 deletions
+6 -10
View File
@@ -8,14 +8,9 @@ import (
"strings"
)
var homedir string
// absHomeDir returns the absolute path to given filename using home directory as root dir.
func absHomeDir(filename string) string {
if homedir != "" {
return filepath.Join(homedir, filename)
}
dir, err := userHomeDir()
// AbsHomeDir returns the absolute path to given filename using home directory as root dir.
func AbsHomeDir(filename string) string {
dir, err := UserHomeDir()
if err != nil {
return filename
}
@@ -31,7 +26,8 @@ func dirWritable(dir string) (bool, error) {
return true, f.Close()
}
func userHomeDir() (string, error) {
// UserHomeDir returns the home directory for user who is running ctrld.
func UserHomeDir() (string, error) {
// viper will expand for us.
if runtime.GOOS == "windows" {
// If we're on windows, use the install path for this.
@@ -58,7 +54,7 @@ func userHomeDir() (string, error) {
// The caller must ensure iface is non-nil.
func SavedStaticDnsSettingsFilePath(iface *net.Interface) string {
// The file is stored in the user home directory under a hidden file.
return absHomeDir(".dns_" + iface.Name)
return AbsHomeDir(".dns_" + iface.Name)
}
// SavedStaticNameserversAndPath returns the stored static nameservers for the given interface,