@@ -46,9 +46,49 @@ type GalleryElement interface {
4646 SetGallery (gallery config.Gallery )
4747 SetInstalled (installed bool )
4848 GetName () string
49+ GetDescription () string
50+ GetTags () []string
4951 GetGallery () config.Gallery
5052}
5153
54+ type GalleryElements [T GalleryElement ] []T
55+
56+ func (gm GalleryElements [T ]) Search (term string ) GalleryElements [T ] {
57+ var filteredModels GalleryElements [T ]
58+
59+ for _ , m := range gm {
60+ if strings .Contains (m .GetName (), term ) ||
61+ strings .Contains (m .GetDescription (), term ) ||
62+ strings .Contains (m .GetGallery ().Name , term ) ||
63+ strings .Contains (strings .Join (m .GetTags (), "," ), term ) {
64+ filteredModels = append (filteredModels , m )
65+ }
66+ }
67+ return filteredModels
68+ }
69+
70+ func (gm GalleryElements [T ]) FindByName (name string ) T {
71+ for _ , m := range gm {
72+ if strings .EqualFold (m .GetName (), name ) {
73+ return m
74+ }
75+ }
76+ var zero T
77+ return zero
78+ }
79+
80+ func (gm GalleryElements [T ]) Paginate (pageNum int , itemsNum int ) GalleryElements [T ] {
81+ start := (pageNum - 1 ) * itemsNum
82+ end := start + itemsNum
83+ if start > len (gm ) {
84+ start = len (gm )
85+ }
86+ if end > len (gm ) {
87+ end = len (gm )
88+ }
89+ return gm [start :end ]
90+ }
91+
5292func FindGalleryElement [T GalleryElement ](models []T , name string , basePath string ) T {
5393 var model T
5494 name = strings .ReplaceAll (name , string (os .PathSeparator ), "__" )
@@ -76,7 +116,7 @@ func FindGalleryElement[T GalleryElement](models []T, name string, basePath stri
76116// List available models
77117// Models galleries are a list of yaml files that are hosted on a remote server (for example github).
78118// Each yaml file contains a list of models that can be downloaded and optionally overrides to define a new model setting.
79- func AvailableGalleryModels (galleries []config.Gallery , basePath string ) (GalleryModels , error ) {
119+ func AvailableGalleryModels (galleries []config.Gallery , basePath string ) (GalleryElements [ * GalleryModel ] , error ) {
80120 var models []* GalleryModel
81121
82122 // Get models from galleries
@@ -92,7 +132,7 @@ func AvailableGalleryModels(galleries []config.Gallery, basePath string) (Galler
92132}
93133
94134// List available backends
95- func AvailableBackends (galleries []config.Gallery , basePath string ) (GalleryBackends , error ) {
135+ func AvailableBackends (galleries []config.Gallery , basePath string ) (GalleryElements [ * GalleryBackend ] , error ) {
96136 var models []* GalleryBackend
97137
98138 // Get models from galleries
@@ -149,9 +189,13 @@ func getGalleryElements[T GalleryElement](gallery config.Gallery, basePath strin
149189 model .SetGallery (gallery )
150190 // we check if the model was already installed by checking if the config file exists
151191 // TODO: (what to do if the model doesn't install a config file?)
192+ // TODO: This is sub-optimal now that the gallery handles both backends and models - we need to abstract this away
152193 if _ , err := os .Stat (filepath .Join (basePath , fmt .Sprintf ("%s.yaml" , model .GetName ()))); err == nil {
153194 model .SetInstalled (true )
154195 }
196+ if _ , err := os .Stat (filepath .Join (basePath , model .GetName ())); err == nil {
197+ model .SetInstalled (true )
198+ }
155199 }
156200 return models , nil
157201}
0 commit comments