Skip to content

Commit 18da265

Browse files
committed
Clean up a lot. Change some of the tests. And restructureed the struct and methods. Added doc with comments.
1 parent 65ed829 commit 18da265

File tree

5 files changed

+78
-323
lines changed

5 files changed

+78
-323
lines changed

location/location.go

Lines changed: 39 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,40 @@ import (
1010
"github.com/adrg/xdg"
1111
)
1212

13+
// ILocation 🧩 Is the interface for the NewLocationService.
1314
type ILocation interface {
1415
SystemPaths() *SystemPaths
15-
ThisAppPaths() *ApplicationPaths
16+
ApplicationPaths() *ApplicationPaths
1617
}
18+
19+
// SLocation 🏛️ Is the main structure for the NewLocationService.
1720
type SLocation struct {
1821
appName AppName
1922
appVersion AppVersion
2023
pluginTypeNames PluginTypeNames
2124
paths *Paths
2225
}
2326

27+
// For mocking 🥸.
2428
var (
2529
xdgCacheFile = xdg.CacheFile
2630
xdgConfigFile = xdg.ConfigFile
2731
xdgRuntimeFile = xdg.RuntimeFile
2832
xdgDataFile = xdg.DataFile
2933
)
3034

31-
func (service *SLocation) setAll() error {
32-
service.paths.ThisApplicationPaths.Name = service.appName
33-
service.paths.ThisApplicationPaths.PluginTypeNames = service.pluginTypeNames
35+
// SystemPaths returns struct of all systems paths
36+
func (service *SLocation) SystemPaths() *SystemPaths {
37+
return service.paths.SystemPaths
38+
}
39+
40+
// ApplicationPaths returns the struct of all the main application paths
41+
func (service *SLocation) ApplicationPaths() *ApplicationPaths {
42+
return service.paths.ApplicationPaths
43+
}
3444

45+
// setAllPaths calls on all the private methods to set in the struct all the absolute paths.
46+
func (service *SLocation) setAllPaths() error {
3547
relFilePath := fmt.Sprintf("%s/%s", service.appName, service.appName)
3648

3749
err := service.setSystemPaths()
@@ -59,7 +71,7 @@ func (service *SLocation) setAll() error {
5971
return err
6072
}
6173

62-
service.setBinPaths()
74+
service.setBinPath()
6375

6476
err = service.setPluginPaths()
6577
if err != nil {
@@ -74,11 +86,8 @@ func (service *SLocation) setAll() error {
7486
return nil
7587
}
7688

77-
// setSystemPaths sets the basic system paths
89+
// setSystemPaths sets the system paths.
7890
func (service *SLocation) setSystemPaths() error {
79-
//
80-
// System
81-
//
8291
sysPaths := &SystemPaths{
8392
Home: xdg.Home,
8493
DataHome: xdg.DataHome,
@@ -99,9 +108,6 @@ func (service *SLocation) setSystemPaths() error {
99108
runtime.GOOS != "plan9" {
100109
sysPaths.UserLocalHome = filepath.Join(xdg.DataHome, ".local")
101110

102-
// TODO:
103-
//sysPaths.UserApplicationsHome = filepath.Join(xdg.DataHome, "applications")
104-
105111
var existApplicationsPath string
106112
for _, applicationsPath := range xdg.ApplicationDirs {
107113
if file.Exists(applicationsPath) {
@@ -111,7 +117,7 @@ func (service *SLocation) setSystemPaths() error {
111117
}
112118
if existApplicationsPath != "" {
113119
sysPaths.UserApplicationsHome = existApplicationsPath
114-
service.paths.ThisApplicationPaths.Paths.ApplicationsDesktopFile = fmt.Sprintf(
120+
service.paths.ApplicationPaths.ApplicationsDesktopFile = fmt.Sprintf(
115121
"%s/%s.desktop",
116122
existApplicationsPath,
117123
string(service.appName),
@@ -126,248 +132,85 @@ func (service *SLocation) setSystemPaths() error {
126132
return nil
127133
}
128134

129-
// setDataPaths
135+
// setDataPaths sets the application specific data directory path.
130136
func (service *SLocation) setDataPaths() error {
131137
dataDir, err := xdgDataFile(string(service.appName))
132138
if err != nil {
133139
return errors.Join(fmt.Errorf(`xdg.DataFile was unable to set "%s" path`, dataDir), err)
134140
}
135-
service.paths.ThisApplicationPaths.Paths.DataHome = dataDir
141+
service.paths.ApplicationPaths.DataHome = dataDir
136142

137143
return nil
138144
}
139145

140-
// setConfigPaths
146+
// setConfigPaths sets the application config directory and file YAML absolute paths.
141147
func (service *SLocation) setConfigPaths(relFilePath string) error {
142148
configFile, err := xdgConfigFile(relFilePath + ".yaml")
143149
if err != nil {
144150
return errors.Join(fmt.Errorf(`xdg.ConfigFile was unable to set "%s" path`, configFile), err)
145151
}
146-
service.paths.ThisApplicationPaths.Paths.ConfigFile = configFile
147-
service.paths.ThisApplicationPaths.Paths.ConfigHome = filepath.Dir(configFile)
152+
service.paths.ApplicationPaths.ConfigFile = configFile
153+
service.paths.ApplicationPaths.ConfigHome = filepath.Dir(configFile)
148154

149155
return nil
150156
}
151157

152-
// setStatePaths
158+
// setStatePaths sets the absolute path to the state directory.
153159
func (service *SLocation) setStatePaths() error {
154160
stateDir, err := xdg.StateFile(string(service.appName))
155161
if err != nil {
156162
return errors.Join(fmt.Errorf(`xdg.StateFile was unable to set "%s" path`, stateDir), err)
157163
}
158-
service.paths.ThisApplicationPaths.Paths.StateHome = stateDir
164+
service.paths.ApplicationPaths.StateHome = stateDir
159165

160166
return nil
161167
}
162168

163-
// setCachePaths
169+
// setCachePaths sets the to the cache directory and file absolute paths.
164170
func (service *SLocation) setCachePaths(relFilePath string) error {
165171
cacheFilePath, err := xdgCacheFile(relFilePath + ".cache")
166172
if err != nil {
167173
return errors.Join(fmt.Errorf(`xdg.CacheFile was unable to set "%s" path`, cacheFilePath), err)
168174
}
169-
service.paths.ThisApplicationPaths.Paths.CacheHome = filepath.Dir(cacheFilePath)
170-
service.paths.ThisApplicationPaths.Paths.CacheFile = cacheFilePath
175+
service.paths.ApplicationPaths.CacheHome = filepath.Dir(cacheFilePath)
176+
service.paths.ApplicationPaths.CacheFile = cacheFilePath
171177

172178
return nil
173179
}
174180

175-
// setBinPaths
176-
func (service *SLocation) setBinPaths() {
177-
service.paths.ThisApplicationPaths.Paths.BinFile = fmt.Sprintf("%s/%s", xdg.BinHome, service.appName)
181+
// setBinPath sets the absolute bin path.
182+
// The bin is the application main binary.
183+
func (service *SLocation) setBinPath() {
184+
service.paths.ApplicationPaths.BinFile = fmt.Sprintf("%s/%s", xdg.BinHome, service.appName)
178185
}
179186

180-
// setPluginPaths
187+
// setPluginPaths sets the path or paths to the plugin directory.
181188
func (service *SLocation) setPluginPaths() error {
182189
pluginDirs := make(map[string]string)
183190
for _, pluginTypeName := range service.pluginTypeNames {
184191
pluginDirs[pluginTypeName] = fmt.Sprintf("%s/%s.d", xdg.DataHome, pluginTypeName)
185192
}
186-
service.paths.ThisApplicationPaths.Paths.PluginsHome = pluginDirs
193+
service.paths.ApplicationPaths.PluginsHome = pluginDirs
187194

188195
return nil
189196
}
190197

191-
// setSecretPaths
198+
// setSecretPaths sets the secret absolute paths.
192199
func (service *SLocation) setSecretPaths() error {
193200
secretsRelPath := fmt.Sprintf("%s/secrets", service.appName)
194201

195202
tmpSecrets, err := xdgRuntimeFile(secretsRelPath)
196203
if err != nil {
197204
return errors.Join(fmt.Errorf(`xdg.RuntimeFile was unable to set "%s" path`, tmpSecrets), err)
198205
}
199-
service.paths.ThisApplicationPaths.Paths.RuntimeDir = filepath.Dir(tmpSecrets)
200-
service.paths.ThisApplicationPaths.Paths.SecretsHome = tmpSecrets
206+
service.paths.ApplicationPaths.RuntimeDir = filepath.Dir(tmpSecrets)
207+
service.paths.ApplicationPaths.SecretsHome = tmpSecrets
201208

202209
tmpSecretsMTls, err := xdgRuntimeFile(secretsRelPath + "/mTLS")
203210
if err != nil {
204211
return errors.Join(fmt.Errorf(`xdg.RuntimeFile was unable to set "%s" path`, tmpSecrets), err)
205212
}
206-
service.paths.ThisApplicationPaths.Paths.MTLSHome = tmpSecretsMTls
207-
208-
return nil
209-
}
210-
211-
// SystemPaths returns struct of all systems paths
212-
func (service *SLocation) SystemPaths() *SystemPaths {
213-
return service.paths.SystemPaths
214-
}
215-
216-
// ThisAppPaths returns the struct of all the main application paths
217-
func (service *SLocation) ThisAppPaths() *ApplicationPaths {
218-
return service.paths.ThisApplicationPaths.Paths
219-
}
220-
221-
// Paths return the absolute paths for the different parts of storage
222-
/*func (d *AbsPaths) Paths(collectionName string) (*AbsPaths, error) {
223-
mainPath, err := d.Main()
224-
if err != nil {
225-
return &AbsPaths{}, err
226-
}
227-
228-
return d.paths(mainPath, collectionName)
229-
}
230-
231-
// Main returns the main path for `.hery` storage path
232-
// TODO: Maybe name it Root
233-
func (d *AbsPaths) Main() (string, error) {
234-
//
235-
// Using env var
236-
//
237-
238-
envStoragePathValue := os.Getenv(HeryStoragePath)
239-
240-
if envStoragePathValue != "" {
241-
envStoragePath, err := filepathAbs(envStoragePathValue)
242-
if err != nil {
243-
return "", err
244-
}
245-
return envStoragePath, nil
246-
}
247-
248-
//
249-
// Using current location
250-
//
251-
252-
cwd, err := osGetwd()
253-
if err != nil {
254-
return "", err
255-
}
256-
257-
localStoragePath := filepathJoin(cwd, ".hery")
258-
259-
if fileExists(localStoragePath) {
260-
return localStoragePath, nil
261-
}
262-
263-
//
264-
// Default
265-
//
266-
267-
var mainDir string
268-
switch runtime.GOOS {
269-
case "windows":
270-
appDataDir := os.Getenv("APPDATA")
271-
mainDir = filepathJoin(appDataDir, "Hery")
272-
default: // "linux" and "darwin" (macOS)
273-
homeDir, err := os.UserHomeDir()
274-
if err != nil {
275-
return "", fmt.Errorf("error getting home directory: %s", err)
276-
}
277-
mainDir = filepathJoin(homeDir, ".hery")
278-
}
279-
280-
return mainDir, nil
281-
}
282-
283-
// EntityPath returns the absolute path to a specific entity
284-
func (d *AbsPaths) EntityPath(entitiesPath, entityRelativePath string) string {
285-
return filepathJoin(entitiesPath, entityRelativePath)
286-
}
287-
288-
// TmpPaths returns the tmp absolute paths for the different parts of storage
289-
func (d *AbsPaths) TmpPaths(collectionName string) (*AbsPaths, error) {
290-
mainTmpPath, err := d.TmpMain()
291-
if err != nil {
292-
return &AbsPaths{}, err
293-
}
294-
295-
return d.paths(mainTmpPath, collectionName)
296-
}
297-
298-
// TmpMain returns the tmp main path for `.hery` storage path
299-
func (d *AbsPaths) TmpMain() (string, error) {
300-
tempDir, err := osMkdirTemp("", "hery_*")
301-
if err != nil {
302-
return "", err
303-
}
304-
305-
storageTmpPath := filepath.Join(tempDir, ".hery")
306-
err = osMkdirAll(storageTmpPath, perm)
307-
if err != nil {
308-
return "", err
309-
}
310-
311-
return storageTmpPath, nil
312-
}
313-
314-
// MakePaths makes all the storage subdirectories
315-
func (d *AbsPaths) MakePaths(paths AbsPaths) error {
316-
err := osMkdirAll(paths.Storage, perm)
317-
if err != nil {
318-
return err
319-
}
320-
321-
err = osMkdirAll(paths.Catalog, perm)
322-
if err != nil {
323-
return err
324-
}
325-
326-
err = osMkdirAll(paths.Collection, perm)
327-
if err != nil {
328-
return err
329-
}
330-
331-
err = osMkdirAll(paths.Entities, perm)
332-
if err != nil {
333-
return err
334-
}
213+
service.paths.ApplicationPaths.MTLSHome = tmpSecretsMTls
335214

336215
return nil
337216
}
338-
339-
// paths internal function to generate the paths for different part of storage based on main path and collection name
340-
func (d *AbsPaths) paths(mainPath, collectionName string) (*AbsPaths, error) {
341-
catalogPath := d.catalogPath(mainPath)
342-
collectionPath := d.collectionPath(catalogPath, collectionName)
343-
entityPath := d.entitiesPath(collectionPath)
344-
cachePath := d.cachePath(collectionName, collectionPath)
345-
346-
return &AbsPaths{
347-
Storage: mainPath,
348-
Catalog: catalogPath,
349-
Collection: collectionPath,
350-
Entities: entityPath,
351-
Cache: cachePath,
352-
}, nil
353-
}
354-
355-
// catalogPath returns the catalog absolute path
356-
func (d *AbsPaths) catalogPath(mainPath string) string {
357-
return filepathJoin(mainPath, "collection")
358-
}
359-
360-
// collectionPath returns the collection absolute path
361-
func (d *AbsPaths) collectionPath(mainPath, collectionName string) string {
362-
return filepathJoin(mainPath, collectionName)
363-
}
364-
365-
// entityPath returns the entity absolute path
366-
func (d *AbsPaths) entitiesPath(collectionPath string) string {
367-
return filepathJoin(collectionPath, "entity")
368-
}
369-
370-
// cachePath returns the collection cache absolute path
371-
func (d *AbsPaths) cachePath(collectionName, collectionPath string) string {
372-
return filepathJoin(collectionPath, fmt.Sprintf("%s.cache", collectionName))
373-
}*/

location/location_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package location
22

3-
/*func TestPaths(t *testing.T) {
4-
locationService := NewLocationService()
5-
locationService.Paths()
3+
/*func Test_setSystemPaths(t *testing.T) {
4+
tests := []struct {
5+
name string
6+
path string
7+
}
68
}*/

0 commit comments

Comments
 (0)