Skip to content

Commit 3e7d5f3

Browse files
authored
Support version as an URL (#225)
* Support version as an URL * Add unit tests for getDynamicVersion
1 parent 1fb4527 commit 3e7d5f3

File tree

16 files changed

+81
-376
lines changed

16 files changed

+81
-376
lines changed

README-zh.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
通过命令:`brew install linuxsuren/linuxsuren/hd` 来安装
1212

1313
或者,对于 Linux 用户可以直接通过命令下载:
14-
```
14+
```shell
1515
curl -L https://github.com/linuxsuren/http-downloader/releases/latest/download/hd-linux-amd64.tar.gz | tar xzv
1616
mv hd /usr/local/bin
1717
```
@@ -20,26 +20,25 @@ mv hd /usr/local/bin
2020

2121
# 用法
2222

23-
```
23+
```shell
2424
hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz --thread 6
2525
```
2626

2727
或者,用一个更加简便的办法:
2828

29-
```
29+
```shell
3030
hd get jenkins-zh/jenkins-cli/jcli -t 6
3131
```
3232

3333
获取,你也可以安装一个来自 GitHub 的软件包:
3434

35-
```
35+
```shell
3636
hd install jenkins-zh/jenkins-cli/jcli -t 6
3737
```
38-
```
3938

4039
或者,你也可以从 GitHub 上下载预发布的二进制包:
4140

42-
```
41+
```shell
4342
hd get --pre ks
4443
```
4544

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ Want to go through the code? [GitPod](https://gitpod.io/#https://github.com/linu
2323
# Usage
2424

2525
## Download
26-
```
26+
```shell
2727
hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz --thread 6
2828
```
2929

3030
Or use a simple way instead of typing the whole URL:
3131

32-
```
32+
```shell
3333
hd get jenkins-zh/jenkins-cli/jcli -t 6
3434
```
3535

3636
Or you might want to download a pre-released binary package from GitHub:
3737

38-
```
38+
```shell
3939
hd get --pre ks
4040
```
4141

@@ -76,7 +76,7 @@ CMD ["ks"]
7676
You can import it from `github.com/linuxsuren/http-downloader/pkg/installer`, then put the following code to your CLI.
7777
It can help you to download desired tools:
7878

79-
```
79+
```go
8080
is := installer.Installer{
8181
Provider: "github",
8282
}

cmd/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ func TestArrayCompletion(t *testing.T) {
8787
array, direct := function(nil, nil, "")
8888
assert.Equal(t, []string{"a", "b"}, array)
8989
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, direct)
90-
}
90+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ require (
1919
github.com/stretchr/testify v1.7.0
2020
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
2121
gopkg.in/yaml.v2 v2.4.0
22+
github.com/h2non/gock v1.0.9
2223
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg
88
cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To=
99
cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4=
1010
cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M=
11+
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
12+
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
1113
cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc=
1214
cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk=
1315
cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs=

pkg/installer.go

Lines changed: 0 additions & 156 deletions
This file was deleted.

pkg/installer/check.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
230230
}
231231
}
232232

233+
var ver string
234+
if ver, err = getDynamicVersion(cfg.Version); ver != "" {
235+
hdPkg.Version = ver
236+
} else if err != nil {
237+
return
238+
}
239+
233240
if cfg.URL != "" {
234241
// it does not come from GitHub release
235242
tmp, _ := template.New("hd").Parse(cfg.URL)
@@ -284,6 +291,24 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
284291
return
285292
}
286293

294+
// parse the version if it's an URL
295+
func getDynamicVersion(version string) (realVersion string, err error) {
296+
if version != "" && (strings.HasPrefix(version, "http://") || strings.HasPrefix(version, "https://")) {
297+
var resp *http.Response
298+
if resp, err = http.Get(version); err != nil || resp.StatusCode != http.StatusOK {
299+
err = fmt.Errorf("cannot get version from '%s', error is '%v', status code is '%d'", version, err, resp.StatusCode)
300+
return
301+
}
302+
var data []byte
303+
if data, err = ioutil.ReadAll(resp.Body); err != nil {
304+
err = fmt.Errorf("failed to get version from '%s', error is '%v'", version, err)
305+
return
306+
}
307+
realVersion = string(data)
308+
}
309+
return
310+
}
311+
287312
func getVersionOrDefault(version string, defaultVer string) (target string) {
288313
target = defaultVer
289314
// for the security reason, only support https

pkg/installer/check_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package installer
22

33
import (
44
"fmt"
5+
"github.com/h2non/gock"
56
"github.com/stretchr/testify/assert"
7+
"net/http"
68
"os"
79
"path"
810
"runtime"
@@ -372,3 +374,43 @@ func Test_getHDConfig(t *testing.T) {
372374
})
373375
}
374376
}
377+
378+
func Test_getDynamicVersion(t *testing.T) {
379+
const fakeVersionURL = "https://fake.com"
380+
const expectVersion = "v1.1.1"
381+
382+
tests := []struct {
383+
name string
384+
prepare func()
385+
expectVersion string
386+
expectError bool
387+
}{{
388+
name: "normal case",
389+
prepare: func() {
390+
gock.New(fakeVersionURL).Get("/").Reply(http.StatusOK).BodyString(expectVersion)
391+
},
392+
expectVersion: expectVersion,
393+
expectError: false,
394+
}, {
395+
name: "got statusCode which is not 200",
396+
prepare: func() {
397+
gock.New(fakeVersionURL).Get("/").Reply(http.StatusNotFound)
398+
},
399+
expectVersion: "",
400+
expectError: true,
401+
}}
402+
for i := range tests {
403+
tt := tests[i]
404+
t.Run(tt.name, func(t *testing.T) {
405+
defer gock.Off()
406+
tt.prepare()
407+
version, err := getDynamicVersion(fakeVersionURL)
408+
assert.Equal(t, tt.expectVersion, version)
409+
if tt.expectError {
410+
assert.NotNil(t, err)
411+
} else {
412+
assert.Nil(t, err)
413+
}
414+
})
415+
}
416+
}

pkg/installer/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type HDConfig struct {
2727
PreInstalls []CmdWithArgs `yaml:"preInstalls"`
2828
PostInstalls []CmdWithArgs `yaml:"postInstalls"`
2929
TestInstalls []CmdWithArgs `yaml:"testInstalls"`
30+
Version string `yaml:"version"`
3031

3132
Org, Repo string
3233
}

pkg/net.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

pkg/os/apt/init.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ func SetInstallerRegistry(registry core.InstallerRegistry) {
1010
registry.Registry("vim", &vimInstallerInUbuntu{})
1111
registry.Registry("golang", &golangInstallerInUbuntu{})
1212
registry.Registry("git", &gitInstallerInUbuntu{})
13-
registry.Registry("kubectl", &kubectlInstallerInUbuntu{})
1413
registry.Registry("bash-completion", &bashCompletionInstallerInUbuntu{})
1514
registry.Registry("asciinema", &asciinemaInstallerInUbuntu{})
1615
registry.Registry("ffmpge", &ffmpegInstallerInUbuntu{})

0 commit comments

Comments
 (0)