Skip to content

Commit

Permalink
Cleanup after system signals
Browse files Browse the repository at this point in the history
  • Loading branch information
mgomes committed Mar 26, 2022
1 parent 4eb4c59 commit 4555ea5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"mime"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"

"github.com/schollz/progressbar/v3"
)
Expand Down Expand Up @@ -58,6 +60,19 @@ func main() {
}
}

sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sigc
fmt.Printf("\n%s; cleaning up...\n", sig)
dl.cleanupParts()
os.Exit(0)
}()

fmt.Println(dl.filename)

dl.Fetch()
Expand Down Expand Up @@ -152,6 +167,8 @@ func (dl *download) filenameFromURI() string {
func (dl *download) ConcatFiles() {
var readers []io.Reader

defer dl.cleanupParts()

bar := progressbar.DefaultBytes(
int64(dl.filesize),
"Combining ",
Expand All @@ -162,7 +179,6 @@ func (dl *download) ConcatFiles() {
if err != nil {
panic(err)
}
defer os.Remove(part.downloadPartFilename())
defer downloadPart.Close()
readers = append(readers, downloadPart)
}
Expand All @@ -179,3 +195,9 @@ func (dl *download) ConcatFiles() {
panic(err)
}
}

func (dl *download) cleanupParts() {
for _, part := range dl.parts {
os.Remove(part.downloadPartFilename())
}
}

0 comments on commit 4555ea5

Please sign in to comment.