Skip to content

Commit 0bd9107

Browse files
gloursndeloof
authored andcommitted
add functions to list models defined and services using models
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 6d8281c commit 0bd9107

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

loader/loader_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
is "gotest.tools/v3/assert/cmp"
3333

3434
"github.com/compose-spec/compose-go/v2/types"
35+
"github.com/compose-spec/compose-go/v2/utils"
3536
)
3637

3738
func buildConfigDetails(yaml string, env map[string]string) types.ConfigDetails {
@@ -3899,6 +3900,8 @@ models:
38993900
ModelVariable: "MODEL",
39003901
},
39013902
})
3903+
assert.DeepEqual(t, p.ModelNames(), []string{"foo"})
3904+
assert.Check(t, utils.ArrayContains(p.ServicesWithModels(), []string{"test_array", "test_mapping"}), p.ServicesWithModels())
39023905
}
39033906

39043907
func TestAttestations(t *testing.T) {

types/project.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ func (p *Project) ConfigNames() []string {
118118
return names
119119
}
120120

121+
// ModelNames return names for all models in this Compose config
122+
func (p *Project) ModelNames() []string {
123+
var names []string
124+
for k := range p.Models {
125+
names = append(names, k)
126+
}
127+
sort.Strings(names)
128+
return names
129+
}
130+
121131
func (p *Project) ServicesWithBuild() []string {
122132
servicesBuild := p.Services.Filter(func(s ServiceConfig) bool {
123133
return s.Build != nil && s.Build.Context != ""
@@ -139,6 +149,11 @@ func (p *Project) ServicesWithDependsOn() []string {
139149
return slices.Collect(maps.Keys(servicesDependsOn))
140150
}
141151

152+
func (p *Project) ServicesWithModels() []string {
153+
servicesModels := p.Services.Filter(func(s ServiceConfig) bool { return len(s.Models) > 0 })
154+
return slices.Collect(maps.Keys(servicesModels))
155+
}
156+
142157
func (p *Project) ServicesWithCapabilities() ([]string, []string, []string) {
143158
capabilities := []string{}
144159
gpu := []string{}

0 commit comments

Comments
 (0)