-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the possibility to translate the msg.Data before output.
- Loading branch information
Showing
4 changed files
with
196 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func filterDataThroughCmd(data []byte, filter string, subject string) ([]byte, error) { | ||
parts, err := parseCommandLine(filter) | ||
if err != nil { | ||
return nil, fmt.Errorf("the filter command line could not be parsed: %w", err) | ||
} | ||
cmd := parts[0] | ||
args := parts[1:] | ||
// maybe we want to replace some strings to give the filter something to reason about? | ||
for idx, arg := range args { | ||
args[idx] = strings.ReplaceAll(arg, "{{subject}}", subject) | ||
} | ||
runner := exec.Command(cmd, args...) | ||
// pass the message as string to stdin | ||
runner.Stdin = bytes.NewReader(data) | ||
// maybe we want to do something on error? | ||
return runner.CombinedOutput() | ||
} | ||
|
||
func parseCommandLine(command string) ([]string, error) { | ||
// copied from https://stackoverflow.com/questions/34118732/parse-a-command-line-string-into-flags-and-arguments-in-golang | ||
// could be optimized but I think it is good enough for this use case | ||
var args []string | ||
state := "start" | ||
current := "" | ||
quote := "\"" | ||
escapeNext := true | ||
for _, c := range command { | ||
|
||
if state == "quotes" { | ||
if string(c) != quote { | ||
current += string(c) | ||
} else { | ||
args = append(args, current) | ||
current = "" | ||
state = "start" | ||
} | ||
continue | ||
} | ||
|
||
if escapeNext { | ||
current += string(c) | ||
escapeNext = false | ||
continue | ||
} | ||
|
||
if c == '\\' { | ||
escapeNext = true | ||
continue | ||
} | ||
|
||
if c == '"' || c == '\'' { | ||
state = "quotes" | ||
quote = string(c) | ||
continue | ||
} | ||
|
||
if state == "arg" { | ||
if c == ' ' || c == '\t' { | ||
args = append(args, current) | ||
current = "" | ||
state = "start" | ||
} else { | ||
current += string(c) | ||
} | ||
continue | ||
} | ||
|
||
if c != ' ' && c != '\t' { | ||
state = "arg" | ||
current += string(c) | ||
} | ||
} | ||
|
||
if state == "quotes" { | ||
return []string{}, errors.New(fmt.Sprintf("Unclosed quote in command line: %s", command)) | ||
} | ||
|
||
if current != "" { | ||
args = append(args, current) | ||
} | ||
|
||
return args, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters