Skip to content

Commit ab54361

Browse files
authored
fix: extract io writer in recipe generator (#466)
meteor new command allows user to generate recipe, although the functionality is fine, I want use this method to generate recipe outside of meteor as well. If this is being called programmatically printing it to hard coded stdout blocked me to take the output as a string. Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
1 parent 2309f9f commit ab54361

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

cmd/new.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"errors"
5+
"os"
56
"strings"
67

78
"github.com/AlecAivazis/survey/v2"
@@ -89,13 +90,13 @@ func NewRecipeCmd() *cobra.Command {
8990
}
9091
}
9192

92-
return generator.Recipe(generator.RecipeParams{
93+
return generator.RecipeWriteTo(generator.RecipeParams{
9394
Name: args[0],
9495
Source: extractor,
9596
Scope: scope,
9697
Sinks: sinkList,
9798
Processors: procList,
98-
})
99+
}, os.Stdout)
99100
},
100101
}
101102

generator/recipe.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package generator
22

33
import (
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

Comments
 (0)