Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(cli): 添加更新下载超时配置选项 (#1622)

Joeyd17b2bfb8c1b
diff --git a/server/cmd/multica/cmd_update.go b/server/cmd/multica/cmd_update.go
index c79d29bc63..4b8c7e6f0c 100644
--- a/server/cmd/multica/cmd_update.go
+++ b/server/cmd/multica/cmd_update.go
@@ -4,19 +4,30 @@ 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.
@@ -51,7 +62,7 @@ func runUpdate(_ *cobra.Command, _ []string) error {
}
targetVersion := latest.TagName
fmt.Fprintf(os.Stderr, "Downloading %s from GitHub Releases...\n", targetVersion)
- output, err := cli.UpdateViaDownload(targetVersion)
+ output, err := cli.UpdateViaDownloadWithTimeout(targetVersion, updateDownloadTimeout)
if err != nil {
return fmt.Errorf("update failed: %w", err)
}
diff --git a/server/internal/cli/update.go b/server/internal/cli/update.go
index e51755e9f0..f468e5aa01 100644
--- a/server/internal/cli/update.go
+++ b/server/internal/cli/update.go
@@ -17,6 +17,8 @@ import (
"time"
)
+const DefaultUpdateDownloadTimeout = 120 * time.Second
+
// GitHubRelease is the subset of the GitHub releases API response we need.
type GitHubRelease struct {
TagName string `json:"tag_name"`
@@ -164,9 +166,21 @@ func UpdateViaBrew() (string, error) {
return string(out), nil
}
+func updateDownloadTimeoutOrDefault(timeout time.Duration) time.Duration {
+ if timeout <= 0 {
+ return DefaultUpdateDownloadTimeout
+ }
+ return timeout
+}
+
// UpdateViaDownload downloads the latest release binary from GitHub and replaces
// the current executable in-place. Returns the combined output message and any error.
func UpdateViaDownload(targetVersion string) (string, error) {
+ return UpdateViaDownloadWithTimeout(targetVersion, DefaultUpdateDownloadTimeout)
+}
+
+// UpdateViaDownloadWithTimeout downloads the latest release binary with a caller-selected timeout.
+func UpdateViaDownloadWithTimeout(targetVersion string, downloadTimeout time.Duration) (string, error) {
// Determine current binary path.
exePath, err := os.Executable()
if err != nil {
@@ -190,7 +204,7 @@ func UpdateViaDownload(targetVersion string) (string, error) {
assetName := asset.Name
// Download the archive.
- client := &http.Client{Timeout: 120 * time.Second}
+ client := &http.Client{Timeout: updateDownloadTimeoutOrDefault(downloadTimeout)}
resp, err := client.Get(downloadURL)
if err != nil {
return "", fmt.Errorf("download failed: %w", err)
diff --git a/server/internal/cli/update_test.go b/server/internal/cli/update_test.go
index 237cb1a55a..f2f8e05881 100644
--- a/server/internal/cli/update_test.go
+++ b/server/internal/cli/update_test.go
@@ -1,6 +1,9 @@
package cli
-import "testing"
+import (
+ "testing"
+ "time"
+)
func TestReleaseAssetCandidates(t *testing.T) {
tests := []struct {
@@ -94,3 +97,36 @@ func TestFindReleaseAsset(t *testing.T) {
}
})
}
+
+func TestUpdateDownloadTimeoutOrDefault(t *testing.T) {
+ tests := []struct {
+ name string
+ timeout time.Duration
+ want time.Duration
+ }{
+ {
+ name: "uses default for zero",
+ timeout: 0,
+ want: DefaultUpdateDownloadTimeout,
+ },
+ {
+ name: "uses default for negative",
+ timeout: -1 * time.Second,
+ want: DefaultUpdateDownloadTimeout,
+ },
+ {
+ name: "keeps explicit timeout",
+ timeout: 10 * time.Minute,
+ want: 10 * time.Minute,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := updateDownloadTimeoutOrDefault(tt.timeout)
+ if got != tt.want {
+ t.Fatalf("timeout = %s, want %s", got, tt.want)
+ }
+ })
+ }
+}