/
dirs.go
61 lines (48 loc) · 1.37 KB
/
dirs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package settings
import (
"os"
"path/filepath"
"github.com/leonelquinteros/gotext"
"github.com/pkg/errors"
)
// 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 != "" {
if err := initDir(configHome); err == nil {
return filepath.Join(configHome, "yay", configFileName)
}
}
if configHome := os.Getenv("HOME"); configHome != "" {
if err := initDir(configHome); err == nil {
return filepath.Join(configHome, ".config", "yay", configFileName)
}
}
return ""
}
func getCacheHome() string {
if cacheHome := os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
if err := initDir(cacheHome); err == nil {
return filepath.Join(cacheHome, "yay")
}
}
if cacheHome := os.Getenv("HOME"); cacheHome != "" {
if err := initDir(cacheHome); err == nil {
return filepath.Join(cacheHome, ".cache", "yay")
}
}
return "/tmp"
}
func initDir(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0o755); err != nil {
return errors.New(gotext.Get("failed to create config directory '%s': %s", dir, err))
}
} else if err != nil {
return err
}
return nil
}