-
Notifications
You must be signed in to change notification settings - Fork 208
/
required.go
67 lines (58 loc) · 2.05 KB
/
required.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cnab
import (
"github.com/pkg/errors"
)
// RequiredExtension represents a required extension that is known and supported by Porter
type RequiredExtension struct {
Shorthand string
Key string
Schema string
Reader func(b ExtendedBundle) (interface{}, error)
}
// SupportedExtensions represent a listing of the current required extensions
// that Porter supports
var SupportedExtensions = []RequiredExtension{
DependenciesExtension,
DockerExtension,
FileParameterExtension,
ParameterSourcesExtension,
}
// ProcessedExtensions represents a map of the extension name to the
// processed extension configuration
type ProcessedExtensions map[string]interface{}
// ProcessRequiredExtensions checks all required extensions in the provided
// bundle and makes sure Porter supports them.
//
// If an unsupported required extension is found, an error is returned.
//
// For each supported required extension, the configuration for that extension
// is read and returned in the form of a map of the extension name to
// the extension configuration
func (b ExtendedBundle) ProcessRequiredExtensions() (ProcessedExtensions, error) {
processed := ProcessedExtensions{}
for _, reqExt := range b.RequiredExtensions {
supportedExtension, err := GetSupportedExtension(reqExt)
if err != nil {
return processed, err
}
raw, err := supportedExtension.Reader(b)
if err != nil {
return processed, errors.Wrapf(err, "unable to process extension: %s", reqExt)
}
processed[supportedExtension.Key] = raw
}
return processed, nil
}
// GetSupportedExtension returns a supported extension according to the
// provided name, or an error
func GetSupportedExtension(e string) (*RequiredExtension, error) {
for _, ext := range SupportedExtensions {
// TODO(v1) we should only check for the key in v1.0.0
// We are checking for both because of a bug in the cnab dependencies spec
// https://github.com/cnabio/cnab-spec/issues/403
if e == ext.Key || e == ext.Shorthand {
return &ext, nil
}
}
return nil, errors.Errorf("unsupported required extension: %s", e)
}