From fed1f04b53cbd98032c71f1b9bbf61695bfcdc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 18 Apr 2024 18:42:23 +0900 Subject: [PATCH] feat: add cmdline option `-f` --- README.md | 1 + cfg.go | 14 ++++++++++---- main.go | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 806bd04..6783f8d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' -c use custom yaml instruction -dns string custom dns.yaml + -f force download even file exists -notrs use standard TLS client 'target/to/download' diff --git a/cfg.go b/cfg.go index ba2b7da..3456aec 100644 --- a/cfg.go +++ b/cfg.go @@ -65,7 +65,7 @@ type targets struct { Arch string `yaml:"Arch"` } -func (c *config) download(path, prefix string, usecust bool) error { +func (c *config) download(path, prefix string, usecust, force bool) error { for i, t := range c.Targets { if t.Refer != "" { refp := path[:strings.LastIndex(path, "/")+1] + t.Refer @@ -74,7 +74,7 @@ func (c *config) download(path, prefix string, usecust bool) error { if err != nil { return err } - err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", usecust) + err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", usecust, force) if err != nil { return err } @@ -116,6 +116,13 @@ func (c *config) download(path, prefix string, usecust bool) error { if sleep > time.Millisecond { time.Sleep(sleep) } + fname := t.Folder + "/" + cp + if !force { + if _, err := os.Stat(fname); err == nil || os.IsExist(err) { + logrus.Warnf("#%s%d skip exist file %s", prefix, i+1, fname) + return + } + } req, err := http.NewRequest("GET", c.BaseURL+"/"+cp, nil) if err != nil { logrus.Errorf("#%s%d new request to %s err: %v", prefix, i+1, cp, err) @@ -134,14 +141,13 @@ func (c *config) download(path, prefix string, usecust bool) error { logrus.Errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err) return } - fname := t.Folder + "/" + cp f, err := os.Create(fname) if err != nil { logrus.Errorf("#%s%d create file %s err: %v", prefix, i+1, fname, err) return } - logrus.Infof("#%s%d writing file %s", prefix, i+1, fname) defer f.Close() + logrus.Infof("#%s%d writing file %s", prefix, i+1, fname) pm := newmeter(fmt.Sprintf("#%s%d", prefix, i+1), fname, int(resp.ContentLength)) _, err = io.Copy(io.MultiWriter(f, &pm), resp.Body) if err != nil { diff --git a/main.go b/main.go index 42ddf65..b260733 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func main() { ntrs := flag.Bool("notrs", false, "use standard TLS client") dnsf := flag.String("dns", "", "custom dns.yaml") cust := flag.Bool("c", false, "use custom yaml instruction") + force := flag.Bool("f", false, "force download even file exists") flag.Parse() args := flag.Args() if len(args) != 1 { @@ -64,7 +65,7 @@ func main() { logrus.Errorln(err) return } - err = usercfg.download(args[0], "", *cust) + err = usercfg.download(args[0], "", *cust, *force) if err != nil { logrus.Errorln(err) return