Skip to content

Commit ca0dcee

Browse files
committed
修复更新下载失败重试
1 parent efa9487 commit ca0dcee

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

cmd/update.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"time"
1010

11+
"github.com/https-cert/deploy/internal/config"
1112
"github.com/https-cert/deploy/internal/updater"
1213
"github.com/spf13/cobra"
1314
)
@@ -21,6 +22,10 @@ func CreateCheckUpdateCmd() *cobra.Command {
2122
RunE: func(cmd *cobra.Command, args []string) error {
2223
ctx := context.Background()
2324

25+
if err := config.Init(ConfigFile); err != nil {
26+
return fmt.Errorf("初始化配置失败: %w", err)
27+
}
28+
2429
fmt.Println("正在检查更新...")
2530
info, err := updater.CheckUpdate(ctx)
2631
if err != nil {
@@ -50,6 +55,10 @@ func CreateUpdateCmd() *cobra.Command {
5055
RunE: func(cmd *cobra.Command, args []string) error {
5156
ctx := context.Background()
5257

58+
if err := config.Init(ConfigFile); err != nil {
59+
return fmt.Errorf("初始化配置失败: %w", err)
60+
}
61+
5362
// 获取当前可执行文件的真实路径
5463
execPath, err := os.Executable()
5564
if err != nil {

internal/updater/updater.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
githubRepo = "https-cert/deploy"
2929
githubAPIURL = "https://api.github.com/repos/" + githubRepo + "/releases/latest"
3030
downloadTimeout = 10 * time.Minute
31+
downloadRetries = 3
3132
)
3233

3334
// 常见的 GitHub 镜像加速服务
@@ -436,6 +437,27 @@ func getBinaryName() string {
436437

437438
// downloadFile 下载文件
438439
func downloadFile(ctx context.Context, downloadURL, filepath string) error {
440+
var lastErr error
441+
442+
for attempt := 1; attempt <= downloadRetries; attempt++ {
443+
if attempt > 1 {
444+
time.Sleep(time.Duration(attempt-1) * time.Second)
445+
os.Remove(filepath)
446+
}
447+
448+
if err := downloadFileOnce(ctx, downloadURL, filepath); err != nil {
449+
lastErr = err
450+
logger.Warn("下载失败,准备重试", "attempt", attempt, "max", downloadRetries, "error", err)
451+
continue
452+
}
453+
454+
return nil
455+
}
456+
457+
return lastErr
458+
}
459+
460+
func downloadFileOnce(ctx context.Context, downloadURL, filepath string) error {
439461
ctx, cancel := context.WithTimeout(ctx, downloadTimeout)
440462
defer cancel()
441463

0 commit comments

Comments
 (0)