Skip to content

Commit 6b23384

Browse files
committed
Using simpler download library
There is no need for a fully fledged download library like `grab`. This commit fix cavaliergopher/grab#43 (comment)
1 parent d1ece64 commit 6b23384

File tree

31 files changed

+248
-1560
lines changed

31 files changed

+248
-1560
lines changed

Gopkg.lock

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@
121121
name = "github.com/bouk/monkey"
122122
revision = "5df1f207ff77e025801505ae4d903133a0b4353f"
123123

124-
[[constraint]]
125-
name = "github.com/cavaliercoder/grab"
126-
branch = "master"
127-
128-
129124
[prune]
130125
go-tests = true
131126
unused-packages = true

arduino/cores/packagemanager/download.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/arduino/arduino-cli/arduino/cores"
24-
"github.com/cavaliercoder/grab"
24+
"go.bug.st/downloader"
2525
"go.bug.st/relaxed-semver"
2626
)
2727

@@ -102,7 +102,7 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
102102

103103
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Response
104104
// is returned.
105-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*grab.Response, error) {
105+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*downloader.Downloader, error) {
106106
resource := tool.GetCompatibleFlavour()
107107
if resource == nil {
108108
return nil, fmt.Errorf("tool not available for your OS")
@@ -112,6 +112,6 @@ func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*grab.Re
112112

113113
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
114114
// nil Response is returned.
115-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*grab.Response, error) {
115+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*downloader.Downloader, error) {
116116
return platform.Resource.Download(pm.DownloadDir)
117117
}

arduino/libraries/librariesmanager/download.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,17 @@
1818
package librariesmanager
1919

2020
import (
21-
"fmt"
2221
"net/url"
2322

24-
"github.com/cavaliercoder/grab"
23+
"go.bug.st/downloader"
2524
)
2625

2726
// LibraryIndexURL is the URL where to get library index.
2827
var LibraryIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json")
2928

3029
// UpdateIndex downloads the libraries index file from Arduino repository.
31-
func (lm *LibrariesManager) UpdateIndex() (*grab.Response, error) {
32-
req, err := grab.NewRequest(lm.IndexFile.String(), LibraryIndexURL.String())
33-
req.NoResume = true
34-
if err != nil {
35-
return nil, fmt.Errorf("creating HTTP request: %s", err)
36-
}
37-
client := grab.NewClient()
38-
return client.Do(req), nil
39-
30+
func (lm *LibrariesManager) UpdateIndex() (*downloader.Downloader, error) {
31+
lm.IndexFile.Remove()
4032
// TODO: Download from gzipped URL index
33+
return downloader.Download(lm.IndexFile.String(), LibraryIndexURL.String())
4134
}

arduino/resources/helpers.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"os"
2323

2424
paths "github.com/arduino/go-paths-helper"
25-
"github.com/cavaliercoder/grab"
25+
"go.bug.st/downloader"
2626
)
2727

2828
// ArchivePath returns the path of the Archive of the specified DownloadResource relative
@@ -45,7 +45,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
4545
}
4646

4747
// Download a DownloadResource.
48-
func (r *DownloadResource) Download(downloadDir *paths.Path) (*grab.Response, error) {
48+
func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downloader, error) {
4949
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
5050
if err != nil {
5151
return nil, fmt.Errorf("testing local archive integrity: %s", err)
@@ -73,10 +73,5 @@ func (r *DownloadResource) Download(downloadDir *paths.Path) (*grab.Response, er
7373
return nil, fmt.Errorf("getting archive file info: %s", err)
7474
}
7575

76-
req, err := grab.NewRequest(path.String(), r.URL)
77-
if err != nil {
78-
return nil, fmt.Errorf("creating HTTP request: %s", err)
79-
}
80-
client := grab.NewClient()
81-
return client.Do(req), nil
76+
return downloader.Download(path.String(), r.URL)
8277
}

commands/commands.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib
155155
// UpdateLibrariesIndex updates the library_index.json
156156
func UpdateLibrariesIndex(lm *librariesmanager.LibrariesManager) {
157157
logrus.Info("Updating libraries index")
158-
resp, err := lm.UpdateIndex()
158+
d, err := lm.UpdateIndex()
159159
if err != nil {
160160
formatter.PrintError(err, "Error downloading librarires index")
161161
os.Exit(ErrNetwork)
162162
}
163-
formatter.DownloadProgressBar(resp, "Updating index: library_index.json")
164-
if resp.Err() != nil {
165-
formatter.PrintError(resp.Err(), "Error downloading librarires index")
163+
formatter.DownloadProgressBar(d, "Updating index: library_index.json")
164+
if d.Error() != nil {
165+
formatter.PrintError(d.Error(), "Error downloading librarires index")
166166
os.Exit(ErrNetwork)
167167
}
168168
}

commands/core/download.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ package core
2020
import (
2121
"os"
2222

23+
"go.bug.st/downloader"
24+
2325
"github.com/arduino/arduino-cli/arduino/cores"
2426
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2527
"github.com/arduino/arduino-cli/commands"
2628
"github.com/arduino/arduino-cli/common/formatter"
27-
"github.com/cavaliercoder/grab"
2829
"github.com/sirupsen/logrus"
2930
"github.com/spf13/cobra"
3031
)
@@ -88,18 +89,18 @@ func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.T
8889
download(resp, err, toolRelease.String())
8990
}
9091

91-
func download(resp *grab.Response, err error, label string) {
92+
func download(d *downloader.Downloader, err error, label string) {
9293
if err != nil {
9394
formatter.PrintError(err, "Error downloading "+label)
9495
os.Exit(commands.ErrNetwork)
9596
}
96-
if resp == nil {
97+
if d == nil {
9798
formatter.Print(label + " already downloaded")
9899
return
99100
}
100-
formatter.DownloadProgressBar(resp, label)
101-
if resp.Err() != nil {
102-
formatter.PrintError(resp.Err(), "Error downloading "+label)
101+
formatter.DownloadProgressBar(d, label)
102+
if d.Error() != nil {
103+
formatter.PrintError(d.Error(), "Error downloading "+label)
103104
os.Exit(commands.ErrNetwork)
104105
}
105106
}

commands/core/update_index.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"os"
2424
"path"
2525

26+
"go.bug.st/downloader"
27+
2628
"github.com/arduino/arduino-cli/commands"
2729
"github.com/arduino/arduino-cli/common/formatter"
2830
"github.com/arduino/arduino-cli/configs"
2931
"github.com/arduino/go-paths-helper"
30-
"github.com/cavaliercoder/grab"
3132
"github.com/sirupsen/logrus"
3233
"github.com/spf13/cobra"
3334
)
@@ -68,16 +69,14 @@ func updateIndex(URL *url.URL) {
6869
defer os.Remove(tmpFile.Name())
6970
tmpFile.Close()
7071

71-
req, err := grab.NewRequest(tmpFile.Name(), URL.String())
72+
d, err := downloader.Download(tmpFile.Name(), URL.String())
7273
if err != nil {
7374
formatter.PrintError(err, "Error downloading index "+URL.String())
7475
os.Exit(commands.ErrNetwork)
7576
}
76-
client := grab.NewClient()
77-
resp := client.Do(req)
78-
formatter.DownloadProgressBar(resp, "Updating index: "+coreIndexPath.Base())
79-
if resp.Err() != nil {
80-
formatter.PrintError(resp.Err(), "Error downloading index "+URL.String())
77+
formatter.DownloadProgressBar(d, "Updating index: "+coreIndexPath.Base())
78+
if d.Error() != nil {
79+
formatter.PrintError(d.Error(), "Error downloading index "+URL.String())
8180
os.Exit(commands.ErrNetwork)
8281
}
8382

commands/lib/download.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ func downloadLibrariesFromReferences(lm *librariesmanager.LibrariesManager, refs
7272
func downloadLibraries(lm *librariesmanager.LibrariesManager, libReleases []*librariesindex.Release) {
7373
logrus.Info("Downloading libraries")
7474
for _, libRelease := range libReleases {
75-
resp, err := libRelease.Resource.Download(lm.DownloadsDir)
75+
d, err := libRelease.Resource.Download(lm.DownloadsDir)
7676
if err != nil {
7777
formatter.PrintError(err, "Error downloading "+libRelease.String())
7878
os.Exit(commands.ErrNetwork)
7979
}
80-
formatter.DownloadProgressBar(resp, libRelease.String())
81-
if resp.Err() != nil {
82-
formatter.PrintError(err, "Error downloading "+libRelease.String())
80+
formatter.DownloadProgressBar(d, libRelease.String())
81+
if d.Error() != nil {
82+
formatter.PrintError(d.Error(), "Error downloading "+libRelease.String())
8383
os.Exit(commands.ErrNetwork)
8484
}
8585
}

common/formatter/download.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
package formatter
1919

2020
import (
21-
"github.com/cavaliercoder/grab"
21+
"go.bug.st/downloader"
2222
)
2323

2424
// DownloadProgressBar prints a progress bar from a running download Request
25-
func DownloadProgressBar(resp *grab.Response, prefix string) {
26-
defaultFormatter.DownloadProgressBar(resp, prefix)
25+
func DownloadProgressBar(d *downloader.Downloader, prefix string) {
26+
defaultFormatter.DownloadProgressBar(d, prefix)
2727
}

common/formatter/formatter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"errors"
2222
"fmt"
2323

24-
"github.com/cavaliercoder/grab"
2524
"github.com/sirupsen/logrus"
25+
"go.bug.st/downloader"
2626
)
2727

2828
// Formatter interface represents a generic formatter. It allows to print and format Messages.
@@ -31,7 +31,7 @@ type Formatter interface {
3131
Format(interface{}) (string, error)
3232

3333
// DownloadProgressBar outputs a progress bar if possible. Waits until the download ends.
34-
DownloadProgressBar(resp *grab.Response, prefix string)
34+
DownloadProgressBar(d *downloader.Downloader, prefix string)
3535
}
3636

3737
// PrintFunc represents a function used to print formatted data.

common/formatter/json.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323
"reflect"
2424

25-
"github.com/cavaliercoder/grab"
25+
"go.bug.st/downloader"
2626
)
2727

2828
// JSONFormatter is a Formatter that output JSON objects.
@@ -47,6 +47,6 @@ func (jf *JSONFormatter) Format(msg interface{}) (string, error) {
4747
}
4848

4949
// DownloadProgressBar implements Formatter interface
50-
func (jf *JSONFormatter) DownloadProgressBar(resp *grab.Response, prefix string) {
51-
resp.Wait()
50+
func (jf *JSONFormatter) DownloadProgressBar(d *downloader.Downloader, prefix string) {
51+
d.Run()
5252
}

common/formatter/text.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"fmt"
2323
"time"
2424

25-
"github.com/cavaliercoder/grab"
25+
"go.bug.st/downloader"
26+
2627
pb "gopkg.in/cheggaaa/pb.v1"
2728
)
2829

@@ -45,20 +46,16 @@ func (tp *TextFormatter) Format(msg interface{}) (string, error) {
4546
}
4647

4748
// DownloadProgressBar implements Formatter interface
48-
func (tp *TextFormatter) DownloadProgressBar(resp *grab.Response, prefix string) {
49+
func (tp *TextFormatter) DownloadProgressBar(d *downloader.Downloader, prefix string) {
4950
t := time.NewTicker(250 * time.Millisecond)
5051
defer t.Stop()
5152

52-
bar := pb.StartNew(int(resp.Size))
53+
bar := pb.StartNew(int(d.Size()))
5354
bar.SetUnits(pb.U_BYTES)
5455
bar.Prefix(prefix)
55-
for {
56-
select {
57-
case <-t.C:
58-
bar.Set(int(resp.BytesComplete()))
59-
case <-resp.Done:
60-
bar.FinishPrintOver(prefix + " downloaded")
61-
return
62-
}
56+
update := func(curr int64) {
57+
bar.Set(int(curr))
6358
}
59+
d.RunAndPoll(update, 250*time.Millisecond)
60+
bar.FinishPrintOver(prefix + " downloaded")
6461
}

vendor/github.com/cavaliercoder/grab/.travis.yml

-13
This file was deleted.

vendor/github.com/cavaliercoder/grab/LICENSE

-26
This file was deleted.

0 commit comments

Comments
 (0)