Skip to content

Commit

Permalink
Add ability to exit with "q"
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodangelis authored Apr 19, 2021
2 parents 59f03b7 + dcbbb9f commit f07a891
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func receiveCmdFunc(command *cobra.Command, args []string) error {
return err
}
// Prints the URL to scan to screen
log.Print("Scan the following URL with a QR reader to start the file transfer:")
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
log.Print(srv.ReceiveURL)
// Renders the QR
qr.RenderString(srv.ReceiveURL)
Expand Down
2 changes: 1 addition & 1 deletion cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
}
// Sets the payload
srv.Send(payload)
log.Print("Scan the following URL with a QR reader to start the file transfer:")
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
log.Print(srv.SendURL)
qr.RenderString(srv.SendURL)
if browserFlag {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
github.com/fatih/color v1.9.0 // indirect
github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd
github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
23 changes: 22 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sync"

"github.com/claudiodangelis/qrcp/qr"
"github.com/eiannone/keyboard"

"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/pages"
Expand Down Expand Up @@ -89,8 +90,14 @@ func (s Server) Wait() error {
return nil
}

// Shutdown the server
func (s Server) Shutdown() {
s.stopChannel <- true
}

// New instance of the server
func New(cfg *config.Config) (*Server, error) {

app := &Server{}
// Get the address of the configured interface to bind the server to
bind, err := util.GetInterfaceAddress(cfg.Interface)
Expand Down Expand Up @@ -156,13 +163,27 @@ func New(cfg *config.Config) (*Server, error) {
app.stopChannel = make(chan bool)
// Create cookie used to verify request is coming from first client to connect
cookie := http.Cookie{Name: "qrcp", Value: ""}
// Gracefully shutdown when an OS signal is received
// Gracefully shutdown when an OS signal is received or when "q" is pressed
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
<-sig
app.stopChannel <- true
}()
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, _, _ := keyboard.GetKey()
if string(char) == "q" {
app.stopChannel <- true
}
}
}()
// The handler adds and removes from the sync.WaitGroup
// When the group is zero all requests are completed
// and the server is shutdown
Expand Down

0 comments on commit f07a891

Please sign in to comment.