kos/util/sys/homedir.go

37 lines
614 B
Go

package sys
import (
"os"
"runtime"
)
// HomeDir return user home directory
func HomeDir() string {
if dirname, err := os.UserHomeDir(); err == nil {
return dirname
}
return os.TempDir()
}
// HiddenFile get hidden file prefix
func HiddenFile(name string) string {
switch runtime.GOOS {
case "windows":
return "~" + name
default:
return "." + name
}
}
// CacheDir return user cache directory
func CacheDir() string {
if dirname, err := os.UserCacheDir(); err == nil {
return dirname
}
return os.TempDir()
}
func TempFile() (*os.File, error) {
return os.CreateTemp(os.TempDir(), "kos_*")
}