@@ -82,10 +82,10 @@ func (alts *LibraryAlternatives) FindVersion(version *semver.Version) *libraries
82
82
}
83
83
84
84
// Names returns an array with all the names of the installed libraries.
85
- func (sc LibrariesManager ) Names () []string {
86
- res := make ([]string , len (sc .Libraries ))
85
+ func (lm LibrariesManager ) Names () []string {
86
+ res := make ([]string , len (lm .Libraries ))
87
87
i := 0
88
- for n := range sc .Libraries {
88
+ for n := range lm .Libraries {
89
89
res [i ] = n
90
90
i ++
91
91
}
@@ -109,27 +109,27 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
109
109
110
110
// LoadIndex reads a library_index.json from a file and returns
111
111
// the corresponding Index structure.
112
- func (sc * LibrariesManager ) LoadIndex () error {
113
- index , err := librariesindex .LoadIndex (sc .IndexFile )
112
+ func (lm * LibrariesManager ) LoadIndex () error {
113
+ index , err := librariesindex .LoadIndex (lm .IndexFile )
114
114
if err != nil {
115
- sc .Index = librariesindex .EmptyIndex
115
+ lm .Index = librariesindex .EmptyIndex
116
116
return err
117
117
}
118
- sc .Index = index
118
+ lm .Index = index
119
119
return nil
120
120
}
121
121
122
122
// AddLibrariesDir adds path to the list of directories
123
123
// to scan when searching for libraries. If a path is already
124
124
// in the list it is ignored.
125
- func (sc * LibrariesManager ) AddLibrariesDir (path * paths.Path , location libraries.LibraryLocation ) {
126
- for _ , dir := range sc .LibrariesDir {
125
+ func (lm * LibrariesManager ) AddLibrariesDir (path * paths.Path , location libraries.LibraryLocation ) {
126
+ for _ , dir := range lm .LibrariesDir {
127
127
if dir .Path .EquivalentTo (path ) {
128
128
return
129
129
}
130
130
}
131
131
logrus .WithField ("dir" , path ).WithField ("location" , location .String ()).Info ("Adding libraries dir" )
132
- sc .LibrariesDir = append (sc .LibrariesDir , & LibrariesDir {
132
+ lm .LibrariesDir = append (lm .LibrariesDir , & LibrariesDir {
133
133
Path : path ,
134
134
Location : location ,
135
135
})
@@ -138,36 +138,36 @@ func (sc *LibrariesManager) AddLibrariesDir(path *paths.Path, location libraries
138
138
// AddPlatformReleaseLibrariesDir add the libraries directory in the
139
139
// specified PlatformRelease to the list of directories to scan when
140
140
// searching for libraries.
141
- func (sc * LibrariesManager ) AddPlatformReleaseLibrariesDir (plaftormRelease * cores.PlatformRelease , location libraries.LibraryLocation ) {
141
+ func (lm * LibrariesManager ) AddPlatformReleaseLibrariesDir (plaftormRelease * cores.PlatformRelease , location libraries.LibraryLocation ) {
142
142
path := plaftormRelease .GetLibrariesDir ()
143
143
if path == nil {
144
144
return
145
145
}
146
- for _ , dir := range sc .LibrariesDir {
146
+ for _ , dir := range lm .LibrariesDir {
147
147
if dir .Path .EquivalentTo (path ) {
148
148
return
149
149
}
150
150
}
151
151
logrus .WithField ("dir" , path ).WithField ("location" , location .String ()).Info ("Adding libraries dir" )
152
- sc .LibrariesDir = append (sc .LibrariesDir , & LibrariesDir {
152
+ lm .LibrariesDir = append (lm .LibrariesDir , & LibrariesDir {
153
153
Path : path ,
154
154
Location : location ,
155
155
PlatformRelease : plaftormRelease ,
156
156
})
157
157
}
158
158
159
159
// RescanLibraries reload all installed libraries in the system.
160
- func (sc * LibrariesManager ) RescanLibraries () error {
161
- for _ , dir := range sc .LibrariesDir {
162
- if err := sc .LoadLibrariesFromDir (dir ); err != nil {
160
+ func (lm * LibrariesManager ) RescanLibraries () error {
161
+ for _ , dir := range lm .LibrariesDir {
162
+ if err := lm .LoadLibrariesFromDir (dir ); err != nil {
163
163
return fmt .Errorf ("loading libs from %s: %s" , dir .Path , err )
164
164
}
165
165
}
166
166
return nil
167
167
}
168
168
169
- func (sc * LibrariesManager ) getUserLibrariesDir () * paths.Path {
170
- for _ , dir := range sc .LibrariesDir {
169
+ func (lm * LibrariesManager ) getUserLibrariesDir () * paths.Path {
170
+ for _ , dir := range lm .LibrariesDir {
171
171
if dir .Location == libraries .User {
172
172
return dir .Path
173
173
}
@@ -177,7 +177,7 @@ func (sc *LibrariesManager) getUserLibrariesDir() *paths.Path {
177
177
178
178
// LoadLibrariesFromDir loads all libraries in the given directory. Returns
179
179
// nil if the directory doesn't exists.
180
- func (sc * LibrariesManager ) LoadLibrariesFromDir (librariesDir * LibrariesDir ) error {
180
+ func (lm * LibrariesManager ) LoadLibrariesFromDir (librariesDir * LibrariesDir ) error {
181
181
subDirs , err := librariesDir .Path .ReadDir ()
182
182
if os .IsNotExist (err ) {
183
183
return nil
@@ -194,22 +194,45 @@ func (sc *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) err
194
194
return fmt .Errorf ("loading library from %s: %s" , subDir , err )
195
195
}
196
196
library .ContainerPlatform = librariesDir .PlatformRelease
197
- alternatives , ok := sc .Libraries [library .Name ]
197
+ alternatives , ok := lm .Libraries [library .Name ]
198
198
if ! ok {
199
199
alternatives = & LibraryAlternatives {}
200
- sc .Libraries [library .Name ] = alternatives
200
+ lm .Libraries [library .Name ] = alternatives
201
201
}
202
202
alternatives .Add (library )
203
203
}
204
204
return nil
205
205
}
206
206
207
+ // LoadLibraryFromDir loads one single library from the libRootDir.
208
+ // libRootDir must point to the root of a valid library.
209
+ // An error is returned if the path doesn't exist or loading of the library fails.
210
+ func (lm * LibrariesManager ) LoadLibraryFromDir (libRootDir * paths.Path , location libraries.LibraryLocation ) error {
211
+ if libRootDir .NotExist () {
212
+ return fmt .Errorf ("library path does not exist: %s" , libRootDir )
213
+ }
214
+
215
+ library , err := libraries .Load (libRootDir , location )
216
+ if err != nil {
217
+ return fmt .Errorf ("loading library from %s: %s" , libRootDir , err )
218
+ }
219
+
220
+ alternatives , ok := lm .Libraries [library .Name ]
221
+ if ! ok {
222
+ alternatives = & LibraryAlternatives {}
223
+ lm .Libraries [library .Name ] = alternatives
224
+ }
225
+ alternatives .Add (library )
226
+
227
+ return nil
228
+ }
229
+
207
230
// FindByReference return the installed library matching the Reference
208
231
// name and version or, if the version is nil, the library installed
209
232
// in the User folder.
210
- func (sc * LibrariesManager ) FindByReference (libRef * librariesindex.Reference ) * libraries.Library {
233
+ func (lm * LibrariesManager ) FindByReference (libRef * librariesindex.Reference ) * libraries.Library {
211
234
saneName := utils .SanitizeName (libRef .Name )
212
- alternatives , have := sc .Libraries [saneName ]
235
+ alternatives , have := lm .Libraries [saneName ]
213
236
if ! have {
214
237
return nil
215
238
}
0 commit comments