提交 8889f643 编写于 作者: C ccccly

Add cleanup when download is interrupted by user.

上级 c75f0880
...@@ -5,10 +5,12 @@ import( ...@@ -5,10 +5,12 @@ import(
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"io" "io"
"io/ioutil" "io/ioutil"
"strings"
"syscall"
"crypto/tls" "crypto/tls"
"strings"
"strconv" "strconv"
"../arch" "../arch"
"../file" "../file"
...@@ -50,7 +52,7 @@ func GetFullNpmUrl(path string) string{ ...@@ -50,7 +52,7 @@ func GetFullNpmUrl(path string) string{
return npmBaseAddress + path; return npmBaseAddress + path;
} }
func Download(url string, target string) bool { func Download(url string, target string, version string) bool {
output, err := os.Create(target) output, err := os.Create(target)
if err != nil { if err != nil {
...@@ -63,12 +65,28 @@ func Download(url string, target string) bool { ...@@ -63,12 +65,28 @@ func Download(url string, target string) bool {
fmt.Println("Error while downloading", url, "-", err) fmt.Println("Error while downloading", url, "-", err)
} }
defer response.Body.Close() defer response.Body.Close()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Download interrupted.Rolling back...")
output.Close()
response.Body.Close()
var err error
if strings.Contains(target, "node") {
err = os.RemoveAll(os.Getenv("NVM_HOME") + "\\v" + version)
} else {
err = os.Remove(target)
}
if err != nil {
fmt.Println("Error while rolling back", err)
}
os.Exit(1)
}()
_, err = io.Copy(output, response.Body) _, err = io.Copy(output, response.Body)
if err != nil { if err != nil {
fmt.Println("Error while downloading", url, "-", err) fmt.Println("Error while downloading", url, "-", err)
} }
if response.Status[0:3] != "200" { if response.Status[0:3] != "200" {
fmt.Println("Download failed. Rolling Back.") fmt.Println("Download failed. Rolling Back.")
err := os.Remove(target) err := os.Remove(target)
...@@ -111,9 +129,9 @@ func GetNodeJS(root string, v string, a string) bool { ...@@ -111,9 +129,9 @@ func GetNodeJS(root string, v string, a string) bool {
} else { } else {
fileName := root+"\\v"+v+"\\node"+a+".exe" fileName := root+"\\v"+v+"\\node"+a+".exe"
fmt.Printf("Downloading node.js version "+v+" ("+a+"-bit)... ") fmt.Println("Downloading node.js version "+v+" ("+a+"-bit)... ")
if Download(url,fileName) { if Download(url,fileName,v) {
fmt.Printf("Complete\n") fmt.Printf("Complete\n")
return true return true
} else { } else {
...@@ -142,7 +160,7 @@ func GetNpm(root string, v string) bool { ...@@ -142,7 +160,7 @@ func GetNpm(root string, v string) bool {
fileName := tempDir+"\\"+"npm-v"+v+".zip" fileName := tempDir+"\\"+"npm-v"+v+".zip"
fmt.Printf("Downloading npm version "+v+"... ") fmt.Printf("Downloading npm version "+v+"... ")
if Download(url,fileName) { if Download(url,fileName,v) {
fmt.Printf("Complete\n") fmt.Printf("Complete\n")
return true return true
} else { } else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册