Skip to content

Commit f84cdf8

Browse files
committed
fix: update asset download urls
1 parent 025444a commit f84cdf8

File tree

3 files changed

+52
-57
lines changed

3 files changed

+52
-57
lines changed

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: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,38 @@ import (
55
"bytes"
66
"compress/gzip"
77
"fmt"
8-
"github.com/loft-sh/devspace/assets"
98
"io"
109
"io/fs"
1110
"io/ioutil"
1211
"net/http"
1312
"os"
1413
"path/filepath"
15-
"regexp"
1614
"strings"
1715
"sync"
1816
"time"
1917

18+
"github.com/loft-sh/devspace/assets"
2019
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
2120
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
2221
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
2322
"github.com/loft-sh/devspace/pkg/devspace/upgrade"
23+
"github.com/loft-sh/devspace/pkg/util/git"
2424
"github.com/loft-sh/devspace/pkg/util/hash"
2525
logpkg "github.com/loft-sh/devspace/pkg/util/log"
2626
"github.com/mitchellh/go-homedir"
2727
"github.com/pkg/errors"
2828
v1 "k8s.io/api/core/v1"
2929
)
3030

31+
// DevSpaceHelperRepository is the repository containing the devspace helper
32+
const DevSpaceHelperRepository = "https://github.com/loft-sh/devspace"
33+
3134
// DevSpaceHelperBaseURL is the base url where to look for the sync helper
32-
const DevSpaceHelperBaseURL = "https://github.com/loft-sh/devspace/releases"
35+
const DevSpaceHelperBaseURL = DevSpaceHelperRepository + "/releases/download"
3336

3437
// DevSpaceHelperTempFolder is the local folder where we store the sync helper
3538
const DevSpaceHelperTempFolder = "devspacehelper"
3639

37-
// helperBinaryRegEx is the regexp that finds the correct download link for the sync helper binary
38-
var helperBinaryRegEx = `href="(\/loft-sh\/devspace\/releases\/download\/[^\/]*\/%s)"`
39-
4040
// DevSpaceHelperContainerPath is the path of the devspace helper in the container
4141
const DevSpaceHelperContainerPath = "/tmp/devspacehelper"
4242

@@ -143,34 +143,15 @@ func installDevSpaceHelperInContainer(client kubectl.Client, pod *v1.Pod, contai
143143

144144
// getDownloadURL
145145
func devSpaceHelperDownloadURL(version, filename string) (string, error) {
146-
url := ""
147146
if version == "latest" {
148-
url = fmt.Sprintf("%s/%s", DevSpaceHelperBaseURL, version)
149-
} else {
150-
url = fmt.Sprintf("%s/tag/%s", DevSpaceHelperBaseURL, version)
151-
}
152-
153-
// Download html
154-
resp, err := http.Get(url)
155-
if err != nil {
156-
return "", errors.Wrap(err, "get url")
157-
}
158-
159-
body, err := ioutil.ReadAll(resp.Body)
160-
if err != nil {
161-
return "", errors.Wrap(err, "read body")
162-
}
163-
164-
regEx, err := regexp.Compile(fmt.Sprintf(helperBinaryRegEx, filename))
165-
if err != nil {
166-
return "", err
147+
var err error
148+
version, err = git.GetLatestVersion(DevSpaceHelperRepository)
149+
if err != nil {
150+
return "", errors.Wrap(err, "get latest version")
151+
}
167152
}
168153

169-
matches := regEx.FindStringSubmatch(string(body))
170-
if len(matches) != 2 {
171-
return "", errors.Errorf("couldn't find %s in github release %s at url %s", filename, version, url)
172-
}
173-
return "https://github.com" + matches[1], nil
154+
return fmt.Sprintf("%s/%s/%s", DevSpaceHelperBaseURL, version, filename), nil
174155
}
175156

176157
func downloadSyncHelper(helperName, syncBinaryFolder, version string, log logpkg.Logger) error {

pkg/util/git/helper.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package git
22

33
import (
4+
"fmt"
5+
"net/http"
46
"os"
57
"os/exec"
8+
"regexp"
69
"strings"
710

811
"github.com/pkg/errors"
912
"gopkg.in/src-d/go-git.v4"
1013
)
1114

15+
var LatestTagRegEx = regexp.MustCompile(`\/tag\/(.*)$`)
16+
1217
// GetBranch retrieves the current HEADs name
1318
func GetBranch(localPath string) (string, error) {
1419
repo, err := git.PlainOpen(localPath)
@@ -77,3 +82,28 @@ func GetRemote(localPath string) (string, error) {
7782

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

0 commit comments

Comments
 (0)