2024-04-18 01:52:23 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2024-04-18 18:21:01 +09:00
|
|
|
"strconv"
|
2024-04-18 01:52:23 +09:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed cfg.zip
|
|
|
|
var cfgdata []byte
|
|
|
|
|
|
|
|
var cfg = func() *zip.Reader {
|
|
|
|
r, err := zip.NewReader(bytes.NewReader(cfgdata), int64(len(cfgdata)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, f := range r.File {
|
|
|
|
cmdlst = append(cmdlst, f.Name)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}()
|
|
|
|
|
2024-04-18 18:21:01 +09:00
|
|
|
func readconfig(path string, usecust bool) (c config, err error) {
|
2024-04-18 01:52:23 +09:00
|
|
|
fname := path + ".yaml"
|
|
|
|
f, err := cfg.Open(fname)
|
2024-04-18 18:21:01 +09:00
|
|
|
if usecust && err != nil {
|
|
|
|
f, err = os.Open(fname)
|
|
|
|
}
|
2024-04-18 01:52:23 +09:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "invalid path")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = yaml.NewDecoder(f).Decode(&c)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "invalid config")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
BaseURL string `yaml:"BaseURL"`
|
|
|
|
Targets []targets `yaml:"Targets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type targets struct {
|
|
|
|
Refer string `yaml:"Refer"`
|
|
|
|
Folder string `yaml:"Folder"`
|
|
|
|
Copy []string `yaml:"Copy"`
|
|
|
|
OS string `yaml:"OS"`
|
|
|
|
Arch string `yaml:"Arch"`
|
|
|
|
}
|
|
|
|
|
2024-04-18 18:42:23 +09:00
|
|
|
func (c *config) download(path, prefix string, usecust, force bool) error {
|
2024-04-18 01:52:23 +09:00
|
|
|
for i, t := range c.Targets {
|
|
|
|
if t.Refer != "" {
|
|
|
|
refp := path[:strings.LastIndex(path, "/")+1] + t.Refer
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Infof("#%s%d refer to target '%s'.", prefix, i+1, refp)
|
|
|
|
refcfg, err := readconfig(refp, usecust)
|
2024-04-18 01:52:23 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-18 18:42:23 +09:00
|
|
|
err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", usecust, force)
|
2024-04-18 01:52:23 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.OS != "" && t.OS != runtime.GOOS {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Warnf("#%s%d target required OS: %s but you are %s, skip.", prefix, i+1, t.OS, runtime.GOOS)
|
2024-04-18 01:52:23 +09:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.Arch != "" && t.Arch != runtime.GOARCH {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Warnf("#%s%d target required Arch: %s but you are %s, skip.", prefix, i+1, t.Arch, runtime.GOARCH)
|
2024-04-18 01:52:23 +09:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := os.MkdirAll(t.Folder, 0755)
|
|
|
|
if err != nil {
|
2024-04-18 18:21:01 +09:00
|
|
|
return errors.Wrap(err, fmt.Sprintf("#%s%d make target folder '%s'", prefix, i+1, t.Folder))
|
2024-04-18 01:52:23 +09:00
|
|
|
}
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Infof("#%s%d open target folder '%s'.", prefix, i+1, t.Folder)
|
2024-04-18 01:52:23 +09:00
|
|
|
if len(t.Copy) == 0 {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Warningf("#%s%d empty copy target.", prefix, i+1)
|
2024-04-18 01:52:23 +09:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(t.Copy))
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Infof("#%s%d download copy: '%v'.", prefix, i+1, t.Copy)
|
|
|
|
for j, cp := range t.Copy {
|
|
|
|
go func(i int, cp, prefix string) {
|
2024-04-18 01:52:23 +09:00
|
|
|
defer wg.Done()
|
2024-04-18 18:21:01 +09:00
|
|
|
if strings.Contains(cp, "/") { // have innner folder
|
|
|
|
infldr := t.Folder + "/" + cp[:strings.LastIndex(cp, "/")]
|
|
|
|
err := os.MkdirAll(infldr, 0755)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("#%s%d make target inner folder '%s' err: %v", prefix, i+1, t.Folder, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Infof("#%s%d make target inner folder '%s'.", prefix, i+1, t.Folder)
|
|
|
|
}
|
2024-04-18 01:52:23 +09:00
|
|
|
sleep := time.Millisecond * 100 * time.Duration(i)
|
|
|
|
if sleep > time.Millisecond {
|
|
|
|
time.Sleep(sleep)
|
|
|
|
}
|
2024-04-18 18:42:23 +09:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 01:52:23 +09:00
|
|
|
req, err := http.NewRequest("GET", c.BaseURL+"/"+cp, nil)
|
|
|
|
if err != nil {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Errorf("#%s%d new request to %s err: %v", prefix, i+1, cp, err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Infof("#%s%d get: %s", prefix, i+1, req.URL)
|
2024-04-18 01:52:23 +09:00
|
|
|
req.Header.Add("user-agent", ua)
|
|
|
|
resp, err := cli.Do(req)
|
|
|
|
if err != nil {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err := errors.New(fmt.Sprintf("HTTP %d %s", resp.StatusCode, resp.Status))
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.Create(fname)
|
|
|
|
if err != nil {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Errorf("#%s%d create file %s err: %v", prefix, i+1, fname, err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2024-04-18 18:42:23 +09:00
|
|
|
logrus.Infof("#%s%d writing file %s", prefix, i+1, fname)
|
2024-04-18 18:21:01 +09:00
|
|
|
pm := newmeter(fmt.Sprintf("#%s%d", prefix, i+1), fname, int(resp.ContentLength))
|
|
|
|
_, err = io.Copy(io.MultiWriter(f, &pm), resp.Body)
|
2024-04-18 01:52:23 +09:00
|
|
|
if err != nil {
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Errorf("#%s%d download file %s err: %v", prefix, i+1, fname, err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
2024-04-18 18:21:01 +09:00
|
|
|
logrus.Infof("#%s%d finished download %s", prefix, i+1, fname)
|
|
|
|
}(j, cp, fmt.Sprintf("%s%d.", prefix, i+1))
|
2024-04-18 01:52:23 +09:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|