Skip to content

Commit

Permalink
Made the template work in the translation command before splitting it.
Browse files Browse the repository at this point in the history
This way you can use the template language to create the actual command
based on the data.
  • Loading branch information
oderwat committed Mar 24, 2023
1 parent b625e30 commit 14e7089
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions cli/filter_data_through_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,27 @@ func filterDataThroughCmd(data []byte, filter string, subject, stream string) ([
if filter == "" {
return data, nil
}
parts, err := shlex.Split(filter)
if err != nil {
return nil, fmt.Errorf("the filter command line could not be parsed: %w", err)
}
cmd := parts[0]
args := parts[1:]

funcMap := template.FuncMap{
"Subject": func() string { return subject },
"Stream": func() string { return stream },
}

for idx, arg := range args {
tmpl, err := template.New("translate").Funcs(funcMap).Parse(arg)
if err != nil {
return nil, err
}
var builder strings.Builder
err = tmpl.Execute(&builder, nil)
if err != nil {
return nil, err
}
tmpl, err := template.New("translate").Funcs(funcMap).Parse(filter)
if err != nil {
return nil, err
}
var builder strings.Builder
err = tmpl.Execute(&builder, nil)
if err != nil {
return nil, err
}

args[idx] = builder.String()
parts, err := shlex.Split(builder.String())
if err != nil {
return nil, fmt.Errorf("the filter command line could not be parsed: %w", err)
}
cmd := parts[0]
args := parts[1:]

runner := exec.Command(cmd, args...)
// pass the message as string to stdin
Expand Down

0 comments on commit 14e7089

Please sign in to comment.