-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
location.go
198 lines (167 loc) · 5.65 KB
/
location.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package util
import (
"fmt"
"github.com/mitchellh/go-homedir"
"os"
"path"
"strings"
"sync"
)
var locationInstance *Location
var locationOnce sync.Once
type Location struct {
woxDataDirectory string
userDataDirectory string
userDataDirectoryShortcutPath string // A file named .wox.location that contains the user data directory path
}
func GetLocation() *Location {
locationOnce.Do(func() {
locationInstance = &Location{}
})
return locationInstance
}
func (l *Location) Init() error {
dirname, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get user home dir: %w", err)
}
// check if wox data directory exists, if not, create it
l.woxDataDirectory = path.Join(dirname, ".wox")
if directoryErr := l.EnsureDirectoryExist(l.woxDataDirectory); directoryErr != nil {
return directoryErr
}
l.userDataDirectoryShortcutPath = path.Join(l.woxDataDirectory, ".userdata.location")
if _, statErr := os.Stat(l.userDataDirectoryShortcutPath); os.IsNotExist(statErr) {
// shortcut file does not exist, create and write default data directory path to it
file, createErr := os.Create(l.userDataDirectoryShortcutPath)
if createErr != nil {
return fmt.Errorf("failed to create shortcut file: %w", createErr)
}
defer file.Close()
// write data directory path to file
_, writeErr := file.WriteString(path.Join(l.woxDataDirectory, "wox-user"))
if writeErr != nil {
return fmt.Errorf("failed to write user data directory path to shortcut file: %w", writeErr)
}
}
// read data directory path from shortcut file
file, openErr := os.Open(l.userDataDirectoryShortcutPath)
if openErr != nil {
return fmt.Errorf("failed to open shortcut file: %w", openErr)
}
defer file.Close()
// read data directory path from file
readFile, readFileErr := os.ReadFile(l.userDataDirectoryShortcutPath)
if readFileErr != nil {
return fmt.Errorf("failed to read shortcut file: %w", readFileErr)
}
userDataDirectory, _ := homedir.Expand(string(readFile))
userDataDirectory = strings.ReplaceAll(userDataDirectory, "\n", "")
l.userDataDirectory = userDataDirectory
if directoryErr := l.EnsureDirectoryExist(l.userDataDirectory); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetLogDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetLogHostsDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetLogPluginDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetPluginDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetThemeDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetHostDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetPluginSettingDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetUIDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetCacheDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetImageCacheDirectory()); directoryErr != nil {
return directoryErr
}
if directoryErr := l.EnsureDirectoryExist(l.GetBackupDirectory()); directoryErr != nil {
return directoryErr
}
return nil
}
func (l *Location) EnsureDirectoryExist(directory string) error {
if _, statErr := os.Stat(directory); os.IsNotExist(statErr) {
mkdirErr := os.MkdirAll(directory, os.ModePerm)
if mkdirErr != nil {
return fmt.Errorf("failed to create directory [%s]: %w", directory, mkdirErr)
}
}
return nil
}
func (l *Location) GetLogDirectory() string {
return path.Join(l.woxDataDirectory, "log")
}
func (l *Location) GetWoxDataDirectory() string {
return l.woxDataDirectory
}
func (l *Location) GetLogPluginDirectory() string {
return path.Join(l.GetLogDirectory(), "plugins")
}
func (l *Location) GetLogHostsDirectory() string {
return path.Join(l.GetLogDirectory(), "hosts")
}
func (l *Location) GetPluginDirectory() string {
return path.Join(l.userDataDirectory, "plugins")
}
func (l *Location) GetThemeDirectory() string {
return path.Join(l.userDataDirectory, "themes")
}
func (l *Location) GetPluginSettingDirectory() string {
return path.Join(l.userDataDirectory, "settings")
}
func (l *Location) GetUserDataDirectory() string {
return l.userDataDirectory
}
func (l *Location) GetWoxSettingPath() string {
return path.Join(l.GetPluginSettingDirectory(), "wox.json")
}
func (l *Location) GetWoxAppDataPath() string {
return path.Join(l.GetPluginSettingDirectory(), "wox.data.json")
}
func (l *Location) GetHostDirectory() string {
return path.Join(l.woxDataDirectory, "hosts")
}
func (l *Location) GetUIDirectory() string {
return path.Join(l.woxDataDirectory, "ui")
}
func (l *Location) GetCacheDirectory() string {
return path.Join(l.woxDataDirectory, "cache")
}
func (l *Location) GetImageCacheDirectory() string {
return path.Join(l.GetCacheDirectory(), "images")
}
func (l *Location) GetBackupDirectory() string {
return path.Join(l.woxDataDirectory, "backup")
}
func (l *Location) GetUIAppPath() string {
if IsWindows() {
return path.Join(l.GetUIDirectory(), "flutter", "wox", "wox.exe")
}
if IsLinux() {
return path.Join(l.GetUIDirectory(), "flutter", "wox", "wox")
}
if IsMacOS() {
return path.Join(l.GetUIDirectory(), "flutter", "wox.app", "Contents", "MacOS", "wox")
}
return ""
}
func (l *Location) GetAppLockPath() string {
return path.Join(l.GetWoxDataDirectory(), "wox.lock")
}