-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrecipe.go
More file actions
116 lines (99 loc) · 2.5 KB
/
recipe.go
File metadata and controls
116 lines (99 loc) · 2.5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package generator
import (
_ "embed"
"io"
"strings"
"text/template"
"github.com/odpf/meteor/registry"
"github.com/pkg/errors"
)
//go:embed recipe.yaml
var RecipeTemplate string
// TemplateData represents the template for generating a recipe.
type TemplateData struct {
Name string
Version string
Source struct {
Name string
Scope string
SampleConfig string
}
Sinks map[string]string
Processors map[string]string
}
var TemplateFuncs = map[string]interface{}{
"indent": indent,
"rawfmt": rawfmt,
}
var recipeVersions = [1]string{"v1beta1"}
type RecipeParams struct {
Name string
Source string
Scope string
Sinks []string
Processors []string
}
// Recipe checks if the recipe is valid and returns a Template
func Recipe(p RecipeParams) (*TemplateData, error) {
tem := &TemplateData{
Name: p.Name,
Version: recipeVersions[len(recipeVersions)-1],
}
if p.Source != "" {
tem.Source.Name = p.Source
tem.Source.Scope = p.Scope
sourceInfo, err := registry.Extractors.Info(p.Source)
if err != nil {
return nil, errors.Wrap(err, "failed to provide extractor information")
}
tem.Source.SampleConfig = sourceInfo.SampleConfig
}
if len(p.Sinks) > 0 {
tem.Sinks = make(map[string]string)
for _, sink := range p.Sinks {
info, err := registry.Sinks.Info(sink)
if err != nil {
return nil, errors.Wrap(err, "failed to provide sink information")
}
tem.Sinks[sink] = info.SampleConfig
}
}
if len(p.Processors) > 0 {
tem.Processors = make(map[string]string)
for _, procc := range p.Processors {
info, err := registry.Processors.Info(procc)
if err != nil {
return nil, errors.Wrap(err, "failed to provide processor information")
}
tem.Processors[procc] = info.SampleConfig
}
}
return tem, nil
}
// RecipeWriteTo build and apply recipe to provided io writer
func RecipeWriteTo(p RecipeParams, writer io.Writer) error {
tem, err := Recipe(p)
if err != nil {
return err
}
tmpl := template.Must(
template.New("recipe.yaml").Funcs(TemplateFuncs).Parse(RecipeTemplate),
)
if err := tmpl.Execute(writer, *tem); err != nil {
return errors.Wrap(err, "failed to execute template")
}
return nil
}
func indent(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
}
func rawfmt(s string) string {
if !strings.HasPrefix(s, "\n") {
s = "\n" + s
}
return strings.ReplaceAll(s, "\t", " ")
}
func GetRecipeVersions() [1]string {
return recipeVersions
}