Skip to content

Commit 5cf23cf

Browse files
committed
update: prefer mirror manifest with github fallback
1 parent 9311f7c commit 5cf23cf

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ MSCODE_INSTALL_SOURCE=github curl -fsSL https://raw.githubusercontent.com/vigo99
1818
MSCODE_INSTALL_SOURCE=mirror curl -fsSL https://raw.githubusercontent.com/vigo999/mindspore-code/main/scripts/install.sh | bash
1919

2020
# Override the mirror base URL if you host your own Caddy/Nginx mirror.
21-
MSCODE_MIRROR_BASE_URL=http://13.229.44.116/mscode/releases curl -fsSL https://raw.githubusercontent.com/vigo999/mindspore-code/main/scripts/install.sh | bash
21+
MSCODE_MIRROR_BASE_URL=http://47.115.175.134/mscode/releases curl -fsSL https://raw.githubusercontent.com/vigo999/mindspore-code/main/scripts/install.sh | bash
2222
```
2323

2424
### Build from source

internal/update/checker.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,38 @@ func Check(ctx context.Context, currentVersion string) (*CheckResult, error) {
4343
}
4444

4545
func fetchManifest(ctx context.Context) (*Manifest, error) {
46+
return fetchManifestFromURLs(ctx, ManifestURLs())
47+
}
48+
49+
func fetchManifestFromURLs(ctx context.Context, urls []string) (*Manifest, error) {
50+
if len(urls) == 0 {
51+
return nil, fmt.Errorf("no manifest URLs configured")
52+
}
53+
54+
var errs []string
55+
for _, url := range urls {
56+
m, err := fetchManifestFromURL(ctx, url)
57+
if err == nil {
58+
return m, nil
59+
}
60+
errs = append(errs, fmt.Sprintf("%s: %v", url, err))
61+
}
62+
63+
return nil, fmt.Errorf("fetch manifest: %s", strings.Join(errs, "; "))
64+
}
65+
66+
func fetchManifestFromURL(ctx context.Context, url string) (*Manifest, error) {
4667
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
4768
defer cancel()
4869

49-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ManifestURL(), nil)
70+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
5071
if err != nil {
5172
return nil, fmt.Errorf("create request: %w", err)
5273
}
5374

5475
resp, err := http.DefaultClient.Do(req)
5576
if err != nil {
56-
return nil, fmt.Errorf("fetch manifest: %w", err)
77+
return nil, err
5778
}
5879
defer resp.Body.Close()
5980

internal/update/checker_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package update
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func TestManifestURLsDefaultOrder(t *testing.T) {
11+
t.Setenv("MSCODE_MANIFEST_URL", "")
12+
13+
got := ManifestURLs()
14+
if len(got) != 2 {
15+
t.Fatalf("ManifestURLs() len = %d, want 2", len(got))
16+
}
17+
if got[0] != defaultMirrorManifestURL {
18+
t.Fatalf("ManifestURLs()[0] = %q, want %q", got[0], defaultMirrorManifestURL)
19+
}
20+
if got[1] != defaultGitHubManifestURL {
21+
t.Fatalf("ManifestURLs()[1] = %q, want %q", got[1], defaultGitHubManifestURL)
22+
}
23+
}
24+
25+
func TestManifestURLsEnvOverride(t *testing.T) {
26+
t.Setenv("MSCODE_MANIFEST_URL", "http://example.test/manifest.json")
27+
28+
got := ManifestURLs()
29+
if len(got) != 1 {
30+
t.Fatalf("ManifestURLs() len = %d, want 1", len(got))
31+
}
32+
if got[0] != "http://example.test/manifest.json" {
33+
t.Fatalf("ManifestURLs()[0] = %q, want env override", got[0])
34+
}
35+
}
36+
37+
func TestFetchManifestFallsBackToNextURL(t *testing.T) {
38+
success := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39+
w.Header().Set("Content-Type", "application/json")
40+
_, _ = w.Write([]byte(`{"latest":"0.5.0-beta.2","min_allowed":"","download_base":"http://mirror.example/mscode/releases"}`))
41+
}))
42+
defer success.Close()
43+
44+
got, err := fetchManifestFromURLs(context.Background(), []string{
45+
"http://127.0.0.1:1/latest/manifest.json",
46+
success.URL,
47+
})
48+
if err != nil {
49+
t.Fatalf("fetchManifestFromURLs() error = %v", err)
50+
}
51+
if got.Latest != "0.5.0-beta.2" {
52+
t.Fatalf("manifest latest = %q, want %q", got.Latest, "0.5.0-beta.2")
53+
}
54+
if got.DownloadBase != "http://mirror.example/mscode/releases" {
55+
t.Fatalf("manifest download_base = %q", got.DownloadBase)
56+
}
57+
}

internal/update/paths.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"runtime"
77
)
88

9-
const defaultManifestURL = "https://github.com/vigo999/mindspore-code/releases/latest/download/manifest.json"
9+
const (
10+
defaultMirrorManifestURL = "http://47.115.175.134/mscode/releases/latest/manifest.json"
11+
defaultGitHubManifestURL = "https://github.com/vigo999/mindspore-code/releases/latest/download/manifest.json"
12+
)
1013

1114
// InstallDir returns ~/.mscode/bin.
1215
func InstallDir() string {
@@ -28,10 +31,20 @@ func ConfigDir() string {
2831
return filepath.Join(home, ".mscode")
2932
}
3033

31-
// ManifestURL returns the manifest URL, overridable via MSCODE_MANIFEST_URL.
34+
// ManifestURL returns the first manifest URL candidate.
3235
func ManifestURL() string {
36+
urls := ManifestURLs()
37+
if len(urls) == 0 {
38+
return ""
39+
}
40+
return urls[0]
41+
}
42+
43+
// ManifestURLs returns manifest URL candidates in lookup order.
44+
// If MSCODE_MANIFEST_URL is set, it is used exclusively.
45+
func ManifestURLs() []string {
3346
if u := os.Getenv("MSCODE_MANIFEST_URL"); u != "" {
34-
return u
47+
return []string{u}
3548
}
36-
return defaultManifestURL
49+
return []string{defaultMirrorManifestURL, defaultGitHubManifestURL}
3750
}

scripts/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
GITHUB_REPO="${MSCODE_GITHUB_REPO:-vigo999/mindspore-code}"
5-
MIRROR_BASE_URL="${MSCODE_MIRROR_BASE_URL:-http://13.229.44.116/mscode/releases}"
5+
MIRROR_BASE_URL="${MSCODE_MIRROR_BASE_URL:-http://47.115.175.134/mscode/releases}"
66
INSTALL_DIR="$HOME/.mscode/bin"
77
BINARY_NAME="mscode"
88
INSTALL_SOURCE="${MSCODE_INSTALL_SOURCE:-auto}"

0 commit comments

Comments
 (0)