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
14 changes: 13 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewCmd() *cobra.Command {
func NewRecipeCmd() *cobra.Command {
var (
extractor string
scope string
sinks string
processors string
)
Expand Down Expand Up @@ -88,14 +89,25 @@ func NewRecipeCmd() *cobra.Command {
}
}

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

cmd.Flags().StringVarP(&extractor, "extractor", "e", "", "Type of extractor")
cmd.Flags().StringVarP(&scope, "scope", "n", "", "URN's namespace")
cmd.Flags().StringVarP(&sinks, "sinks", "s", "", "List of sink types")
cmd.Flags().StringVarP(&processors, "processors", "p", "", "List of processor types")

if err := cmd.MarkFlagRequired("scope"); err != nil {
panic(err)
}

return cmd

}
Expand Down
49 changes: 32 additions & 17 deletions generator/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import (
//go:embed recipe.yaml
var file embed.FS

// Template represents the template for generating a recipe.
type Template struct {
Name string
Version string
Source map[string]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
}
Expand All @@ -28,34 +32,45 @@ var templateFuncs = map[string]interface{}{

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(name string, source string, sinks []string, processors []string) (err error) {
tem := Template{
Name: name,
func Recipe(p RecipeParams) error {
tem := templateData{
Name: p.Name,
Version: recipeVersions[len(recipeVersions)-1],
}

if source != "" {
tem.Source = make(map[string]string)
sourceInfo, err := registry.Extractors.Info(source)
if p.Source != "" {
tem.Source.Name = p.Source
tem.Source.Scope = p.Scope

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

tem.Source.SampleConfig = sourceInfo.SampleConfig
}
if len(sinks) > 0 {
if len(p.Sinks) > 0 {
tem.Sinks = make(map[string]string)
for _, sink := range sinks {
for _, sink := range p.Sinks {
info, err := registry.Sinks.Info(sink)
if err != nil {
return errors.Wrap(err, "failed to provide sink information")
}
tem.Sinks[sink] = info.SampleConfig
}
}
if len(processors) > 0 {
if len(p.Processors) > 0 {
tem.Processors = make(map[string]string)
for _, procc := range processors {
for _, procc := range p.Processors {
info, err := registry.Processors.Info(procc)
if err != nil {
return errors.Wrap(err, "failed to provide processor information")
Expand All @@ -67,7 +82,7 @@ func Recipe(name string, source string, sinks []string, processors []string) (er
tmpl := template.Must(
template.New("recipe.yaml").Funcs(templateFuncs).ParseFS(file, "*"),
)
if err = tmpl.Execute(os.Stdout, tem); err != nil {
if err := tmpl.Execute(os.Stdout, tem); err != nil {
return errors.Wrap(err, "failed to execute template")
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions generator/recipe.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: {{.Name}}
version: {{.Version}}
source:
{{- range $key, $value := .Source }}
type: {{$key}}
config: {{$value | indent 4}}
{{- with .Source }}
name: {{.Name}}
scope: {{.Scope}}
config: {{.SampleConfig | indent 4}}
{{- end }}
{{- if ne (len .Sinks) 0 }}
sinks:
Expand Down