Skip to content

Commit

Permalink
Add message filtering by regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
slytomcat committed Dec 30, 2023
1 parent d73c638 commit dd036c6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/user"
"path/filepath"
"regexp"
"time"

"github.com/chzyer/readline"
Expand All @@ -27,7 +28,9 @@ var (
pingPong bool
compression bool
pingInterval time.Duration
filter *regexp.Regexp
}
filter string
)

func main() {
Expand All @@ -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()
}

Expand All @@ -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 {
Expand Down

0 comments on commit dd036c6

Please sign in to comment.