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
|
// data directory
package datadir
import (
"errors"
"io/fs"
"os"
"path/filepath"
)
// Get data directory path.
func DataDir() (string, error) {
var dir string
// check environment
if val, ok := os.LookupEnv("CVEZ_DATA_DIR"); ok {
return val, nil
}
// get config dir
configDir, err := os.UserConfigDir()
if err != nil {
return dir, err
}
// build path to data directory in config dir
dir = filepath.Join(configDir, "cvez")
if _, err = os.Stat(dir); err != nil {
if errors.Is(err, fs.ErrNotExist) {
// create data directory
return dir, os.MkdirAll(dir, 0700)
} else {
// return error
return "", err
}
} else {
return dir, nil
}
}
// Build path to file in cvez data directory.
func Join(args ...string) (string, error) {
if dir, err := DataDir(); err != nil {
return "", err
} else {
return filepath.Join(dir, filepath.Join(args...)), nil
}
}
// Get path to cache directory path within data directory and create it
// if it does not exist.
func CacheDir() (string, error) {
// build path, check for error
dir, err := Join("cache")
if err != nil {
return "", err
}
// create and return cache directory
return dir, os.MkdirAll(dir, 0700)
}
|