Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support version as an URL #225

Merged
merged 2 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
通过命令:`brew install linuxsuren/linuxsuren/hd` 来安装

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

# 用法

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

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

```
```shell
hd get jenkins-zh/jenkins-cli/jcli -t 6
```

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

```
```shell
hd install jenkins-zh/jenkins-cli/jcli -t 6
```
```

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

```
```shell
hd get --pre ks
```

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ Want to go through the code? [GitPod](https://gitpod.io/#https://github.com/linu
# Usage

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

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

```
```shell
hd get jenkins-zh/jenkins-cli/jcli -t 6
```

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

```
```shell
hd get --pre ks
```

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

```
```go
is := installer.Installer{
Provider: "github",
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ func TestArrayCompletion(t *testing.T) {
array, direct := function(nil, nil, "")
assert.Equal(t, []string{"a", "b"}, array)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, direct)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ require (
github.com/stretchr/testify v1.7.0
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
gopkg.in/yaml.v2 v2.4.0
github.com/h2non/gock v1.0.9
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg
cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To=
cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4=
cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M=
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc=
cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk=
cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs=
Expand Down
156 changes: 0 additions & 156 deletions pkg/installer.go

This file was deleted.

25 changes: 25 additions & 0 deletions pkg/installer/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
}
}

var ver string
if ver, err = getDynamicVersion(cfg.Version); ver != "" {
hdPkg.Version = ver
} else if err != nil {
return
}

if cfg.URL != "" {
// it does not come from GitHub release
tmp, _ := template.New("hd").Parse(cfg.URL)
Expand Down Expand Up @@ -284,6 +291,24 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
return
}

// parse the version if it's an URL
func getDynamicVersion(version string) (realVersion string, err error) {
if version != "" && (strings.HasPrefix(version, "http://") || strings.HasPrefix(version, "https://")) {
var resp *http.Response
if resp, err = http.Get(version); err != nil || resp.StatusCode != http.StatusOK {
err = fmt.Errorf("cannot get version from '%s', error is '%v', status code is '%d'", version, err, resp.StatusCode)
return
}
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {
err = fmt.Errorf("failed to get version from '%s', error is '%v'", version, err)
return
}
realVersion = string(data)
}
return
}

func getVersionOrDefault(version string, defaultVer string) (target string) {
target = defaultVer
// for the security reason, only support https
Expand Down
42 changes: 42 additions & 0 deletions pkg/installer/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package installer

import (
"fmt"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -372,3 +374,43 @@ func Test_getHDConfig(t *testing.T) {
})
}
}

func Test_getDynamicVersion(t *testing.T) {
const fakeVersionURL = "https://fake.com"
const expectVersion = "v1.1.1"

tests := []struct {
name string
prepare func()
expectVersion string
expectError bool
}{{
name: "normal case",
prepare: func() {
gock.New(fakeVersionURL).Get("/").Reply(http.StatusOK).BodyString(expectVersion)
},
expectVersion: expectVersion,
expectError: false,
}, {
name: "got statusCode which is not 200",
prepare: func() {
gock.New(fakeVersionURL).Get("/").Reply(http.StatusNotFound)
},
expectVersion: "",
expectError: true,
}}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
defer gock.Off()
tt.prepare()
version, err := getDynamicVersion(fakeVersionURL)
assert.Equal(t, tt.expectVersion, version)
if tt.expectError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/installer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type HDConfig struct {
PreInstalls []CmdWithArgs `yaml:"preInstalls"`
PostInstalls []CmdWithArgs `yaml:"postInstalls"`
TestInstalls []CmdWithArgs `yaml:"testInstalls"`
Version string `yaml:"version"`

Org, Repo string
}
Expand Down
15 changes: 0 additions & 15 deletions pkg/net.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/os/apt/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func SetInstallerRegistry(registry core.InstallerRegistry) {
registry.Registry("vim", &vimInstallerInUbuntu{})
registry.Registry("golang", &golangInstallerInUbuntu{})
registry.Registry("git", &gitInstallerInUbuntu{})
registry.Registry("kubectl", &kubectlInstallerInUbuntu{})
registry.Registry("bash-completion", &bashCompletionInstallerInUbuntu{})
registry.Registry("asciinema", &asciinemaInstallerInUbuntu{})
registry.Registry("ffmpge", &ffmpegInstallerInUbuntu{})
Expand Down
Loading