Skip to content

Commit f07a891

Browse files
Add ability to exit with "q"
2 parents 59f03b7 + dcbbb9f commit f07a891

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

cmd/receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func receiveCmdFunc(command *cobra.Command, args []string) error {
3636
return err
3737
}
3838
// Prints the URL to scan to screen
39-
log.Print("Scan the following URL with a QR reader to start the file transfer:")
39+
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
4040
log.Print(srv.ReceiveURL)
4141
// Renders the QR
4242
qr.RenderString(srv.ReceiveURL)

cmd/send.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
3838
}
3939
// Sets the payload
4040
srv.Send(payload)
41-
log.Print("Scan the following URL with a QR reader to start the file transfer:")
41+
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
4242
log.Print(srv.SendURL)
4343
qr.RenderString(srv.SendURL)
4444
if browserFlag {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
7+
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
78
github.com/fatih/color v1.9.0 // indirect
89
github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd
910
github.com/jhoonb/archivex v0.0.0-20180718040744-0488e4ce1681

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
2525
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2626
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
2727
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
28+
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ=
29+
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8=
2830
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
2931
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
3032
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

server/server.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"sync"
1919

2020
"github.com/claudiodangelis/qrcp/qr"
21+
"github.com/eiannone/keyboard"
2122

2223
"github.com/claudiodangelis/qrcp/config"
2324
"github.com/claudiodangelis/qrcp/pages"
@@ -89,8 +90,14 @@ func (s Server) Wait() error {
8990
return nil
9091
}
9192

93+
// Shutdown the server
94+
func (s Server) Shutdown() {
95+
s.stopChannel <- true
96+
}
97+
9298
// New instance of the server
9399
func New(cfg *config.Config) (*Server, error) {
100+
94101
app := &Server{}
95102
// Get the address of the configured interface to bind the server to
96103
bind, err := util.GetInterfaceAddress(cfg.Interface)
@@ -156,13 +163,27 @@ func New(cfg *config.Config) (*Server, error) {
156163
app.stopChannel = make(chan bool)
157164
// Create cookie used to verify request is coming from first client to connect
158165
cookie := http.Cookie{Name: "qrcp", Value: ""}
159-
// Gracefully shutdown when an OS signal is received
166+
// Gracefully shutdown when an OS signal is received or when "q" is pressed
160167
sig := make(chan os.Signal, 1)
161168
signal.Notify(sig, os.Interrupt)
162169
go func() {
163170
<-sig
164171
app.stopChannel <- true
165172
}()
173+
if err := keyboard.Open(); err != nil {
174+
panic(err)
175+
}
176+
defer func() {
177+
keyboard.Close()
178+
}()
179+
go func() {
180+
for {
181+
char, _, _ := keyboard.GetKey()
182+
if string(char) == "q" {
183+
app.stopChannel <- true
184+
}
185+
}
186+
}()
166187
// The handler adds and removes from the sync.WaitGroup
167188
// When the group is zero all requests are completed
168189
// and the server is shutdown

0 commit comments

Comments
 (0)