Skip to content

Commit

Permalink
feat: add cmdline option -f
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Apr 18, 2024
1 parent da4eaf8 commit fed1f04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 10 additions & 4 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fed1f04

Please sign in to comment.