Skip to content

Commit 2070841

Browse files
committed
Added more path gen code.
1 parent 99c6f7e commit 2070841

File tree

3 files changed

+98
-107
lines changed

3 files changed

+98
-107
lines changed

location/location.go

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package location
22

33
import (
4+
"errors"
45
"fmt"
5-
"github.com/adrg/xdg"
6+
"os"
67
"path/filepath"
78
"runtime"
9+
10+
"github.com/AmadlaOrg/LibraryUtils/file"
11+
"github.com/adrg/xdg"
812
)
913

1014
type ILocation interface {
@@ -19,9 +23,10 @@ type ILocation interface {
1923
MakePaths(paths AbsPaths) error*/
2024
}
2125
type SLocation struct {
22-
appName string
23-
appVersion string
24-
paths *Paths
26+
appName AppName
27+
appVersion AppVersion
28+
pluginTypeNames PluginTypeNames
29+
paths *Paths
2530
}
2631

2732
//const perm os.FileMode = os.ModePerm
@@ -44,7 +49,11 @@ var (
4449
)
4550

4651
// setSystemPaths
47-
func (service *SLocation) setSystemPaths() {
52+
func (service *SLocation) setSystemPaths() error {
53+
service.paths.ThisApplicationPaths.Name = service.appName
54+
//
55+
// System
56+
//
4857
sysPaths := &SystemPaths{
4958
Home: xdg.Home,
5059
DataHome: xdg.DataHome,
@@ -65,49 +74,77 @@ func (service *SLocation) setSystemPaths() {
6574
runtime.GOOS != "plan9" {
6675
sysPaths.UserLocalHome = filepath.Join(xdg.DataHome, ".local")
6776
sysPaths.UserApplicationsHome = filepath.Join(xdg.DataHome, "applications")
77+
78+
if !file.Exists(sysPaths.UserApplicationsHome) {
79+
var existApplicationsPath string
80+
for _, applicationsPath := range xdg.ApplicationDirs {
81+
if file.Exists(applicationsPath) {
82+
existApplicationsPath = applicationsPath
83+
break
84+
}
85+
}
86+
if existApplicationsPath != "" {
87+
sysPaths.UserApplicationsHome = existApplicationsPath
88+
} else {
89+
return errors.New("applications directory not found")
90+
}
91+
}
6892
}
6993

7094
service.paths.SystemPaths = sysPaths
7195

72-
relPath := fmt.Sprintf("%s/%s", service.appName, service.appName)
96+
relFilePath := fmt.Sprintf("%s/%s", service.appName, service.appName)
7397

74-
filePath, err := xdgCacheFile(relPath + ".cache")
98+
dataDir, err := xdgDataFile(string(service.appName))
7599
if err != nil {
76-
return
100+
return errors.Join(fmt.Errorf(`xdg.DataFile was unable to set "%s" path`, dataDir), err)
77101
}
102+
service.paths.ThisApplicationPaths.Paths.DataHome = dataDir
78103

79-
file, err := xdgConfigFile(relPath + ".yaml")
104+
configFile, err := xdgConfigFile(relFilePath + ".yaml")
80105
if err != nil {
81-
return
106+
return errors.Join(fmt.Errorf(`xdg.ConfigFile was unable to set "%s" path`, configFile), err)
82107
}
108+
service.paths.ThisApplicationPaths.Paths.ConfigFile = filepath.Dir(configFile)
83109

84-
tmpSecrets, err := xdgRuntimeFile(relPath + "/secrets")
110+
stateDir, err := xdg.StateFile(string(service.appName))
85111
if err != nil {
86-
return
112+
return errors.Join(fmt.Errorf(`xdg.StateFile was unable to set "%s" path`, stateDir), err)
87113
}
114+
service.paths.ThisApplicationPaths.Paths.StateHome = stateDir
88115

89-
tmpSecretsMTls, err := xdgRuntimeFile(relPath + "/secrets/mTLS")
116+
cacheFilePath, err := xdgCacheFile(relFilePath + ".cache")
90117
if err != nil {
91-
return
118+
return errors.Join(fmt.Errorf(`xdg.CacheFile was unable to set "%s" path`, cacheFilePath), err)
92119
}
120+
service.paths.ThisApplicationPaths.Paths.CacheHome = filepath.Dir(cacheFilePath)
121+
service.paths.ThisApplicationPaths.Paths.CacheFile = cacheFilePath
122+
123+
service.paths.ThisApplicationPaths.Paths.BinFile = fmt.Sprintf("%s/%s", xdg.BinHome, service.appName)
124+
125+
//for _, pluginTypeName := range service.
93126

94-
dataFile, err := xdgDataFile(relPath)
127+
service.paths.ThisApplicationPaths.Paths.PluginsHome = xdg.DataHome + "/plugins"
128+
129+
//
130+
// Secrets
131+
//
132+
secretsRelPath := fmt.Sprintf("%s/%s", service.appName, "/secrets")
133+
134+
tmpSecrets, err := xdgRuntimeFile(secretsRelPath)
95135
if err != nil {
96-
return
136+
return errors.Join(fmt.Errorf(`xdg.RuntimeFile was unable to set "%s" path`, tmpSecrets), err)
97137
}
138+
service.paths.ThisApplicationPaths.Paths.RuntimeDir = filepath.Dir(tmpSecrets)
139+
service.paths.ThisApplicationPaths.Paths.SecretsHome = tmpSecrets
98140

99-
service.paths.ThisApplicationPaths.Name = service.appName
100-
service.paths.ThisApplicationPaths.Paths.SecretsPaths.SecretsHome = tmpSecrets
101-
service.paths.ThisApplicationPaths.Paths.SecretsPaths.SecretsHome = tmpSecretsMTls
102-
//service.paths.ThisApplicationPaths.Paths.
103-
104-
/*xdg.RuntimeFile(relPath + "socket")
141+
tmpSecretsMTls, err := xdgRuntimeFile(secretsRelPath + "/mTLS")
142+
if err != nil {
143+
return errors.Join(fmt.Errorf(`xdg.RuntimeFile was unable to set "%s" path`, tmpSecrets), err)
144+
}
145+
service.paths.ThisApplicationPaths.Paths.MTLSHome = tmpSecretsMTls
105146

106-
xdg.SearchCacheFile()
107-
xdg.SearchConfigFile()
108-
xdg.SearchDataFile()
109-
xdg.SearchRuntimeFile()
110-
xdg.SearchStateFile()*/
147+
return nil
111148
}
112149

113150
// SystemPaths returns struct of all systems paths

location/service.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// Package location 📍
22
package location
33

4-
import (
5-
"fmt"
6-
"path/filepath"
7-
)
8-
94
// NewLocationService to set up the location service.
105
//
116
// Params:
127
// - 📇 appName - Is the of the application (normally all lowercase).
138
// - ♻️ appVersion - The version of the application.
14-
// - 💊 pluginTypeName - The plugin type name (e.g.: HERY => entity, doorman => clerk).
15-
func NewLocationService(appName, appVersion, pluginTypeName string) ILocation {
9+
// - 💊 pluginTypeName - The plugin type names (e.g.: HERY => entity, doorman => clerk).
10+
func NewLocationService(appName AppName, appVersion AppVersion, pluginTypeNames PluginTypeNames) (ILocation, error) {
1611
serviceLocation := &SLocation{
17-
appName: appName,
18-
appVersion: appVersion,
12+
appName: appName,
13+
appVersion: appVersion,
14+
pluginTypeNames: pluginTypeNames,
1915
}
2016

2117
// Set
22-
serviceLocation.setSystemPaths()
18+
err := serviceLocation.setSystemPaths()
19+
if err != nil {
20+
return nil, err
21+
}
2322

2423
sysPaths := serviceLocation.paths.SystemPaths
2524

@@ -43,12 +42,12 @@ func NewLocationService(appName, appVersion, pluginTypeName string) ILocation {
4342
if sysPaths.BinHome == "" {
4443
}*/
4544

46-
appDataHome := filepath.Join(sysPaths.DataHome, appName)
45+
/*appDataHome := filepath.Join(sysPaths.DataHome, appName)
4746
appConfigHome := filepath.Join(sysPaths.ConfigHome, appName)
4847
appStateHome := filepath.Join(sysPaths.StateHome, appName)
4948
appCacheHome := filepath.Join(sysPaths.CacheHome, appName)
5049
appRuntimeDir := filepath.Join(sysPaths.RuntimeDir, appName)
51-
appBinHome := filepath.Join(sysPaths.BinHome, appName)
50+
appBinHome := filepath.Join(sysPaths.BinHome, appName)*/
5251

5352
// TODO: Validate them
5453

@@ -57,7 +56,7 @@ func NewLocationService(appName, appVersion, pluginTypeName string) ILocation {
5756
SystemPaths: sysPaths,
5857
ThisApplicationPaths: &Application{
5958
Name: appName,
60-
Paths: &ApplicationPaths{
59+
/*Paths: &ApplicationPaths{
6160
DataHome: appDataHome,
6261
ConfigHome: appConfigHome,
6362
StateHome: appStateHome,
@@ -79,8 +78,8 @@ func NewLocationService(appName, appVersion, pluginTypeName string) ILocation {
7978
"applications",
8079
fmt.Sprintf("%s.desktop", appName),
8180
),
82-
},
81+
},*/
8382
},
8483
},
85-
}
84+
}, nil
8685
}

location/types.go

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import (
55
"github.com/adrg/xdg"
66
)
77

8+
// PluginTypeNames
9+
// TODO: Should be moved to the plugin package
10+
type PluginTypeNames []string
11+
type AppName string
12+
type AppVersion string
13+
814
// Paths 🛣️ contains all the paths
915
type Paths struct {
1016
// 🤖 SystemPaths contains the struct of the system paths
@@ -182,13 +188,13 @@ type AmadlaPaths struct {
182188

183189
// Application contains the specific paths and name/title of an application
184190
type Application struct {
185-
Name string
186-
Paths *ApplicationPaths
191+
Name AppName
192+
PluginTypeNames PluginTypeNames
193+
Paths *ApplicationPaths
187194
}
188195

189196
// ApplicationPaths contains all the path
190197
type ApplicationPaths struct {
191-
192198
//
193199
// Section: Basic
194200
//
@@ -248,14 +254,13 @@ type ApplicationPaths struct {
248254
// - 🪟 Windows: C:\Users\Username\AppData\Roaming (%APPDATA%)
249255
ConfigFile string
250256

251-
// TODO:
252257
// PluginsHome
253258
//
254259
// E.g.:
255-
// - 🐧 Linux: /home/username/.config/{appName}/plugins.d/
260+
// - 🐧 Linux: /home/username/.local/share/{appName}/{pluginTypeName}.d/
256261
// - 🍎 Mac OS X:
257262
// - 🪟 Windows:
258-
PluginsHome string
263+
PluginsHome map[string]string
259264

260265
// CacheFile is a SQLite3 file
261266
//
@@ -274,27 +279,10 @@ type ApplicationPaths struct {
274279
ApplicationsDesktopFile string
275280

276281
//
277-
// Section: Secrets
282+
// Secrets
278283
//
279284

280-
SecretsPaths *SecretsPaths
281-
282-
/*
283-
chmod 600 ~/.config/amadla/private/*.key ~/.config/amadla/mTLS/*.key
284-
chmod 644 ~/.config/amadla/certs/*.crt ~/.config/amadla/mTLS/*.crt
285-
*/
286-
287-
//
288-
// Temporary
289-
//
290-
291-
// ln -s ~/.local/lib/amadla/amadla ~/.local/bin/amadla
292-
293-
}
294-
295-
// SecretsPaths
296-
type SecretsPaths struct {
297-
// SecretsHome contains the directories and the files for all the secrets the application might need
285+
// Home contains the directories and the files for all the secrets the application might need
298286
// TODO: What about the tmp?
299287
// TODO: Is this the best place what about run directory for tmp secrets?
300288
// TODO: Is there other propositions for this?
@@ -306,50 +294,17 @@ type SecretsPaths struct {
306294

307295
// MTLSHome to be able to connect to `doorman` you need mTLS certification that are assigned temporally by `doorman`
308296
//
297+
// Note:
298+
//
299+
// chmod 600 ~/.config/amadla/private/*.key ~/.config/amadla/mTLS/*.key
300+
// chmod 644 ~/.config/amadla/certs/*.crt ~/.config/amadla/mTLS/*.crt
301+
//
309302
// E.g.:
310303
// - 🐧 Linux: /home/user/.config/{appName}/secrets/mTLS/
311304
// - 🍎 Mac OS X:
312305
// - 🪟 Windows:
313306
MTLSHome string
314-
}
315-
316-
type PathName string
317-
318-
const (
319-
Home PathName = "Home"
320-
DataHome PathName = "DataHome"
321-
DataDirs PathName = "DataDirs"
322-
ConfigHome PathName = "ConfigHome"
323-
ConfigDirs PathName = "ConfigDirs"
324-
StateHome PathName = "StateHome"
325-
CacheHome PathName = "CacheHome"
326-
RuntimeDir PathName = "RuntimeDir"
327-
BinHome PathName = "BinHome"
328-
UserDirs PathName = "UserDirs"
329-
FontDirs PathName = "FontDirs"
330-
ApplicationDirs PathName = "ApplicationDirs"
331-
UserApplicationsHome PathName = "UserApplicationsHome"
332-
)
333307

334-
type AppPathName string
335-
336-
const (
337-
AppDataHome AppPathName = "AppDataHome"
338-
AppConfigHome AppPathName = "AppConfigHome"
339-
AppStateHome AppPathName = "AppStateHome"
340-
AppCacheHome AppPathName = "AppCacheHome"
341-
AppRuntimeDir AppPathName = "AppRuntimeDir"
342-
AppBinFile AppPathName = "AppBinFile"
343-
AppConfigFile AppPathName = "AppConfigFile"
344-
AppPluginsHome AppPathName = "AppPluginsHome"
345-
AppCacheFile AppPathName = "AppCacheFile"
346-
ApplicationsHome AppPathName = "ApplicationsHome"
347-
AppApplicationsDesktopFile AppPathName = "ApplicationsDesktopFile"
348-
)
349-
350-
type AppSecretsPathName string
308+
// ln -s ~/.local/lib/amadla/amadla ~/.local/bin/amadla
351309

352-
const (
353-
AppSecretsHome AppSecretsPathName = "AppSecretsHome"
354-
MTLSHome AppSecretsPathName = "MTLSHome"
355-
)
310+
}

0 commit comments

Comments
 (0)