@@ -46,9 +46,49 @@ type GalleryElement interface {
46
46
SetGallery (gallery config.Gallery )
47
47
SetInstalled (installed bool )
48
48
GetName () string
49
+ GetDescription () string
50
+ GetTags () []string
49
51
GetGallery () config.Gallery
50
52
}
51
53
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
+
52
92
func FindGalleryElement [T GalleryElement ](models []T , name string , basePath string ) T {
53
93
var model T
54
94
name = strings .ReplaceAll (name , string (os .PathSeparator ), "__" )
@@ -76,7 +116,7 @@ func FindGalleryElement[T GalleryElement](models []T, name string, basePath stri
76
116
// List available models
77
117
// Models galleries are a list of yaml files that are hosted on a remote server (for example github).
78
118
// 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 ) {
80
120
var models []* GalleryModel
81
121
82
122
// Get models from galleries
@@ -92,7 +132,7 @@ func AvailableGalleryModels(galleries []config.Gallery, basePath string) (Galler
92
132
}
93
133
94
134
// List available backends
95
- func AvailableBackends (galleries []config.Gallery , basePath string ) (GalleryBackends , error ) {
135
+ func AvailableBackends (galleries []config.Gallery , basePath string ) (GalleryElements [ * GalleryBackend ] , error ) {
96
136
var models []* GalleryBackend
97
137
98
138
// Get models from galleries
@@ -149,9 +189,13 @@ func getGalleryElements[T GalleryElement](gallery config.Gallery, basePath strin
149
189
model .SetGallery (gallery )
150
190
// we check if the model was already installed by checking if the config file exists
151
191
// 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
152
193
if _ , err := os .Stat (filepath .Join (basePath , fmt .Sprintf ("%s.yaml" , model .GetName ()))); err == nil {
153
194
model .SetInstalled (true )
154
195
}
196
+ if _ , err := os .Stat (filepath .Join (basePath , model .GetName ())); err == nil {
197
+ model .SetInstalled (true )
198
+ }
155
199
}
156
200
return models , nil
157
201
}
0 commit comments