get.porter.sh/porter@v1.3.0/pkg/mixin/query/manifest_generator.go (about) 1 package query 2 3 import ( 4 "fmt" 5 6 "get.porter.sh/porter/pkg/cnab" 7 "get.porter.sh/porter/pkg/manifest" 8 "get.porter.sh/porter/pkg/yaml" 9 ) 10 11 // ManifestGenerator generates mixin input from the manifest contents associated with each mixin. 12 type ManifestGenerator struct { 13 Manifest *manifest.Manifest 14 } 15 16 func NewManifestGenerator(m *manifest.Manifest) *ManifestGenerator { 17 return &ManifestGenerator{ 18 Manifest: m, 19 } 20 } 21 22 var _ MixinInputGenerator = &ManifestGenerator{} 23 24 func (g ManifestGenerator) ListMixins() []string { 25 mixinNames := make([]string, len(g.Manifest.Mixins)) 26 for i, mixin := range g.Manifest.Mixins { 27 mixinNames[i] = mixin.Name 28 } 29 return mixinNames 30 } 31 32 func (g ManifestGenerator) BuildInput(mixinName string) ([]byte, error) { 33 input := g.buildInputForMixin(mixinName) 34 inputB, err := yaml.Marshal(input) 35 if err != nil { 36 return nil, fmt.Errorf("could not marshal mixin build input for %s: %w", mixinName, err) 37 } 38 39 return inputB, nil 40 } 41 42 func (g ManifestGenerator) buildInputForMixin(mixinName string) BuildInput { 43 input := BuildInput{ 44 Actions: make(map[string]interface{}, 3), 45 } 46 47 for _, mixinDecl := range g.Manifest.Mixins { 48 if mixinName == mixinDecl.Name { 49 input.Config = mixinDecl.Config 50 } 51 } 52 53 filterSteps := func(action string, steps manifest.Steps) { 54 mixinSteps := manifest.Steps{} 55 for _, step := range steps { 56 if step.GetMixinName() != mixinName { 57 continue 58 } 59 mixinSteps = append(mixinSteps, step) 60 } 61 input.Actions[action] = mixinSteps 62 } 63 filterSteps(cnab.ActionInstall, g.Manifest.Install) 64 filterSteps(cnab.ActionUpgrade, g.Manifest.Upgrade) 65 filterSteps(cnab.ActionUninstall, g.Manifest.Uninstall) 66 67 for action, steps := range g.Manifest.CustomActions { 68 filterSteps(action, steps) 69 } 70 71 return input 72 }