Skip to content

Commit

Permalink
Merge pull request #29 from zhifengle/new
Browse files Browse the repository at this point in the history
Remove useless code from Download func
  • Loading branch information
ystyle authored Apr 24, 2023
2 parents 492a4ef + 279e2af commit 1335164
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -357,6 +360,9 @@ func startup(c *cli.Context) error {
if config.Originalpath == "" {
config.Originalpath = defaultOriginalpath
}
if config.Proxy != "" {
web.SetProxy(config.Proxy)
}
return nil
}

Expand Down
33 changes: 13 additions & 20 deletions utils/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package web
import (
"errors"
"fmt"
pb "gopkg.in/cheggaaa/pb.v1"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"time"

pb "gopkg.in/cheggaaa/pb.v1"
)

var client = &http.Client{}
Expand All @@ -26,25 +26,26 @@ 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"))
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

Expand All @@ -64,14 +65,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
}
Expand Down

0 comments on commit 1335164

Please sign in to comment.