Skip to content

Commit

Permalink
Merge branch 'main' into command_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 29, 2023
2 parents 15b53c9 + c6ffb0d commit a939033
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 deletions.
93 changes: 62 additions & 31 deletions cmd/wsget/main.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/ksysoev/wsget/pkg/cli"
"github.com/ksysoev/wsget/pkg/ws"
"github.com/spf13/cobra"
)

var wsURL string
var insecure *bool
var request *string
var OutputFH *os.File
var insecure bool
var request string
var outputFile string

func init() {
url := flag.String("u", "", "Websocket url that will be used for connection. this argument is required")
outputFile := flag.String("o", "", "Output file for saving requests and responses")
showHelp := flag.Bool("h", false, "Prints this help message")
insecure = flag.Bool("insecure", false, "Skip SSL certificate verification")
request = flag.String("r", "", "Request that will be sent to the server")
const (
LongDescription = `A command-line tool for interacting with WebSocket servers.
flag.Parse()
The tool have severl modes of operation:
if (showHelp != nil && *showHelp) || (url == nil || *url == "") {
flag.Usage()
os.Exit(0)
}
1. Request mode. The tool will start in interactive mode if no request is provided:
wsURL = *url
- You can type resquest and press Ctrl+S to send it to the server.
- It supports multiline input.
- You can use Ctrl+U to clear the input.
- You can use Ctrl+C or Ctrl+D to exit the tool.
- You can use Esc to exit Request mode and switch to connection mode.
if outputFile != nil && *outputFile != "" {
var err error
2. Connection mode. The tool will start in connection mode if request is provided.
In this request mode the tool will send the request to the server and print responses.
OutputFH, err = os.Create(*outputFile)
if err != nil {
log.Fatal(err)
}
- You can use Ctrl+C or Ctrl+D to exit the tool.
- You can use Esc to switch to Request mode.
`
)

func main() {
cmd := &cobra.Command{
Use: "wsget url [flags]",
Short: "A command-line tool for interacting with WebSocket servers",
Long: LongDescription,
Example: `wsget wss://ws.postman-echo.com/raw -r "Hello, world!"`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"url"},
Version: "0.1.4",
Run: run,
}

cmd.Flags().BoolVarP(&insecure, "insecure", "k", false, "Skip SSL certificate verification")
cmd.Flags().StringVarP(&request, "request", "r", "", "WebSocket request that will be sent to the server")
cmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file for saving all request and responses")

err := cmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func main() {
wsInsp, err := ws.NewWS(wsURL, ws.Options{SkipSSLVerification: *insecure})
func run(cmd *cobra.Command, args []string) {
wsURL := args[0]
if wsURL == "" {
_ = cmd.Help()

os.Exit(1)
}

wsInsp, err := ws.NewWS(wsURL, ws.Options{SkipSSLVerification: insecure})
if err != nil {
log.Fatal(err)
}
Expand All @@ -52,21 +76,28 @@ func main() {
input := cli.NewKeyboard()

client := cli.NewCLI(wsInsp, input, os.Stdout)
StartEditor := true

if *request != "" {
StartEditor = false
opts := cli.RunOptions{StartEditor: true}

if request != "" {
opts.StartEditor = false

go func() {
err = wsInsp.Send(*request)
err = wsInsp.Send(request)
if err != nil {
fmt.Println("Fail to send request:", err)
}
}()
}

err = client.Run(cli.RunOptions{OutputFile: OutputFH, StartEditor: StartEditor})
if err != nil {
if outputFile != "" {
if opts.OutputFile, err = os.Create(outputFile); err != nil {
log.Println(err)
return
}
}

if err = client.Run(opts); err != nil {
log.Println("Error:", err)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ require (

require (
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.13.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 13 additions & 9 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ func (c *CLI) Run(opts RunOptions) error {
}
defer c.input.Close()

fmt.Fprintln(c.output, "Use Esc to switch between modes, Ctrl+C to exit")

if opts.StartEditor {
if err := c.RequestMod(keysEvents); err != nil {
if err.Error() == "interrupted" {
return nil
}

return err
}
} else {
fmt.Fprintln(c.output, "Connection Mode: Press ESC to enter Request mode")
}

for {
Expand All @@ -89,6 +93,10 @@ func (c *CLI) Run(opts RunOptions) error {

case keyboard.KeyEsc:
if err := c.RequestMod(keysEvents); err != nil {
if err.Error() == "interrupted" {
return nil
}

return err
}

Expand Down Expand Up @@ -142,15 +150,11 @@ func (c *CLI) Run(opts RunOptions) error {
}

func (c *CLI) RequestMod(keysEvents <-chan keyboard.KeyEvent) error {
fmt.Fprintln(c.output, "Request Mode: Type your API request and press Ctrl+S to send it. Press ESC to cancel request")
fmt.Fprintln(c.output, "Ctrl+S to send>")

req, err := c.editor.EditRequest(keysEvents, "")
if err != nil {
if err.Error() == "interrupted" {
return nil
}

fmt.Fprintln(c.output, err)
return err
}

if req != "" {
Expand All @@ -160,7 +164,7 @@ func (c *CLI) RequestMod(keysEvents <-chan keyboard.KeyEvent) error {
}
}

fmt.Fprintln(c.output, "Connection Mode: Press ESC to enter Request mode")
fmt.Fprint(c.output, LineUp+LineClear)

return nil
}

0 comments on commit a939033

Please sign in to comment.