@@ -4,7 +4,10 @@ import (
44 "bytes"
55 "context"
66 "fmt"
7+ "github.com/linuxsuren/http-downloader/pkg/common"
8+ "github.com/linuxsuren/http-downloader/pkg/log"
79 "io"
10+ "io/fs"
811 "net/http"
912 "net/url"
1013 sysos "os"
@@ -45,6 +48,8 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
4548 "If you accept preRelease as the binary asset from GitHub" )
4649 flags .BoolVarP (& opt .AcceptPreRelease , "pre" , "" , false ,
4750 "Same with option --accept-preRelease" )
51+ flags .BoolVarP (& opt .Force , "force" , "f" , false , "Overwrite the exist file if this is true" )
52+ flags .IntVarP (& opt .Mod , "mod" , "" , - 1 , "The file permission, -1 means using the system default" )
4853
4954 flags .IntVarP (& opt .Timeout , "time" , "" , 10 ,
5055 `The default timeout in seconds with the HTTP request` )
@@ -99,6 +104,8 @@ type downloadOption struct {
99104 AcceptPreRelease bool
100105 RoundTripper http.RoundTripper
101106 Magnet bool
107+ Force bool
108+ Mod int
102109
103110 ContinueAt int64
104111
@@ -233,6 +240,7 @@ func findAnchor(n *html.Node) (items []string) {
233240}
234241
235242func (o * downloadOption ) runE (cmd * cobra.Command , args []string ) (err error ) {
243+ logger := log .GetLoggerFromContextOrDefault (cmd )
236244 defer func () {
237245 if o .cancel != nil {
238246 o .cancel ()
@@ -271,6 +279,13 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
271279 return
272280 }
273281
282+ // check if want to overwrite the exist file
283+ logger .Println ("output file is" , o .Output )
284+ if common .Exist (o .Output ) && ! o .Force {
285+ logger .Printf ("The output file: '%s' was exist, please use flag --force if you want to overwrite it.\n " , o .Output )
286+ return
287+ }
288+
274289 if o .Magnet || strings .HasPrefix (o .URL , "magnet:?" ) {
275290 err = downloadMagnetFile (o .ProxyGitHub , o .URL , o .execer )
276291 return
@@ -279,8 +294,9 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
279294 targetURL := o .URL
280295 if o .ProxyGitHub != "" {
281296 targetURL = strings .Replace (targetURL , "github.com" , fmt .Sprintf ("%s/github.com" , o .ProxyGitHub ), 1 )
297+ targetURL = strings .Replace (targetURL , "raw.githubusercontent.com" , fmt .Sprintf ("%s/https://raw.githubusercontent.com" , o .ProxyGitHub ), 1 )
282298 }
283- cmd .Printf ("start to download from %s\n " , targetURL )
299+ logger .Printf ("start to download from %s\n " , targetURL )
284300 if o .Thread <= 1 {
285301 downloader := & net.ContinueDownloader {}
286302 downloader .WithoutProxy (o .NoProxy ).
@@ -294,6 +310,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
294310 WithRoundTripper (o .RoundTripper )
295311 err = downloader .Download (targetURL , o .Output , o .Thread )
296312 }
313+
314+ // set file permission
315+ if o .Mod != - 1 {
316+ err = sysos .Chmod (o .Output , fs .FileMode (o .Mod ))
317+ }
297318 return
298319}
299320
0 commit comments