Skip to content

Commit

Permalink
Prevent from panicking in non-TTY environments
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodangelis committed Apr 22, 2021
1 parent 32f5bce commit d2666a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
29 changes: 16 additions & 13 deletions cmd/receive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/logger"
"github.com/claudiodangelis/qrcp/qr"
Expand Down Expand Up @@ -45,20 +47,21 @@ func receiveCmdFunc(command *cobra.Command, args []string) error {
if browserFlag {
srv.DisplayQR(srv.ReceiveURL)
}
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, key, _ := keyboard.GetKey()
if string(char) == "q" || key == keyboard.KeyCtrlC {
srv.Shutdown()
if err := keyboard.Open(); err == nil {
defer func() {
keyboard.Close()
}()
go func() {
for {
char, key, _ := keyboard.GetKey()
if string(char) == "q" || key == keyboard.KeyCtrlC {
srv.Shutdown()
}
}
}
}()
}()
} else {
log.Print(fmt.Sprintf("Warning: keyboard not detected: %v", err))
}
if err := srv.Wait(); err != nil {
return err
}
Expand Down
29 changes: 16 additions & 13 deletions cmd/send.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/logger"
"github.com/claudiodangelis/qrcp/payload"
Expand Down Expand Up @@ -45,20 +47,21 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
if browserFlag {
srv.DisplayQR(srv.SendURL)
}
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, key, _ := keyboard.GetKey()
if string(char) == "q" || key == keyboard.KeyCtrlC {
srv.Shutdown()
if err := keyboard.Open(); err == nil {
defer func() {
keyboard.Close()
}()
go func() {
for {
char, key, _ := keyboard.GetKey()
if string(char) == "q" || key == keyboard.KeyCtrlC {
srv.Shutdown()
}
}
}
}()
}()
} else {
log.Print(fmt.Sprintf("Warning: keyboard not detected: %v", err))
}
if err := srv.Wait(); err != nil {
return err
}
Expand Down

0 comments on commit d2666a3

Please sign in to comment.