diff --git a/cmd/gen.go b/cmd/gen.go index ca93b00ee..4e6b4432e 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -57,9 +57,17 @@ func GenRecipeCmd() *cobra.Command { "group:core": "true", }, RunE: func(cmd *cobra.Command, args []string) error { - sinkList := strings.Split(sinks, ",") - procList := strings.Split(processors, ",") + var sinkList []string + var procList []string + + if sinks != "" { + sinkList = strings.Split(sinks, ",") + } + + if processors != "" { + procList = strings.Split(processors, ",") + } return generator.Recipe(args[0], extractor, sinkList, procList) }, } diff --git a/generator/recipe.go b/generator/recipe.go index 9f7fb22c7..03b0da66b 100644 --- a/generator/recipe.go +++ b/generator/recipe.go @@ -23,25 +23,39 @@ type Template struct { } // Recipe checks if the recipe is valid and returns a Template -func Recipe(name string, source string, sinks []string, processors []string) error { +func Recipe(name string, source string, sinks []string, processors []string) (err error) { tem := Template{ - Name: name, - Source: make(map[string]string), - Sinks: make(map[string]string), - Processors: make(map[string]string), + Name: name, } - sinfo, _ := registry.Extractors.Info(source) - tem.Source[source] = indent.String(sinfo.SampleConfig, size) - - for _, sink := range sinks { - info, _ := registry.Sinks.Info(sink) - tem.Sinks[sink] = indent.String(info.SampleConfig, size+3) + if source != "" { + tem.Source = make(map[string]string) + sinfo, err := registry.Extractors.Info(source) + if err != nil { + return err + } + tem.Source[source] = indent.String(sinfo.SampleConfig, size) + } + if len(sinks) > 0 { + tem.Sinks = make(map[string]string) + for _, sink := range sinks { + info, err := registry.Sinks.Info(sink) + if err != nil { + return err + } + tem.Sinks[sink] = indent.String(info.SampleConfig, size+3) + } } - for _, procc := range processors { - info, _ := registry.Processors.Info(procc) - tem.Processors[procc] = indent.String(info.SampleConfig, size+3) + if len(processors) > 0 { + tem.Processors = make(map[string]string) + for _, procc := range processors { + info, err := registry.Processors.Info(procc) + if err != nil { + return err + } + tem.Processors[procc] = indent.String(info.SampleConfig, size+3) + } } tmpl, err := template.ParseFS(file, "*") diff --git a/generator/recipe.yaml b/generator/recipe.yaml index 1c7127f76..a222d0a09 100644 --- a/generator/recipe.yaml +++ b/generator/recipe.yaml @@ -4,13 +4,21 @@ source: type: {{$key}} config: {{$value}} {{- end }} +{{- if ne (len .Sinks) 0 }} sinks: {{- range $key, $value := .Sinks }} - name: {{$key}} + {{- if $value}} config: {{$value}} + {{- end }} {{- end }} +{{- end }} +{{- if ne (len .Processors) 0 }} processors: {{- range $key, $value := .Processors }} - name: {{$key}} + {{- if $value}} config: {{$value}} -{{- end }} \ No newline at end of file + {{- end }} +{{- end }} +{{- end }}