8
8
"io/ioutil"
9
9
"net/http"
10
10
"os"
11
+ "runtime"
11
12
"strconv"
12
13
"strings"
13
14
"sync"
@@ -24,6 +25,7 @@ func NewRoot() (cmd *cobra.Command) {
24
25
getCmd := & cobra.Command {
25
26
Use : "get" ,
26
27
Short : "download the file" ,
28
+ Example : "hd get jenkins-zh/jenkins-cli/jcli -o jcli.tar.gz --thread 3" ,
27
29
PreRunE : opt .preRunE ,
28
30
RunE : opt .runE ,
29
31
}
@@ -34,6 +36,9 @@ func NewRoot() (cmd *cobra.Command) {
34
36
flags .BoolVarP (& opt .ShowProgress , "show-progress" , "" , true , "If show the progress of download" )
35
37
flags .Int64VarP (& opt .ContinueAt , "continue-at" , "" , - 1 , "ContinueAt" )
36
38
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" )
37
42
38
43
cmd .AddCommand (
39
44
getCmd ,
@@ -48,17 +53,83 @@ type downloadOption struct {
48
53
49
54
ContinueAt int64
50
55
56
+ Provider string
57
+ Arch string
58
+ OS string
59
+
51
60
Thread int
52
61
}
53
62
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
+
54
113
func (o * downloadOption ) preRunE (cmd * cobra.Command , args []string ) (err error ) {
55
114
if len (args ) <= 0 {
56
115
return fmt .Errorf ("no URL provided" )
57
116
}
58
117
118
+ if o .OS == "" {
119
+ o .OS = runtime .GOOS
120
+ }
121
+
122
+ if o .Arch == "" {
123
+ o .Arch = runtime .GOARCH
124
+ }
125
+
59
126
url := args [0 ]
60
127
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 )
62
133
}
63
134
o .URL = url
64
135
0 commit comments