Skip to content

Commit f56dc02

Browse files
authored
Merge pull request #2301 from lizardruss/asset-download-fix
fix: update asset download urls
2 parents 5f1f071 + 24c71e9 commit f56dc02

3 files changed

Lines changed: 58 additions & 61 deletions

File tree

pkg/devspace/server/download.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@ import (
66
"compress/gzip"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"net/http"
1110
"os"
1211
"path/filepath"
13-
"regexp"
1412
"strings"
1513
"time"
1614

1715
"github.com/loft-sh/devspace/assets"
1816

1917
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
2018
"github.com/loft-sh/devspace/pkg/devspace/upgrade"
19+
"github.com/loft-sh/devspace/pkg/util/git"
2120

2221
homedir "github.com/mitchellh/go-homedir"
2322
"github.com/pkg/errors"
2423
)
2524

26-
// UIDownloadBaseURL is the base url where to look for the ui
27-
const UIDownloadBaseURL = "https://github.com/loft-sh/devspace/releases"
25+
// UIRepository is the repository containing the devspace UI
26+
const UIRepository = "https://github.com/loft-sh/devspace"
2827

29-
// UIDownloadRegEx is the regexp that finds the correct download link for the ui
30-
var UIDownloadRegEx = regexp.MustCompile(`href="(\/loft-sh\/devspace\/releases\/download\/[^\/]*\/ui.tar.gz)"`)
28+
// UIDownloadBaseURL is the base url where to look for the ui
29+
const UIDownloadBaseURL = UIRepository + "/releases/download"
3130

3231
// UITempFolder is the temp folder to cache the ui in
3332
const UITempFolder = "ui"
@@ -77,30 +76,15 @@ func downloadFile(version string, folder string) error {
7776
}
7877

7978
// Create download url
80-
url := ""
8179
if version == "latest" {
82-
url = fmt.Sprintf("%s/%s", UIDownloadBaseURL, version)
83-
} else {
84-
url = fmt.Sprintf("%s/tag/%s", UIDownloadBaseURL, version)
80+
version, err = git.GetLatestVersion(UIRepository)
81+
if err != nil {
82+
return errors.Wrap(err, "get latest version")
83+
}
8584
}
8685

87-
// Download html
86+
url := fmt.Sprintf("%s/%s/%s", UIDownloadBaseURL, version, "ui.tar.gz")
8887
resp, err := http.Get(url)
89-
if err != nil {
90-
return errors.Wrap(err, "get url")
91-
}
92-
93-
body, err := ioutil.ReadAll(resp.Body)
94-
if err != nil {
95-
return errors.Wrap(err, "read body")
96-
}
97-
98-
matches := UIDownloadRegEx.FindStringSubmatch(string(body))
99-
if len(matches) != 2 {
100-
return errors.Errorf("Couldn't find ui in github release %s at url %s", version, url)
101-
}
102-
103-
resp, err = http.Get("https://github.com" + matches[1])
10488
if err != nil {
10589
return errors.Wrap(err, "download ui archive")
10690
}

pkg/devspace/services/inject/inject.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,41 @@ import (
66
"compress/gzip"
77
"context"
88
"fmt"
9-
"github.com/loft-sh/devspace/assets"
10-
"github.com/loft-sh/devspace/pkg/devspace/env"
119
"io"
1210
"io/fs"
1311
"io/ioutil"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1512
"net/http"
1613
"os"
1714
"path/filepath"
18-
"regexp"
1915
"strings"
2016
"sync"
2117
"time"
2218

19+
"github.com/loft-sh/devspace/assets"
20+
"github.com/loft-sh/devspace/pkg/devspace/env"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
2323
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
2424
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
2525
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
2626
"github.com/loft-sh/devspace/pkg/devspace/upgrade"
27+
"github.com/loft-sh/devspace/pkg/util/git"
2728
"github.com/loft-sh/devspace/pkg/util/hash"
2829
logpkg "github.com/loft-sh/devspace/pkg/util/log"
2930
"github.com/mitchellh/go-homedir"
3031
"github.com/pkg/errors"
3132
v1 "k8s.io/api/core/v1"
3233
)
3334

35+
// DevSpaceHelperRepository is the repository containing the devspace helper
36+
const DevSpaceHelperRepository = "https://github.com/loft-sh/devspace"
37+
3438
// DevSpaceHelperBaseURL is the base url where to look for the sync helper
35-
const DevSpaceHelperBaseURL = "https://github.com/loft-sh/devspace/releases"
39+
const DevSpaceHelperBaseURL = DevSpaceHelperRepository + "/releases/download"
3640

3741
// DevSpaceHelperTempFolder is the local folder where we store the sync helper
3842
const DevSpaceHelperTempFolder = "devspacehelper"
3943

40-
// helperBinaryRegEx is the regexp that finds the correct download link for the sync helper binary
41-
var helperBinaryRegEx = `href="(\/loft-sh\/devspace\/releases\/download\/[^\/]*\/%s)"`
42-
4344
// DevSpaceHelperContainerPath is the path of the devspace helper in the container
4445
const DevSpaceHelperContainerPath = "/tmp/devspacehelper"
4546

@@ -153,34 +154,15 @@ func installDevSpaceHelperInContainer(ctx context.Context, client kubectl.Client
153154

154155
// getDownloadURL
155156
func devSpaceHelperDownloadURL(version, filename string) (string, error) {
156-
url := ""
157157
if version == "latest" {
158-
url = fmt.Sprintf("%s/%s", DevSpaceHelperBaseURL, version)
159-
} else {
160-
url = fmt.Sprintf("%s/tag/%s", DevSpaceHelperBaseURL, version)
161-
}
162-
163-
// Download html
164-
resp, err := http.Get(url)
165-
if err != nil {
166-
return "", errors.Wrap(err, "get url")
167-
}
168-
169-
body, err := ioutil.ReadAll(resp.Body)
170-
if err != nil {
171-
return "", errors.Wrap(err, "read body")
172-
}
173-
174-
regEx, err := regexp.Compile(fmt.Sprintf(helperBinaryRegEx, filename))
175-
if err != nil {
176-
return "", err
158+
var err error
159+
version, err = git.GetLatestVersion(DevSpaceHelperRepository)
160+
if err != nil {
161+
return "", errors.Wrap(err, "get latest version")
162+
}
177163
}
178164

179-
matches := regEx.FindStringSubmatch(string(body))
180-
if len(matches) != 2 {
181-
return "", errors.Errorf("couldn't find %s in github release %s at url %s", filename, version, url)
182-
}
183-
return "https://github.com" + matches[1], nil
165+
return fmt.Sprintf("%s/%s/%s", DevSpaceHelperBaseURL, version, filename), nil
184166
}
185167

186168
func downloadSyncHelper(ctx context.Context, helperName, syncBinaryFolder, version string, log logpkg.Logger) error {

pkg/util/git/helper.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package git
22

33
import (
44
"context"
5-
"github.com/loft-sh/devspace/pkg/util/command"
6-
"mvdan.cc/sh/v3/expand"
5+
"fmt"
6+
"net/http"
77
"os"
8+
"regexp"
89
"strings"
910

11+
"github.com/loft-sh/devspace/pkg/util/command"
12+
"mvdan.cc/sh/v3/expand"
13+
1014
"github.com/pkg/errors"
1115
"gopkg.in/src-d/go-git.v4"
1216
)
1317

18+
var LatestTagRegEx = regexp.MustCompile(`\/tag\/(.*)$`)
19+
1420
// GetBranch retrieves the current HEADs name
1521
func GetBranch(localPath string) (string, error) {
1622
repo, err := git.PlainOpen(localPath)
@@ -79,3 +85,28 @@ func GetRemote(localPath string) (string, error) {
7985

8086
return urls[0], nil
8187
}
88+
89+
func GetLatestVersion(repository string) (string, error) {
90+
client := &http.Client{
91+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
92+
return http.ErrUseLastResponse
93+
},
94+
}
95+
96+
resp, err := client.Get(repository + "/releases/latest")
97+
if err != nil {
98+
return "", err
99+
}
100+
101+
redirect := resp.Header.Get("location")
102+
if redirect == "" {
103+
return "", fmt.Errorf("redirect URL not found")
104+
}
105+
106+
matches := LatestTagRegEx.FindStringSubmatch(redirect)
107+
if len(matches) != 2 {
108+
return "", errors.Errorf("Couldn't find latest release version")
109+
}
110+
111+
return matches[1], nil
112+
}

0 commit comments

Comments
 (0)