Skip to content

Commit 4452afd

Browse files
authored
Add GitHub release files download friendly (#3)
* Add GitHub release files download friendly * Fix the lint issue
1 parent 6adb359 commit 4452afd

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

README-zh.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
通过命令:`brew install linuxsuren/linuxsuren/hd` 来安装
1212

13+
或者,对于 Linux 用户可以直接通过命令下载:
14+
```
15+
curl -L https://github.com/linuxsuren/http-downloader/releases/latest/download/hd-linux-amd64.tar.gz | tar xzv
16+
mv hd /usr/local/bin
17+
```
18+
1319
# 用法
1420

1521
```
@@ -21,4 +27,4 @@ hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-l
2127
* 基于 HTTP 协议下载文件的 Golang 工具库
2228
* 多线程
2329
* 断点续传 (TODO)
24-
* 对 GitHub release 文件下载友好 (TODO)
30+
* 对 GitHub release 文件下载友好

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
Install it via: `brew install linuxsuren/linuxsuren/hd`
1212

13+
Or download it directly (for Linux):
14+
```
15+
curl -L https://github.com/linuxsuren/http-downloader/releases/latest/download/hd-linux-amd64.tar.gz | tar xzv
16+
mv hd /usr/local/bin
17+
```
18+
1319
# Usage
1420

1521
```
@@ -21,4 +27,4 @@ hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-l
2127
* go library for HTTP
2228
* multi-thread
2329
* continuously (TODO)
24-
* GitHub release asset friendly (TODO)
30+
* GitHub release asset friendly

cmd/root.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"net/http"
1010
"os"
11+
"runtime"
1112
"strconv"
1213
"strings"
1314
"sync"
@@ -24,6 +25,7 @@ func NewRoot() (cmd *cobra.Command) {
2425
getCmd := &cobra.Command{
2526
Use: "get",
2627
Short: "download the file",
28+
Example: "hd get jenkins-zh/jenkins-cli/jcli -o jcli.tar.gz --thread 3",
2729
PreRunE: opt.preRunE,
2830
RunE: opt.runE,
2931
}
@@ -34,6 +36,9 @@ func NewRoot() (cmd *cobra.Command) {
3436
flags.BoolVarP(&opt.ShowProgress, "show-progress", "", true, "If show the progress of download")
3537
flags.Int64VarP(&opt.ContinueAt, "continue-at", "", -1, "ContinueAt")
3638
flags.IntVarP(&opt.Thread, "thread", "", 0, "")
39+
flags.StringVarP(&opt.Provider, "provider", "", ProviderGitHub, "The file provider")
40+
flags.StringVarP(&opt.OS, "os", "", "", "The OS of target binary file")
41+
flags.StringVarP(&opt.Arch, "arch", "", "", "The arch of target binary file")
3742

3843
cmd.AddCommand(
3944
getCmd,
@@ -48,17 +53,83 @@ type downloadOption struct {
4853

4954
ContinueAt int64
5055

56+
Provider string
57+
Arch string
58+
OS string
59+
5160
Thread int
5261
}
5362

63+
const (
64+
// ProviderGitHub represents https://github.com
65+
ProviderGitHub = "github"
66+
)
67+
68+
func (o *downloadOption) providerURLParse(path string) (url string, err error) {
69+
url = path
70+
if o.Provider != ProviderGitHub {
71+
return
72+
}
73+
74+
var (
75+
org string
76+
repo string
77+
name string
78+
version string
79+
)
80+
81+
addr := strings.Split(url, "/")
82+
if len(addr) >= 2 {
83+
org = addr[0]
84+
repo = addr[1]
85+
name = repo
86+
} else {
87+
err = fmt.Errorf("only support format xx/xx or xx/xx/xx")
88+
return
89+
}
90+
91+
if len(addr) == 3 {
92+
name = addr[2]
93+
} else {
94+
err = fmt.Errorf("only support format xx/xx or xx/xx/xx")
95+
}
96+
97+
// extract version from name
98+
if strings.Contains(name, "@") {
99+
nameWithVer := strings.Split(name, "@")
100+
name = nameWithVer[0]
101+
version = nameWithVer[1]
102+
103+
url = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz",
104+
org, repo, version, name, o.OS, o.Arch)
105+
} else {
106+
version = "latest"
107+
url = fmt.Sprintf("https://github.com/%s/%s/releases/%s/download/%s-%s-%s.tar.gz",
108+
org, repo, version, name, o.OS, o.Arch)
109+
}
110+
return
111+
}
112+
54113
func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error) {
55114
if len(args) <= 0 {
56115
return fmt.Errorf("no URL provided")
57116
}
58117

118+
if o.OS == "" {
119+
o.OS = runtime.GOOS
120+
}
121+
122+
if o.Arch == "" {
123+
o.Arch = runtime.GOARCH
124+
}
125+
59126
url := args[0]
60127
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
61-
err = fmt.Errorf("only http:// or https:// supported")
128+
if url, err = o.providerURLParse(url); err != nil {
129+
err = fmt.Errorf("only http:// or https:// supported, error: %v", err)
130+
return
131+
}
132+
cmd.Printf("start to download from %s\n", url)
62133
}
63134
o.URL = url
64135

0 commit comments

Comments
 (0)