Run execute methods in pget package
(ctx context.Context, version string, args []string)
| 39 | |
| 40 | // Run execute methods in pget package |
| 41 | func (pget *Pget) Run(ctx context.Context, version string, args []string) error { |
| 42 | if err := pget.Ready(version, args); err != nil { |
| 43 | return errTop(err) |
| 44 | } |
| 45 | |
| 46 | // TODO(codehex): calc maxIdleConnsPerHost |
| 47 | client := newDownloadClient(16) |
| 48 | |
| 49 | target, err := Check(ctx, &CheckConfig{ |
| 50 | URLs: pget.URLs, |
| 51 | Timeout: time.Duration(pget.timeout) * time.Second, |
| 52 | Client: client, |
| 53 | }) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | filename := target.Filename |
| 59 | |
| 60 | var dir string |
| 61 | if pget.Output != "" { |
| 62 | fi, err := os.Stat(pget.Output) |
| 63 | if err == nil && fi.IsDir() { |
| 64 | dir = pget.Output |
| 65 | } else { |
| 66 | dir, filename = filepath.Split(pget.Output) |
| 67 | if dir != "" { |
| 68 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 69 | return errors.Wrapf(err, "failed to create diretory at %s", dir) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | opts := []DownloadOption{ |
| 76 | WithUserAgent(pget.useragent, version), |
| 77 | WithReferer(pget.referer), |
| 78 | } |
| 79 | |
| 80 | return Download(ctx, &DownloadConfig{ |
| 81 | Filename: filename, |
| 82 | Dirname: dir, |
| 83 | ContentLength: target.ContentLength, |
| 84 | Procs: pget.Procs, |
| 85 | URLs: target.URLs, |
| 86 | Client: client, |
| 87 | }, opts...) |
| 88 | } |
| 89 | |
| 90 | const ( |
| 91 | warningNumConnection = 4 |