Skip to content

Commit

Permalink
Put the interrupt handler into init instead of the middle of main fun…
Browse files Browse the repository at this point in the history
…ction

for issue #23
  • Loading branch information
NHOrus committed Apr 14, 2016
1 parent 9b4223e commit 2c7ec94
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,23 @@ import (
var (
prefix = "https:"
stopParsing bool
interruptSig chan os.Signal
)

func init() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
interruptSig := make(chan os.Signal, 1)

go func() {
for {
<-sig
stopParsing = true
interruptSig <- os.Interrupt
}
}()
}

func main() {
fmt.Println("Derpibooru.org Downloader version 0.8.0")

Expand Down Expand Up @@ -68,26 +83,13 @@ func main() {
filterInit(opts.FiltOpts, bool(opts.Config.LogFilters)) //Initiating filters based on our given flags
filtimgdat := FilterChannel(imgdat) //Actual filtration

sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)

osig := make(chan os.Signal, 1)
go setStopParsing(sig, osig)

filtimgdat.dispatch(osig).downloadImages(opts.Config) // Now that we got asynchronous list of images we want to get done, we can get them.
filtimgdat.dispatch(interruptSig).downloadImages(opts.Config) // Now that we got asynchronous list of images we want to get done, we can get them.

lDone("Finished")
//And we are done here! Hooray!
}

func setStopParsing(sig <-chan os.Signal, osig chan<- os.Signal) {
for {
<-sig
stopParsing = true
osig <- os.Interrupt
}
}

func (imgchan ImageCh) dispatch(sig <-chan os.Signal) (outch ImageCh) {
outch = make(ImageCh)
go imgchan.dispatcher(sig, outch)
Expand Down

0 comments on commit 2c7ec94

Please sign in to comment.