11package location
22
33import (
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
1014type ILocation interface {
@@ -19,9 +23,10 @@ type ILocation interface {
1923 MakePaths(paths AbsPaths) error*/
2024}
2125type 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
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
0 commit comments