-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder.go
134 lines (124 loc) · 2.93 KB
/
folder.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package util
import (
"os"
"path/filepath"
)
var DefaultFolderMode = os.FileMode(0770)
var registeredFolders = [][]string{}
func RegisterDataFolder(folder ...string) string {
registeredFolders = append(registeredFolders, folder)
return AppData(folder...)
}
func MakeLoggerFolderIfNotExist() {
_, err := os.Stat(LogsPath)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(LogsPath, DefaultFolderMode)
if err != nil {
panic(err)
}
}
}
}
func MustLoadRegisteredFolders() {
for _, v := range registeredFolders {
folder := AppData(v...)
_, err := os.Stat(folder)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(folder, DefaultFolderMode)
if err != nil {
panic(err)
}
}
}
}
}
func mustPath(path string, err error) string {
if err != nil {
panic(err)
}
return path
}
func IsPathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
var AutoDetectCWD = true
var RootPath string
var ResourcesPath string
var AppDataPath string
var LogsPath string
var ConfigPath string
var SystemPath string
var ConstantsPath string
var DefaultConfigPath string
var UpdatePaths = func() error {
if RootPath == "" {
var execute string
execute = filepath.Dir(mustPath(os.Executable()))
if AutoDetectCWD && ConfigPath == "" && !IsPathExists(filepath.Join(execute, "../", "config")) {
execute = MustGetWD()
}
RootPath = filepath.Join(execute, "../")
}
if ResourcesPath == "" {
ResourcesPath = filepath.Join(RootPath, "resources")
}
if AppDataPath == "" {
AppDataPath = filepath.Join(RootPath, "appdata")
}
if LogsPath == "" {
LogsPath = filepath.Join(RootPath, "logs")
}
if ConfigPath == "" {
ConfigPath = filepath.Join(RootPath, "config")
}
if SystemPath == "" {
SystemPath = filepath.Join(RootPath, "system")
}
if ConstantsPath == "" {
ConstantsPath = filepath.Join(SystemPath, "constants")
}
if DefaultConfigPath == "" {
DefaultConfigPath = filepath.Join(SystemPath, "defaultconfig")
}
return nil
}
var MustChRoot = func() {
Must(os.Chdir(RootPath))
}
func MustGetWD() string {
path, err := os.Getwd()
if err != nil {
panic(err)
}
return path
}
func joinPath(p string, filepaths ...string) string {
return filepath.Join(p, filepath.Join(filepaths...))
}
func Resources(filepaths ...string) string {
return joinPath(ResourcesPath, filepaths...)
}
func Config(filepaths ...string) string {
return joinPath(ConfigPath, filepaths...)
}
func AppData(filepaths ...string) string {
return joinPath(AppDataPath, filepaths...)
}
func Logs(filepaths ...string) string {
return joinPath(LogsPath, filepaths...)
}
func System(filepaths ...string) string {
return joinPath(SystemPath, filepaths...)
}
func Constants(filepaths ...string) string {
return joinPath(ConstantsPath, filepaths...)
}
func DefaultConfig(filepaths ...string) string {
return joinPath(DefaultConfigPath, filepaths...)
}
func Root(filepaths ...string) string {
return joinPath(RootPath, filepaths...)
}