get.porter.sh/porter@v1.3.0/pkg/cnab/extensions.go (about) 1 package cnab 2 3 import "fmt" 4 5 const ( 6 // PorterExtension is the key for all Porter configuration stored in the custom section of bundles. 7 // Since it was defined before we had porter.sh, it uses our legacy domain. 8 PorterExtension = "sh.porter" 9 10 // OfficialExtensionsPrefix is the prefix applied to extensions defined in the CNAB spec. 11 OfficialExtensionsPrefix = "io.cnab." 12 13 // PorterInternal is the identifier that we put in the $comment of fields in bundle.json 14 // to indicate that it's just for Porter and shouldn't be visible to the end users. 15 PorterInternal = "porter-internal" 16 ) 17 18 // RequiredExtension represents a required extension that is known and supported by Porter 19 type RequiredExtension struct { 20 Shorthand string 21 Key string 22 Schema string 23 Reader func(b ExtendedBundle) (interface{}, error) 24 } 25 26 // SupportedExtensions represent a listing of the current required extensions 27 // that Porter supports 28 var SupportedExtensions = []RequiredExtension{ 29 DependenciesV1Extension, 30 DependenciesV2Extension, 31 DockerExtension, 32 FileParameterExtension, 33 ParameterSourcesExtension, 34 } 35 36 // ProcessedExtensions represents a map of the extension name to the 37 // processed extension configuration 38 type ProcessedExtensions map[string]interface{} 39 40 // ProcessRequiredExtensions checks all required extensions in the provided 41 // bundle and makes sure Porter supports them. 42 // 43 // If an unsupported required extension is found, an error is returned. 44 // 45 // For each supported required extension, the configuration for that extension 46 // is read and returned in the form of a map of the extension name to 47 // the extension configuration 48 func (b ExtendedBundle) ProcessRequiredExtensions() (ProcessedExtensions, error) { 49 processed := ProcessedExtensions{} 50 for _, reqExt := range b.RequiredExtensions { 51 supportedExtension, err := GetSupportedExtension(reqExt) 52 if err != nil { 53 return processed, err 54 } 55 56 raw, err := supportedExtension.Reader(b) 57 if err != nil { 58 return processed, fmt.Errorf("unable to process extension: %s: %w", reqExt, err) 59 } 60 61 processed[supportedExtension.Key] = raw 62 } 63 64 return processed, nil 65 } 66 67 // GetSupportedExtension returns a supported extension according to the 68 // provided name, or an error 69 func GetSupportedExtension(e string) (*RequiredExtension, error) { 70 for _, ext := range SupportedExtensions { 71 // TODO(v1) we should only check for the key in v1.0.0 72 // We are checking for both because of a bug in the cnab dependencies spec 73 // https://github.com/cnabio/cnab-spec/issues/403 74 if e == ext.Key || e == ext.Shorthand { 75 return &ext, nil 76 } 77 } 78 return nil, fmt.Errorf("unsupported required extension: %s", e) 79 } 80 81 // SupportsExtension checks if the bundle supports the specified CNAB extension. 82 func (b ExtendedBundle) SupportsExtension(key string) bool { 83 for _, ext := range b.RequiredExtensions { 84 if key == ext { 85 return true 86 } 87 } 88 return false 89 }