Skip to content

Commit

Permalink
Change to shlex, added dependency and 'stream' template
Browse files Browse the repository at this point in the history
  • Loading branch information
oderwat committed Mar 19, 2023
1 parent 41cc80b commit 6a30167
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 74 deletions.
78 changes: 7 additions & 71 deletions cli/filter_data_through_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,28 @@ package cli

import (
"bytes"
"errors"
"fmt"
"github.com/google/shlex"
"os/exec"
"strings"
)

func filterDataThroughCmd(data []byte, filter string, subject string) ([]byte, error) {
parts, err := parseCommandLine(filter)
func filterDataThroughCmd(data []byte, filter string, subject, stream string) ([]byte, error) {
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:]
// 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)
for idx := range args {
arg := &args[idx]
*arg = strings.ReplaceAll(*arg, "{{subject}}", subject)
*arg = strings.ReplaceAll(*arg, "{{stream}}", stream)
}
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
}
4 changes: 2 additions & 2 deletions cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (c *streamCmd) viewAction(_ *fisk.ParseContext) error {
data := msg.Data
var errFilter error
if c.vwTranslate != "" {
data, errFilter = filterDataThroughCmd(data, c.vwTranslate, msg.Subject)
data, errFilter = filterDataThroughCmd(data, c.vwTranslate, msg.Subject, meta.Stream())
}
if errFilter != nil {
fmt.Printf("%s\nError: %s\n\n", data, errFilter.Error())
Expand Down Expand Up @@ -2719,7 +2719,7 @@ func (c *streamCmd) getAction(_ *fisk.ParseContext) (err error) {
data := item.Data
var errFilter error
if c.vwTranslate != "" {
data, errFilter = filterDataThroughCmd(data, c.vwTranslate, item.Subject)
data, errFilter = filterDataThroughCmd(data, c.vwTranslate, item.Subject, c.stream)
}
if errFilter != nil {
fmt.Printf("%s\nError: %s\n\n", data, errFilter.Error())
Expand Down
2 changes: 1 addition & 1 deletion cli/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func prettyPrintMsg(msg *nats.Msg, headersOnly bool, filter string) {
data := msg.Data
if filter != "" {
var err error
data, err = filterDataThroughCmd(msg.Data, filter, msg.Subject)
data, err = filterDataThroughCmd(msg.Data, filter, msg.Subject, "")
if err != nil {
fmt.Printf("%s\nError: %s\n\n", data, err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This file lists the dependencies used in this repository.
| github.com/nats-io/nats.go | Apache License 2.0 |
| github.com/nats-io/nkeys | Apache License 2.0 |
| github.com/nats-io/nuid | Apache License 2.0 |
| github.com/google/shlex | Apache License 2.0 |
| github.com/tylertreat/hdrhistogram-writer | Apache License 2.0 |
| github.com/xeipuuv/gojsonpointer | Apache License 2.0 |
| github.com/xeipuuv/gojsonreference | Apache License 2.0 |
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gosuri/uilive v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY=
github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI=
github.com/gosuri/uiprogress v0.0.1 h1:0kpv/XY/qTmFWl/SkaJykZXrBBzwwadmW8fRb7RJSxw=
Expand Down

0 comments on commit 6a30167

Please sign in to comment.