Skip to content

Commit

Permalink
Runs request mode by default if request is not provided as part of ar…
Browse files Browse the repository at this point in the history
…guments
  • Loading branch information
ksysoev committed Oct 24, 2023
1 parent a14f3c0 commit 47c9736
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
5 changes: 4 additions & 1 deletion cmd/wsget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func main() {
defer wsInsp.Close()

client := cli.NewCLI(wsInsp)
StartEditor := true

if *request != "" {
StartEditor = false

go func() {
err = wsInsp.Send(*request)
if err != nil {
Expand All @@ -60,7 +63,7 @@ func main() {
}()
}

err = client.Run(OutputFH)
err = client.Run(cli.RunOptions{OutputFile: OutputFH, StartEditor: StartEditor})
if err != nil {
log.Println("Error:", err)
}
Expand Down
62 changes: 41 additions & 21 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type CLI struct {
editor *Editor
}

type RunOptions struct {
OutputFile *os.File
StartEditor bool
}

func NewCLI(wsConn *ws.Connection) *CLI {
currentUser, err := user.Current()
if err != nil {
Expand All @@ -45,7 +50,7 @@ func NewCLI(wsConn *ws.Connection) *CLI {
}
}

func (c *CLI) Run(outputFile *os.File) error {
func (c *CLI) Run(opts RunOptions) error {
if err := keyboard.Open(); err != nil {
return err
}
Expand All @@ -62,7 +67,13 @@ func (c *CLI) Run(outputFile *os.File) error {
return err
}

fmt.Println("Connection Mode: Press ESC to enter Request mode")
if opts.StartEditor {
if err := c.RequestMod(keysEvents); err != nil {
return err
}
} else {
fmt.Println("Connection Mode: Press ESC to enter Request mode")
}

for {
select {
Expand All @@ -72,25 +83,10 @@ func (c *CLI) Run(outputFile *os.File) error {
return nil

case keyboard.KeyEsc:
fmt.Println("Request Mode: Type your API request and press Ctrl+S to send it. Press ESC to cancel request")

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

fmt.Println(err)
}

if req != "" {
err = c.wsConn.Send(req)
if err != nil {
fmt.Println("Fail to send request:", err)
}
if err := c.RequestMod(keysEvents); err != nil {
return err
}

fmt.Println("Connection Mode: Press ESC to enter Request mode")
default:
continue
}
Expand All @@ -107,14 +103,38 @@ func (c *CLI) Run(outputFile *os.File) error {

fmt.Printf("%s\n\n", output)

if outputFile != nil {
if opts.OutputFile != nil {
output, err := c.formater.FormatForFile(msg)
if err != nil {
log.Printf("Fail to format message for file: %s, %s\n", err, msg.Data)
}

fmt.Fprintln(outputFile, output)
fmt.Fprintln(opts.OutputFile, output)
}
}
}
}

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

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

fmt.Println(err)
}

if req != "" {
err = c.wsConn.Send(req)
if err != nil {
fmt.Println("Fail to send request:", err)
}
}

fmt.Println("Connection Mode: Press ESC to enter Request mode")

return nil
}

0 comments on commit 47c9736

Please sign in to comment.