11package generator
22
33import (
4- "embed"
5- "os "
4+ _ "embed"
5+ "io "
66 "strings"
77 "text/template"
88
@@ -11,10 +11,10 @@ import (
1111)
1212
1313//go:embed recipe.yaml
14- var file embed. FS
14+ var RecipeTemplate string
1515
16- // templateData represents the template for generating a recipe.
17- type templateData struct {
16+ // TemplateData represents the template for generating a recipe.
17+ type TemplateData struct {
1818 Name string
1919 Version string
2020 Source struct {
@@ -26,7 +26,7 @@ type templateData struct {
2626 Processors map [string ]string
2727}
2828
29- var templateFuncs = map [string ]interface {}{
29+ var TemplateFuncs = map [string ]interface {}{
3030 "indent" : indent ,
3131 "rawfmt" : rawfmt ,
3232}
@@ -42,8 +42,8 @@ type RecipeParams struct {
4242}
4343
4444// Recipe checks if the recipe is valid and returns a Template
45- func Recipe (p RecipeParams ) error {
46- tem := templateData {
45+ func Recipe (p RecipeParams ) ( * TemplateData , error ) {
46+ tem := & TemplateData {
4747 Name : p .Name ,
4848 Version : recipeVersions [len (recipeVersions )- 1 ],
4949 }
@@ -54,7 +54,7 @@ func Recipe(p RecipeParams) error {
5454
5555 sourceInfo , err := registry .Extractors .Info (p .Source )
5656 if err != nil {
57- return errors .Wrap (err , "failed to provide extractor information" )
57+ return nil , errors .Wrap (err , "failed to provide extractor information" )
5858 }
5959
6060 tem .Source .SampleConfig = sourceInfo .SampleConfig
@@ -64,7 +64,7 @@ func Recipe(p RecipeParams) error {
6464 for _ , sink := range p .Sinks {
6565 info , err := registry .Sinks .Info (sink )
6666 if err != nil {
67- return errors .Wrap (err , "failed to provide sink information" )
67+ return nil , errors .Wrap (err , "failed to provide sink information" )
6868 }
6969 tem .Sinks [sink ] = info .SampleConfig
7070 }
@@ -74,16 +74,25 @@ func Recipe(p RecipeParams) error {
7474 for _ , procc := range p .Processors {
7575 info , err := registry .Processors .Info (procc )
7676 if err != nil {
77- return errors .Wrap (err , "failed to provide processor information" )
77+ return nil , errors .Wrap (err , "failed to provide processor information" )
7878 }
7979 tem .Processors [procc ] = info .SampleConfig
8080 }
8181 }
8282
83+ return tem , nil
84+ }
85+
86+ // RecipeWriteTo build and apply recipe to provided io writer
87+ func RecipeWriteTo (p RecipeParams , writer io.Writer ) error {
88+ tem , err := Recipe (p )
89+ if err != nil {
90+ return err
91+ }
8392 tmpl := template .Must (
84- template .New ("recipe.yaml" ).Funcs (templateFuncs ). ParseFS ( file , "*" ),
93+ template .New ("recipe.yaml" ).Funcs (TemplateFuncs ). Parse ( RecipeTemplate ),
8594 )
86- if err := tmpl .Execute (os . Stdout , tem ); err != nil {
95+ if err := tmpl .Execute (writer , * tem ); err != nil {
8796 return errors .Wrap (err , "failed to execute template" )
8897 }
8998 return nil
0 commit comments