From dd036c6273a1caadf968f624517cc6a00911e575 Mon Sep 17 00:00:00 2001 From: Sly_tom_cat Date: Sat, 30 Dec 2023 20:51:58 +0300 Subject: [PATCH] Add message filtering by regexp --- README.md | 1 + connection.go | 3 +++ main.go | 12 +++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d9d6c55..19f34a8 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Flags: -a, --auth string auth header value, like 'Bearer $TOKEN' -b, --bin2text print binary message as text -c, --compression enable compression + -f, --filter string only messages that match regexp will be printed -h, --help help for ws -m, --init string connection init message -k, --insecure skip ssl certificate check diff --git a/connection.go b/connection.go index 9fd648f..a47b23c 100644 --- a/connection.go +++ b/connection.go @@ -194,6 +194,9 @@ func (s *Session) readWebsocket() { s.setErr(fmt.Errorf("unknown websocket frame type: %d", msgType)) return } + if options.filter != nil && !options.filter.MatchString(text) { + continue + } fmt.Fprint(s.rl.Stdout(), rxSprintf("%s< %s\n", getPrefix(), text)) } } diff --git a/main.go b/main.go index b2e2f89..5c4ae22 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "os/user" "path/filepath" + "regexp" "time" "github.com/chzyer/readline" @@ -27,7 +28,9 @@ var ( pingPong bool compression bool pingInterval time.Duration + filter *regexp.Regexp } + filter string ) func main() { @@ -47,6 +50,7 @@ func main() { rootCmd.Flags().DurationVarP(&options.pingInterval, "interval", "i", 0, "send ping each interval (ex: 20s)") rootCmd.Flags().StringVarP(&options.initMsg, "init", "m", "", "connection init message") rootCmd.Flags().BoolVarP(&options.compression, "compression", "c", false, "enable compression") + rootCmd.Flags().StringVarP(&filter, "filter", "f", "", "only messages that match regexp will be printed") rootCmd.Execute() } @@ -73,7 +77,13 @@ func root(cmd *cobra.Command, args []string) { } options.origin = originURL.String() } - + if len(filter) > 0 { + options.filter, err = regexp.Compile(filter) + if err != nil { + fmt.Fprintf(os.Stderr, "compiling regexp '%s' error: %v", filter, err) + os.Exit(1) + } + } var historyFile string user, err := user.Current() if err == nil {