Skip to content

Commit

Permalink
Added a universal msg output function.
Browse files Browse the repository at this point in the history
  • Loading branch information
oderwat committed Mar 24, 2023
1 parent 14e7089 commit c297fb3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
19 changes: 18 additions & 1 deletion cli/filter_data_through_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ import (
"text/template"
)

func filterDataThroughCmd(data []byte, filter string, subject, stream string) ([]byte, error) {
func outPutMSGBody(data []byte, filter, subject, stream string) {
if len(data) == 0 {
fmt.Println("nil body")
}

data, err := filterDataThroughCmd(data, filter, subject, stream)
if err != nil {
fmt.Printf("%s\nError while translating msg body: %s\n\n", data, err.Error())
return
}
output := string(data)
fmt.Println(output)
if !strings.HasSuffix(output, "\n") {
fmt.Println()
}
}

func filterDataThroughCmd(data []byte, filter, subject, stream string) ([]byte, error) {
if filter == "" {
return data, nil
}
Expand Down
24 changes: 2 additions & 22 deletions cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,20 +703,7 @@ func (c *streamCmd) viewAction(_ *fisk.ParseContext) error {
}

fmt.Println()
if len(msg.Data) == 0 {
fmt.Println("nil body")
} else {
data, err := filterDataThroughCmd(msg.Data, c.vwTranslate, msg.Subject, meta.Stream())
if err != nil {
fmt.Printf("%s\nError while translating msg body: %s\n\n", data, err.Error())
} else {
fmt.Println(string(data))
if !strings.HasSuffix(string(data), "\n") {
fmt.Println()
}
}
}

outPutMSGBody(msg.Data, c.vwTranslate, msg.Subject, meta.Stream())
}

if last {
Expand Down Expand Up @@ -2711,14 +2698,7 @@ func (c *streamCmd) getAction(_ *fisk.ParseContext) (err error) {
}
fmt.Println()
}

data, err := filterDataThroughCmd(item.Data, c.vwTranslate, item.Subject, c.stream)
if err != nil {
fmt.Printf("%s\nError while translating msg body: %s\n\n", data, err.Error())
} else {
fmt.Println(string(data))
fmt.Println()
}
outPutMSGBody(item.Data, c.vwTranslate, item.Subject, c.stream)
return nil
}

Expand Down
11 changes: 1 addition & 10 deletions cli/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,6 @@ func prettyPrintMsg(msg *nats.Msg, headersOnly bool, filter string) {
}

if !headersOnly {
data, err := filterDataThroughCmd(msg.Data, filter, msg.Subject, "")
if err != nil {
fmt.Printf("%s\nError while translating msg body: %s\n\n", data, err.Error())
return
}
output := string(data)
fmt.Println(output)
if !strings.HasSuffix(output, "\n") {
fmt.Println()
}
outPutMSGBody(msg.Data, filter, msg.Subject, "")
}
}

0 comments on commit c297fb3

Please sign in to comment.