kos/util/sys/homedir.go

37 lines
614 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package sys
import (
"os"
"runtime"
)
// HomeDir return user home directory
func HomeDir() string {
2024-04-29 10:52:19 +08:00
if dirname, err := os.UserHomeDir(); err == nil {
return dirname
2023-04-23 17:57:36 +08:00
}
2024-04-29 10:52:19 +08:00
return os.TempDir()
2023-04-23 17:57:36 +08:00
}
// 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 {
2024-04-29 10:52:19 +08:00
if dirname, err := os.UserCacheDir(); err == nil {
return dirname
2023-04-23 17:57:36 +08:00
}
2024-04-29 10:52:19 +08:00
return os.TempDir()
}
func TempFile() (*os.File, error) {
return os.CreateTemp(os.TempDir(), "kos_*")
2023-04-23 17:57:36 +08:00
}