-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
49 lines (39 loc) · 955 Bytes
/
helpers.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
package helpers
import (
"os"
"path/filepath"
)
var bbDirPath = ""
func SetDataDirPath(newPath string) error {
// NOTE: when path does not exist, use GetOrInitializeDir
// set the new path
bbDirPath = newPath
return nil
}
func GetDataDirPath() string {
// check if BUGBUDDY_DIR is set. if present, set it as the directory path
if envPath := os.Getenv("BUGBUDDY_DIR"); len(envPath) != 0 && bbDirPath != envPath {
SetDataDirPath(envPath)
}
if len(bbDirPath) != 0 {
return bbDirPath
}
homeDir, err := os.UserHomeDir()
if err != nil {
if homeEnv := os.Getenv("HOME"); len(homeEnv) != 0 {
homeDir = homeEnv
} else {
homeDir = os.TempDir()
}
}
return filepath.Join(homeDir, ".bugbuddy")
}
func GetOrInitializeDataDir() (string, error) {
dirPath := GetDataDirPath()
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
if err := os.MkdirAll(dirPath, 0755); err != nil {
return "", err
}
}
return dirPath, nil
}