|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package commands |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/url" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/cli/globals" |
| 23 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | + "github.com/spf13/viper" |
| 27 | + "go.bug.st/downloader" |
| 28 | +) |
| 29 | + |
| 30 | +// GetDownloaderConfig returns the downloader configuration based on |
| 31 | +// current settings. |
| 32 | +func GetDownloaderConfig() (*downloader.Config, error) { |
| 33 | + res := &downloader.Config{ |
| 34 | + RequestHeaders: globals.NewHTTPClientHeader(viper.GetString("network.user_agent_ext")), |
| 35 | + } |
| 36 | + if viper.IsSet("network.proxy") { |
| 37 | + proxy := viper.GetString("network.proxy") |
| 38 | + if _, err := url.Parse(proxy); err != nil { |
| 39 | + return nil, errors.New("Invalid network.proxy '" + proxy + "': " + err.Error()) |
| 40 | + } |
| 41 | + res.ProxyURL = proxy |
| 42 | + logrus.Infof("Using proxy %s", proxy) |
| 43 | + } |
| 44 | + return res, nil |
| 45 | +} |
| 46 | + |
| 47 | +// Download performs a download loop using the provided downloader.Downloader. |
| 48 | +// Messages are passed back to the DownloadProgressCB using label as text for the File field. |
| 49 | +func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error { |
| 50 | + if d == nil { |
| 51 | + // This signal means that the file is already downloaded |
| 52 | + downloadCB(&rpc.DownloadProgress{ |
| 53 | + File: label, |
| 54 | + Completed: true, |
| 55 | + }) |
| 56 | + return nil |
| 57 | + } |
| 58 | + downloadCB(&rpc.DownloadProgress{ |
| 59 | + File: label, |
| 60 | + Url: d.URL, |
| 61 | + TotalSize: d.Size(), |
| 62 | + }) |
| 63 | + d.RunAndPoll(func(downloaded int64) { |
| 64 | + downloadCB(&rpc.DownloadProgress{Downloaded: downloaded}) |
| 65 | + }, 250*time.Millisecond) |
| 66 | + if d.Error() != nil { |
| 67 | + return d.Error() |
| 68 | + } |
| 69 | + downloadCB(&rpc.DownloadProgress{Completed: true}) |
| 70 | + return nil |
| 71 | +} |
0 commit comments