mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-02 01:45:52 +02:00
- 在 update 命令中添加 --download-timeout 标志用于设置下载超时时间 - 实现 UpdateViaDownloadWithTimeout 函数支持自定义下载超时 - 添加 updateDownloadTimeoutOrDefault 辅助函数处理超时值验证 - 设置默认下载超时时间为 120 秒 - 添加 updateDownloadTimeoutOrDefault 函数的单元测试 - 验证超时参数必须大于零的错误处理逻辑
72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/multica-ai/multica/server/internal/cli"
|
|
)
|
|
|
|
var updateDownloadTimeout time.Duration = cli.DefaultUpdateDownloadTimeout
|
|
|
|
var updateCmd = &cobra.Command{
|
|
Use: "update",
|
|
Short: "Update multica to the latest version",
|
|
RunE: runUpdate,
|
|
}
|
|
|
|
func init() {
|
|
updateCmd.Flags().DurationVar(&updateDownloadTimeout, "download-timeout", cli.DefaultUpdateDownloadTimeout, "Maximum time to wait for the release archive download")
|
|
}
|
|
|
|
func runUpdate(_ *cobra.Command, _ []string) error {
|
|
if updateDownloadTimeout <= 0 {
|
|
return fmt.Errorf("download timeout must be greater than zero")
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "Current version: %s (commit: %s, built: %s)\n", version, commit, date)
|
|
|
|
// Check latest version from GitHub.
|
|
latest, err := cli.FetchLatestRelease()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: could not check latest version: %v\n", err)
|
|
} else {
|
|
latestVer := strings.TrimPrefix(latest.TagName, "v")
|
|
currentVer := strings.TrimPrefix(version, "v")
|
|
if currentVer == latestVer {
|
|
fmt.Fprintln(os.Stderr, "Already up to date.")
|
|
return nil
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Latest version: %s\n\n", latest.TagName)
|
|
}
|
|
|
|
// Detect installation method and update accordingly.
|
|
if cli.IsBrewInstall() {
|
|
fmt.Fprintln(os.Stderr, "Updating via Homebrew...")
|
|
output, err := cli.UpdateViaBrew()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s\n", output)
|
|
return fmt.Errorf("brew upgrade failed: %w\nYou can try manually: brew upgrade multica-ai/tap/multica", err)
|
|
}
|
|
fmt.Fprintln(os.Stderr, "Update complete.")
|
|
return nil
|
|
}
|
|
|
|
// Not installed via brew — download binary directly from GitHub Releases.
|
|
if latest == nil {
|
|
return fmt.Errorf("could not determine latest version; check https://github.com/multica-ai/multica/releases/latest")
|
|
}
|
|
targetVersion := latest.TagName
|
|
fmt.Fprintf(os.Stderr, "Downloading %s from GitHub Releases...\n", targetVersion)
|
|
output, err := cli.UpdateViaDownloadWithTimeout(targetVersion, updateDownloadTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("update failed: %w", err)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "%s\nUpdate complete.\n", output)
|
|
return nil
|
|
}
|