Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Jan 13, 2022
1 parent cf0f015 commit 55d7873
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func cfwInput() {
fmt.Println("当前cfw版本: " + currentVersion)
fmt.Println()
for k, v := range cfwVersionList {
fmt.Printf("%d. %s\n", k+1, v)
fmt.Printf("%2d. %s\n", k+1, v)
}
for {
choice := ""
Expand Down
26 changes: 17 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,24 @@ func downloadFile(url, downLoadPath string) {
err error
resp *http.Response
bar *pb.ProgressBar
out *os.File
)
closeDownload := func() {
if bar != nil {
bar.Finish()
}
if resp != nil {
resp.Body.Close()
}
if out != nil {
out.Close()
}
}
fmt.Println(fmt.Sprintf("正在下载: %s", url))
for {
if resp, err = http.Get(url); err != nil {
if count > 3 {
if bar != nil {
bar.Finish()
fmt.Println()
}
closeDownload()
exit(err.Error())
} else {
count++
Expand All @@ -90,8 +99,9 @@ func downloadFile(url, downLoadPath string) {
if downLoadPath == "" {
downLoadPath = fullPath(path.Base(url))
}
out, err := os.Create(downLoadPath)
out, err = os.Create(downLoadPath)
if err != nil {
closeDownload()
exit(err.Error())
}

Expand All @@ -100,18 +110,16 @@ func downloadFile(url, downLoadPath string) {
bar.SetTemplateString(`{{counters .}} {{bar .}} {{percent .}} {{speed . "%s/s"}} {{rtime .}}`)
counter := &WriteCounter{bar}
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
closeDownload()
if count > 3 {
exit(err.Error())
} else {
count++
bar.Finish()
fmt.Println("正在重试中(io copy)..")
continue
}
}
resp.Body.Close()
out.Close()
bar.Finish()
closeDownload()
break
}
}
Expand Down

0 comments on commit 55d7873

Please sign in to comment.