github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/buildplan/ingredient.go (about) 1 package buildplan 2 3 import ( 4 "github.com/ActiveState/cli/pkg/buildplan/raw" 5 "github.com/go-openapi/strfmt" 6 ) 7 8 type Ingredient struct { 9 *raw.IngredientSource 10 11 IsBuildtimeDependency bool 12 IsRuntimeDependency bool 13 Artifacts []*Artifact 14 15 platforms []strfmt.UUID 16 } 17 18 type Ingredients []*Ingredient 19 20 type IngredientIDMap map[strfmt.UUID]*Ingredient 21 22 type IngredientNameMap map[string]*Ingredient 23 24 func (i Ingredients) Filter(filters ...filterIngredient) Ingredients { 25 if len(filters) == 0 { 26 return i 27 } 28 ingredients := []*Ingredient{} 29 for _, ig := range i { 30 include := true 31 for _, filter := range filters { 32 if !filter(ig) { 33 include = false 34 break 35 } 36 } 37 if include { 38 ingredients = append(ingredients, ig) 39 } 40 } 41 return ingredients 42 } 43 44 func (i Ingredients) ToIDMap() IngredientIDMap { 45 result := make(map[strfmt.UUID]*Ingredient, len(i)) 46 for _, ig := range i { 47 result[ig.IngredientID] = ig 48 } 49 return result 50 } 51 52 func (i Ingredients) ToNameMap() IngredientNameMap { 53 result := make(map[string]*Ingredient, len(i)) 54 for _, ig := range i { 55 result[ig.Name] = ig 56 } 57 return result 58 } 59 60 func (i *Ingredient) RuntimeDependencies(recursive bool) Ingredients { 61 return i.runtimeDependencies(recursive, make(map[strfmt.UUID]struct{})) 62 } 63 64 func (i *Ingredient) runtimeDependencies(recursive bool, seen map[strfmt.UUID]struct{}) Ingredients { 65 // Guard against recursion, because multiple artifacts can refer to the same ingredient 66 if _, ok := seen[i.IngredientID]; ok { 67 return Ingredients{} 68 } 69 seen[i.IngredientID] = struct{}{} 70 71 dependencies := Ingredients{} 72 for _, a := range i.Artifacts { 73 for _, ac := range a.children { 74 if ac.Relation != RuntimeRelation { 75 continue 76 } 77 dependencies = append(dependencies, ac.Artifact.Ingredients...) 78 if recursive { 79 for _, ic := range ac.Artifact.Ingredients { 80 dependencies = append(dependencies, ic.runtimeDependencies(recursive, seen)...) 81 } 82 } 83 } 84 } 85 return dependencies 86 }