55 extver "github.com/linuxsuren/cobra-extension/version"
66 "github.com/linuxsuren/http-downloader/pkg"
77 "github.com/spf13/cobra"
8+ "net/url"
9+ "path"
810 "runtime"
911 "strings"
1012)
@@ -20,7 +22,7 @@ func NewRoot() (cmd *cobra.Command) {
2022 getCmd := & cobra.Command {
2123 Use : "get" ,
2224 Short : "download the file" ,
23- Example : "hd get jenkins-zh/jenkins-cli/jcli -o jcli.tar.gz -- thread 3 " ,
25+ Example : "hd get jenkins-zh/jenkins-cli/jcli -- thread 6 " ,
2426 PreRunE : opt .preRunE ,
2527 RunE : opt .runE ,
2628 }
@@ -31,6 +33,8 @@ func NewRoot() (cmd *cobra.Command) {
3133 flags .BoolVarP (& opt .ShowProgress , "show-progress" , "" , true , "If show the progress of download" )
3234 flags .Int64VarP (& opt .ContinueAt , "continue-at" , "" , - 1 , "ContinueAt" )
3335 flags .IntVarP (& opt .Thread , "thread" , "" , 0 , "" )
36+ flags .BoolVarP (& opt .KeepPart , "keep-part" , "" , false ,
37+ "If you want to keep the part files instead of deleting them" )
3438 flags .StringVarP (& opt .Provider , "provider" , "" , ProviderGitHub , "The file provider" )
3539 flags .StringVarP (& opt .OS , "os" , "" , "" , "The OS of target binary file" )
3640 flags .StringVarP (& opt .Arch , "arch" , "" , "" , "The arch of target binary file" )
@@ -52,7 +56,8 @@ type downloadOption struct {
5256 Arch string
5357 OS string
5458
55- Thread int
59+ Thread int
60+ KeepPart bool
5661}
5762
5863const (
@@ -118,18 +123,27 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
118123 o .Arch = runtime .GOARCH
119124 }
120125
121- url := args [0 ]
122- if ! strings .HasPrefix (url , "http://" ) && ! strings .HasPrefix (url , "https://" ) {
123- if url , err = o .providerURLParse (url ); err != nil {
126+ targetURL := args [0 ]
127+ if ! strings .HasPrefix (targetURL , "http://" ) && ! strings .HasPrefix (targetURL , "https://" ) {
128+ if targetURL , err = o .providerURLParse (targetURL ); err != nil {
124129 err = fmt .Errorf ("only http:// or https:// supported, error: %v" , err )
125130 return
126131 }
127- cmd .Printf ("start to download from %s\n " , url )
132+ cmd .Printf ("start to download from %s\n " , targetURL )
128133 }
129- o .URL = url
134+ o .URL = targetURL
130135
131136 if o .Output == "" {
132- err = fmt .Errorf ("output cannot be empty" )
137+ var urlObj * url.URL
138+ if urlObj , err = url .Parse (o .URL ); err == nil {
139+ o .Output = path .Base (urlObj .Path )
140+
141+ if o .Output == "" {
142+ err = fmt .Errorf ("output cannot be empty" )
143+ }
144+ } else {
145+ err = fmt .Errorf ("cannot parse the target URL, error: '%v'" , err )
146+ }
133147 }
134148 return
135149}
@@ -138,7 +152,7 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
138152 if o .Thread <= 1 {
139153 err = pkg .DownloadWithContinue (o .URL , o .Output , o .ContinueAt , 0 , o .ShowProgress )
140154 } else {
141- err = pkg .DownloadFileWithMultipleThread (o .URL , o .Output , o .Thread , o .ShowProgress )
155+ err = pkg .DownloadFileWithMultipleThreadKeepParts (o .URL , o .Output , o .Thread , o . KeepPart , o .ShowProgress )
142156 }
143157 return
144158}
0 commit comments