forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
where.go
96 lines (79 loc) 路 2.07 KB
/
where.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package where
import (
"github.com/belphemur/mangal/constant"
"github.com/belphemur/mangal/filesystem"
"github.com/belphemur/mangal/key"
"github.com/samber/lo"
"github.com/spf13/viper"
"os"
"path/filepath"
)
const EnvConfigPath = "MANGAL_CONFIG_PATH"
// mkdir creates a directory and all parent directories if they don't exist
// will return the path of the directory
func mkdir(path string) string {
lo.Must0(filesystem.Api().MkdirAll(path, os.ModePerm))
return path
}
// Config path
// Will create the directory if it doesn't exist
func Config() string {
var path string
if customDir, present := os.LookupEnv(EnvConfigPath); present {
path = customDir
} else {
path = filepath.Join(lo.Must(os.UserConfigDir()), constant.Mangal)
}
return mkdir(path)
}
// Sources path
// Will create the directory if it doesn't exist
func Sources() string {
return mkdir(filepath.Join(Config(), "sources"))
}
func AnilistBinds() string {
return filepath.Join(Config(), "anilist.json")
}
// Logs path
// Will create the directory if it doesn't exist
func Logs() string {
return mkdir(filepath.Join(Config(), "logs"))
}
// Queries path
// Will create the directory if it doesn't exist
func Queries() string {
return filepath.Join(Cache(), "queries.json")
}
// History path to the file
// Will create the directory if it doesn't exist
func History() string {
return filepath.Join(Config(), "history.json")
}
// Downloads path
// Will create the directory if it doesn't exist
func Downloads() string {
path, err := filepath.Abs(viper.GetString(key.DownloaderPath))
if err != nil {
path, err = os.Getwd()
if err != nil {
path = "."
}
}
return mkdir(path)
}
// Cache path
// Will create the directory if it doesn't exist
func Cache() string {
cacheDir, err := os.UserCacheDir()
if err != nil {
cacheDir = filepath.Join(".", "cache")
}
cacheDir = filepath.Join(cacheDir, constant.Mangal)
return mkdir(cacheDir)
}
// Temp path
// Will create the directory if it doesn't exist
func Temp() string {
tempDir := filepath.Join(os.TempDir(), constant.Mangal)
return mkdir(tempDir)
}