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+
54113func (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