Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"os"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -89,13 +90,13 @@ func NewRecipeCmd() *cobra.Command {
}
}

return generator.Recipe(generator.RecipeParams{
return generator.RecipeWriteTo(generator.RecipeParams{
Name: args[0],
Source: extractor,
Scope: scope,
Sinks: sinkList,
Processors: procList,
})
}, os.Stdout)
},
}

Expand Down
35 changes: 22 additions & 13 deletions generator/recipe.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package generator

import (
"embed"
"os"
_ "embed"
"io"
"strings"
"text/template"

Expand All @@ -11,10 +11,10 @@ import (
)

//go:embed recipe.yaml
var file embed.FS
var RecipeTemplate string

// templateData represents the template for generating a recipe.
type templateData struct {
// TemplateData represents the template for generating a recipe.
type TemplateData struct {
Name string
Version string
Source struct {
Expand All @@ -26,7 +26,7 @@ type templateData struct {
Processors map[string]string
}

var templateFuncs = map[string]interface{}{
var TemplateFuncs = map[string]interface{}{
"indent": indent,
"rawfmt": rawfmt,
}
Expand All @@ -42,8 +42,8 @@ type RecipeParams struct {
}

// Recipe checks if the recipe is valid and returns a Template
func Recipe(p RecipeParams) error {
tem := templateData{
func Recipe(p RecipeParams) (*TemplateData, error) {
tem := &TemplateData{
Name: p.Name,
Version: recipeVersions[len(recipeVersions)-1],
}
Expand All @@ -54,7 +54,7 @@ func Recipe(p RecipeParams) error {

sourceInfo, err := registry.Extractors.Info(p.Source)
if err != nil {
return errors.Wrap(err, "failed to provide extractor information")
return nil, errors.Wrap(err, "failed to provide extractor information")
}

tem.Source.SampleConfig = sourceInfo.SampleConfig
Expand All @@ -64,7 +64,7 @@ func Recipe(p RecipeParams) error {
for _, sink := range p.Sinks {
info, err := registry.Sinks.Info(sink)
if err != nil {
return errors.Wrap(err, "failed to provide sink information")
return nil, errors.Wrap(err, "failed to provide sink information")
}
tem.Sinks[sink] = info.SampleConfig
}
Expand All @@ -74,16 +74,25 @@ func Recipe(p RecipeParams) error {
for _, procc := range p.Processors {
info, err := registry.Processors.Info(procc)
if err != nil {
return errors.Wrap(err, "failed to provide processor information")
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).ParseFS(file, "*"),
template.New("recipe.yaml").Funcs(TemplateFuncs).Parse(RecipeTemplate),
)
if err := tmpl.Execute(os.Stdout, tem); err != nil {
if err := tmpl.Execute(writer, *tem); err != nil {
return errors.Wrap(err, "failed to execute template")
}
return nil
Expand Down