diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4406a6b..d144fd4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,6 +15,8 @@ jobs: - name: Set up Go uses: actions/setup-go@master + with: + go-version: 1.22 - name: Run GoReleaser uses: goreleaser/goreleaser-action@master diff --git a/main.go b/main.go index f5ad7ec..14067ed 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ func main() { wait := flag.Uint("w", 4, "connection waiting seconds") ua := flag.String("ua", defua, "customize user agent") h := flag.Bool("h", false, "display this help") + home := flag.String("H", ".", "download under this path") flag.BoolVar(¬ui, "notui", false, "use plain text instead of TUI") flag.Parse() args := flag.Args() @@ -44,6 +45,11 @@ func main() { fmt.Println(cmdlst.String()) return } + err := os.MkdirAll(*home, 0755) + if err != nil { + logrus.Errorln("mkdirs of path", *home, "err:", err) + return + } if notui { logrus.Infoln("RVC Models Downloader start at", time.Now().Local().Format(time.DateTime+" (MST)")) logrus.Infof("operating system: %s, architecture: %s", runtime.GOOS, runtime.GOARCH) @@ -89,7 +95,7 @@ func main() { } ch := make(chan struct{}) go func() { - err := usercfg.download(args[0], "", *ua, time.Second*time.Duration(*wait), *cust, !*ntrs, *force) + err := usercfg.download(args[0], "", *home, *ua, time.Second*time.Duration(*wait), *cust, !*ntrs, *force) ch <- struct{}{} if err != nil { errorln(err) diff --git a/net.go b/net.go index 4d2a057..714ac51 100644 --- a/net.go +++ b/net.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "os" + "path" "runtime" "strconv" "strings" @@ -15,16 +16,16 @@ import ( "github.com/pkg/errors" ) -func (c *config) download(path, prefix, ua string, waits time.Duration, usecust, usetrs, force bool) error { +func (c *config) download(p, prefix, home, ua string, waits time.Duration, usecust, usetrs, force bool) error { for i, t := range c.Targets { if t.Refer != "" { - refp := path[:strings.LastIndex(path, "/")+1] + t.Refer + refp := p[:strings.LastIndex(p, "/")+1] + t.Refer infof("#%s%d refer to target '%s'.", prefix, i+1, refp) refcfg, err := readconfig(refp, usecust) if err != nil { return err } - err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", ua, waits, usecust, usetrs, force) + err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", home, ua, waits, usecust, usetrs, force) if err != nil { return err } @@ -38,11 +39,12 @@ func (c *config) download(path, prefix, ua string, waits time.Duration, usecust, warnf("#%s%d target required Arch: %s but you are %s, skip.", prefix, i+1, t.Arch, runtime.GOARCH) continue } - err := os.MkdirAll(t.Folder, 0755) + homefolder := path.Join(home, t.Folder) + err := os.MkdirAll(homefolder, 0755) if err != nil { - return errors.Wrap(err, fmt.Sprintf("#%s%d make target folder '%s'", prefix, i+1, t.Folder)) + return errors.Wrap(err, fmt.Sprintf("#%s%d make target folder '%s'", prefix, i+1, homefolder)) } - infof("#%s%d open target folder '%s'.", prefix, i+1, t.Folder) + infof("#%s%d open target folder '%s'.", prefix, i+1, homefolder) if len(t.Copy) == 0 { warnf("#%s%d empty copy target.", prefix, i+1) continue @@ -57,7 +59,7 @@ func (c *config) download(path, prefix, ua string, waits time.Duration, usecust, if sleep > time.Millisecond { time.Sleep(sleep) } - fname := t.Folder + "/" + cp[strings.LastIndex(cp, "/")+1:] + fname := path.Join(homefolder, cp[strings.LastIndex(cp, "/")+1:]) if !force { if _, err := os.Stat(fname); err == nil || os.IsExist(err) { warnf("#%s%d skip exist file %s", prefix, i+1, fname)