@@ -82,10 +82,10 @@ func (alts *LibraryAlternatives) FindVersion(version *semver.Version) *libraries
8282}
8383
8484// 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 ))
8787 i := 0
88- for n := range sc .Libraries {
88+ for n := range lm .Libraries {
8989 res [i ] = n
9090 i ++
9191 }
@@ -109,27 +109,27 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
109109
110110// LoadIndex reads a library_index.json from a file and returns
111111// 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 )
114114 if err != nil {
115- sc .Index = librariesindex .EmptyIndex
115+ lm .Index = librariesindex .EmptyIndex
116116 return err
117117 }
118- sc .Index = index
118+ lm .Index = index
119119 return nil
120120}
121121
122122// AddLibrariesDir adds path to the list of directories
123123// to scan when searching for libraries. If a path is already
124124// 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 {
127127 if dir .Path .EquivalentTo (path ) {
128128 return
129129 }
130130 }
131131 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 {
133133 Path : path ,
134134 Location : location ,
135135 })
@@ -138,36 +138,36 @@ func (sc *LibrariesManager) AddLibrariesDir(path *paths.Path, location libraries
138138// AddPlatformReleaseLibrariesDir add the libraries directory in the
139139// specified PlatformRelease to the list of directories to scan when
140140// 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 ) {
142142 path := plaftormRelease .GetLibrariesDir ()
143143 if path == nil {
144144 return
145145 }
146- for _ , dir := range sc .LibrariesDir {
146+ for _ , dir := range lm .LibrariesDir {
147147 if dir .Path .EquivalentTo (path ) {
148148 return
149149 }
150150 }
151151 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 {
153153 Path : path ,
154154 Location : location ,
155155 PlatformRelease : plaftormRelease ,
156156 })
157157}
158158
159159// 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 {
163163 return fmt .Errorf ("loading libs from %s: %s" , dir .Path , err )
164164 }
165165 }
166166 return nil
167167}
168168
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 {
171171 if dir .Location == libraries .User {
172172 return dir .Path
173173 }
@@ -177,7 +177,7 @@ func (sc *LibrariesManager) getUserLibrariesDir() *paths.Path {
177177
178178// LoadLibrariesFromDir loads all libraries in the given directory. Returns
179179// nil if the directory doesn't exists.
180- func (sc * LibrariesManager ) LoadLibrariesFromDir (librariesDir * LibrariesDir ) error {
180+ func (lm * LibrariesManager ) LoadLibrariesFromDir (librariesDir * LibrariesDir ) error {
181181 subDirs , err := librariesDir .Path .ReadDir ()
182182 if os .IsNotExist (err ) {
183183 return nil
@@ -194,22 +194,45 @@ func (sc *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) err
194194 return fmt .Errorf ("loading library from %s: %s" , subDir , err )
195195 }
196196 library .ContainerPlatform = librariesDir .PlatformRelease
197- alternatives , ok := sc .Libraries [library .Name ]
197+ alternatives , ok := lm .Libraries [library .Name ]
198198 if ! ok {
199199 alternatives = & LibraryAlternatives {}
200- sc .Libraries [library .Name ] = alternatives
200+ lm .Libraries [library .Name ] = alternatives
201201 }
202202 alternatives .Add (library )
203203 }
204204 return nil
205205}
206206
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+
207230// FindByReference return the installed library matching the Reference
208231// name and version or, if the version is nil, the library installed
209232// in the User folder.
210- func (sc * LibrariesManager ) FindByReference (libRef * librariesindex.Reference ) * libraries.Library {
233+ func (lm * LibrariesManager ) FindByReference (libRef * librariesindex.Reference ) * libraries.Library {
211234 saneName := utils .SanitizeName (libRef .Name )
212- alternatives , have := sc .Libraries [saneName ]
235+ alternatives , have := lm .Libraries [saneName ]
213236 if ! have {
214237 return nil
215238 }
0 commit comments