This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
extract.go
68 lines (61 loc) · 1.95 KB
/
extract.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
68
package plugins
import (
"fmt"
printer "github.com/KablamoOSS/go-cli-printer"
kombustionTypes "github.com/KablamoOSS/kombustion/types"
)
// ExtractParsersFromPlugins and ensure there are no clashes for plugin resource names
func ExtractParsersFromPlugins(
loadedPlugins []*PluginLoaded,
) (
parsers map[string]kombustionTypes.ParserFunc,
) {
parsers = make(map[string]kombustionTypes.ParserFunc)
for _, plugin := range loadedPlugins {
if *plugin.Parsers != nil {
for key, parserFunc := range *plugin.Parsers {
pluginKey := fmt.Sprintf("%s::%s", plugin.InternalConfig.Prefix, key)
if _, ok := parsers[pluginKey]; ok { // Check for duplicates
printer.Fatal(
fmt.Errorf("Plugin `%s` tried to load resource `%s` but it already exists", plugin.Config.Name, pluginKey),
fmt.Sprintf(
"You can add a `Alias` to this plugin in kombustion.yaml to resolve this.",
),
"https://www.kombustion.io/api/manifest/#alias-optional",
)
} else {
// This function has the same type as types.ParserFunc, but we need to define the type
// as follows, becuase it's crossing the host/plugin boundary
wrappedParserFunc := func(
name string,
data string,
) (
source string,
conditions kombustionTypes.TemplateObject,
metadata kombustionTypes.TemplateObject,
mappings kombustionTypes.TemplateObject,
outputs kombustionTypes.TemplateObject,
parameters kombustionTypes.TemplateObject,
resources kombustionTypes.TemplateObject,
transform kombustionTypes.TemplateObject,
errors []error,
) {
source = plugin.Config.Name
conditions,
metadata,
mappings,
outputs,
parameters,
resources,
transform,
errors = unmarshallParser(parserFunc(name, data))
return
} // </wrappedParserFunc>
// Add the parser func to our parsers
parsers[pluginKey] = wrappedParserFunc
}
}
}
}
return
}