From 24f3334147226092aaba29accd058d90ece875cf Mon Sep 17 00:00:00 2001 From: zhifengle Date: Thu, 20 Apr 2023 17:47:05 +0800 Subject: [PATCH 1/4] refactor: Download func --- utils/web/web.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/utils/web/web.go b/utils/web/web.go index 672bc59..343b50a 100644 --- a/utils/web/web.go +++ b/utils/web/web.go @@ -3,7 +3,6 @@ package web import ( "errors" "fmt" - pb "gopkg.in/cheggaaa/pb.v1" "io" "io/ioutil" "net/http" @@ -12,6 +11,8 @@ import ( "path/filepath" "strconv" "time" + + pb "gopkg.in/cheggaaa/pb.v1" ) var client = &http.Client{} @@ -26,19 +27,23 @@ func SetProxy(p string) { } func Download(url string, target string) bool { - output, err := os.Create(target) + response, err := client.Get(url) if err != nil { - fmt.Println("Error while creating", target, "-", err) + fmt.Println("Error while downloading", url, "-", err) return false } - defer output.Close() + if response.StatusCode != 200 { + fmt.Println("Error status while downloading", url, "-", response.StatusCode) + return false + } + defer response.Body.Close() - response, err := client.Get(url) + output, err := os.Create(target) if err != nil { - fmt.Println("Error while downloading", url, "-", err) + fmt.Println("Error while creating", target, "-", err) return false } - defer response.Body.Close() + defer output.Close() // 获取下载文件的大小 i, _ := strconv.Atoi(response.Header.Get("Content-Length")) @@ -64,14 +69,6 @@ func Download(url string, target string) bool { return false } bar.Finish() - if response.Status[0:3] != "200" { - fmt.Println("Download failed. Rolling Back.") - err := os.Remove(target) - if err != nil { - fmt.Println("Rollback failed.", err) - } - return false - } return true } From 096612c3eeeb890f2b701fe6ac4f39d737af527f Mon Sep 17 00:00:00 2001 From: zhifengle Date: Thu, 20 Apr 2023 17:55:55 +0800 Subject: [PATCH 2/4] update: content length --- utils/web/web.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/utils/web/web.go b/utils/web/web.go index 343b50a..507c475 100644 --- a/utils/web/web.go +++ b/utils/web/web.go @@ -9,7 +9,6 @@ import ( "net/url" "os" "path/filepath" - "strconv" "time" pb "gopkg.in/cheggaaa/pb.v1" @@ -45,11 +44,8 @@ func Download(url string, target string) bool { } defer output.Close() - // 获取下载文件的大小 - i, _ := strconv.Atoi(response.Header.Get("Content-Length")) - sourceSiz := int64(i) // 创建一个进度条 - bar := pb.New(int(sourceSiz)).SetUnits(pb.U_BYTES_DEC).SetRefreshRate(time.Millisecond * 10) + bar := pb.New(int(response.ContentLength)).SetUnits(pb.U_BYTES_DEC).SetRefreshRate(time.Millisecond * 10) // 显示下载速度 bar.ShowSpeed = true From b97d18433dfd69fa5440b8736f787a39e3f61561 Mon Sep 17 00:00:00 2001 From: zhifengle Date: Fri, 21 Apr 2023 09:32:45 +0800 Subject: [PATCH 3/4] update: set proxy on startup --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index c180c30..c2ced49 100644 --- a/main.go +++ b/main.go @@ -357,6 +357,9 @@ func startup(c *cli.Context) error { if config.Originalpath == "" { config.Originalpath = defaultOriginalpath } + if config.Proxy != "" { + web.SetProxy(config.Proxy) + } return nil } From 279e2af21e09ea2674acb568c263b9fbc6d41500 Mon Sep 17 00:00:00 2001 From: zhifengle Date: Fri, 21 Apr 2023 09:34:28 +0800 Subject: [PATCH 4/4] update: set proxy on install --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index c2ced49..e880f10 100644 --- a/main.go +++ b/main.go @@ -127,6 +127,9 @@ func commands() []cli.Command { ShortName: "i", Usage: "Install available remote jdk", Action: func(c *cli.Context) error { + if config.Proxy != "" { + web.SetProxy(config.Proxy) + } v := c.Args().Get(0) if v == "" { return errors.New("invalid version., Type \"jvms rls\" to see what is available for install")