2020-08-21 02:39:52 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// configFileName holds the name of the config file.
|
|
|
|
const configFileName string = "config.json"
|
|
|
|
|
|
|
|
// vcsFileName holds the name of the vcs file.
|
|
|
|
const vcsFileName string = "vcs.json"
|
|
|
|
|
|
|
|
const completionFileName string = "completion.cache"
|
|
|
|
|
|
|
|
func getConfigPath() string {
|
|
|
|
if configHome := os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
|
2021-09-05 01:41:42 +02:00
|
|
|
configDir := filepath.Join(configHome, "yay")
|
|
|
|
if err := initDir(configDir); err == nil {
|
|
|
|
return filepath.Join(configDir, configFileName)
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configHome := os.Getenv("HOME"); configHome != "" {
|
2021-09-05 01:41:42 +02:00
|
|
|
configDir := filepath.Join(configHome, ".config", "yay")
|
|
|
|
if err := initDir(configDir); err == nil {
|
|
|
|
return filepath.Join(configDir, configFileName)
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCacheHome() string {
|
2021-08-31 02:27:25 +02:00
|
|
|
uid := os.Geteuid()
|
|
|
|
|
|
|
|
if cacheHome := os.Getenv("XDG_CACHE_HOME"); cacheHome != "" && uid != 0 {
|
2021-09-05 01:41:42 +02:00
|
|
|
cacheDir := filepath.Join(cacheHome, "yay")
|
|
|
|
if err := initDir(cacheDir); err == nil {
|
|
|
|
return cacheDir
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:27:25 +02:00
|
|
|
if cacheHome := os.Getenv("HOME"); cacheHome != "" && uid != 0 {
|
2021-09-05 01:41:42 +02:00
|
|
|
cacheDir := filepath.Join(cacheHome, ".cache", "yay")
|
|
|
|
if err := initDir(cacheDir); err == nil {
|
|
|
|
return cacheDir
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:41:42 +02:00
|
|
|
if uid == 0 && os.Getenv("SUDO_USER") == "" && os.Getenv("DOAS_USER") == "" {
|
|
|
|
return "/var/cache/yay" // Don't create directory if systemd-run takes care of it
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:27:25 +02:00
|
|
|
return os.TempDir()
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func initDir(dir string) error {
|
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, 0o755); err != nil {
|
2021-09-05 01:41:42 +02:00
|
|
|
return &ErrRuntimeDir{inner: err, dir: dir}
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|