From 90ba48342e498d47599102abde0de6c641e82c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Guimar=C3=A3es?= Date: Mon, 23 Jan 2017 08:55:59 -0200 Subject: [PATCH 1/2] Sync send / receive when piping --- main.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 05dcfdf..44456a7 100644 --- a/main.go +++ b/main.go @@ -21,12 +21,14 @@ var ( protocol string displayHelp bool displayVersion bool + isPipe bool red = color.New(color.FgRed).SprintFunc() magenta = color.New(color.FgMagenta).SprintFunc() green = color.New(color.FgGreen).SprintFunc() yellow = color.New(color.FgYellow).SprintFunc() cyan = color.New(color.FgCyan).SprintFunc() wg sync.WaitGroup + wgReceive sync.WaitGroup ) func init() { @@ -62,6 +64,7 @@ func printErrors(errors <-chan error) { os.Exit(0) } else { fmt.Printf("\rerr %v\n> ", red(err)) + doneReceive() } } } @@ -69,6 +72,7 @@ func printErrors(errors <-chan error) { func printReceivedMessages(in <-chan []byte) { for msg := range in { fmt.Printf("\r< %s\n> ", cyan(string(msg))) + doneReceive() } } @@ -81,6 +85,12 @@ func outLoop(ws *websocket.Conn, out <-chan []byte, errors chan<- error) { } } +func doneReceive() { + if isPipe { + wgReceive.Done() + } +} + func main() { flag.Parse() @@ -126,11 +136,26 @@ func main() { go printErrors(errors) go outLoop(ws, out, errors) + fi, err := os.Stdin.Stat() + if err != nil { + panic(err) + } + isPipe = fi.Mode()&os.ModeNamedPipe != 0 + scanner := bufio.NewScanner(os.Stdin) fmt.Print("> ") for scanner.Scan() { - out <- []byte(scanner.Text()) + text := scanner.Text() + if isPipe { + fmt.Println(green(text)) + wgReceive.Add(1) + } + out <- []byte(text) + if isPipe { + wgReceive.Wait() + continue + } fmt.Print("> ") } From 426e27250b915a76d8b7647bbd368494d2c4e93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Guimar=C3=A3es?= Date: Mon, 23 Jan 2017 09:18:00 -0200 Subject: [PATCH 2/2] - Ignoring empty lines if piping - Dont wait channels if piping --- README.md | 21 ++++++++++++++++++++- main.go | 14 +++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd53472..998975e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,26 @@ Usage of ./wsd: -url string WebSocket server address to connect to (default "ws://localhost:1337/ws") -version - Display version number``` + Display version number +``` + +### Piping + +One can piping multiples messages by breaking them with new lines +E.g. +``` +echo 'a +b' \ +| wsd -url="wss://echo.websocket.org" +``` +results +``` +> a +< a +> b +< b +``` + ## Why? diff --git a/main.go b/main.go index 86a06fd..26c2686 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "os" "sync" + "strings" + "github.com/fatih/color" "golang.org/x/net/websocket" ) @@ -23,14 +25,14 @@ var ( displayHelp bool displayVersion bool insecureSkipVerify bool - isPipe bool + isPipe bool red = color.New(color.FgRed).SprintFunc() magenta = color.New(color.FgMagenta).SprintFunc() green = color.New(color.FgGreen).SprintFunc() yellow = color.New(color.FgYellow).SprintFunc() cyan = color.New(color.FgCyan).SprintFunc() wg sync.WaitGroup - wgReceive sync.WaitGroup + wgReceive sync.WaitGroup ) func init() { @@ -165,6 +167,10 @@ func main() { for scanner.Scan() { text := scanner.Text() if isPipe { + // Ignoring empty lines + if len(strings.Trim(text, "")) == 0 { + continue + } fmt.Println(green(text)) wgReceive.Add(1) } @@ -176,5 +182,7 @@ func main() { fmt.Print("> ") } - wg.Wait() + if !isPipe { + wg.Wait() + } }