Skip to content

Commit 9b5d2a0

Browse files
committed
decoupled progBar from download task, label removed (first try)
1 parent 9072ea9 commit 9b5d2a0

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

common/net_functions.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import (
3636
"io/ioutil"
3737
"net/http"
3838
"os"
39-
40-
pb "gopkg.in/cheggaaa/pb.v1"
39+
"time"
4140
)
4241

4342
// DownloadIndex is a function to download a generic index.
@@ -72,17 +71,13 @@ func DownloadIndex(indexPathFunc func() (string, error), URL string) error {
7271
}
7372

7473
// DownloadPackage downloads a package from arduino repository, applying a label for the progress bar.
75-
func DownloadPackage(URL string, downloadLabel string, progressBar *pb.ProgressBar, initialData *os.File, totalSize int64) error {
76-
client := http.DefaultClient
77-
74+
func DownloadPackage(URL string, initialData *os.File, totalSize int64, handleResultFunc func(io.Reader, int) error) error {
7875
if initialData == nil {
7976
return errors.New("Cannot fill a nil file pointer")
8077
}
8178

82-
request, err := http.NewRequest("GET", URL, nil)
83-
if err != nil {
84-
return fmt.Errorf("Cannot create HTTP request: %s", err)
85-
}
79+
client := http.DefaultClient
80+
client.Timeout = time.Minute
8681

8782
var initialSize int64
8883
stats, err := initialData.Stat()
@@ -97,31 +92,34 @@ func DownloadPackage(URL string, downloadLabel string, progressBar *pb.ProgressB
9792
}
9893
}
9994

95+
request, err := http.NewRequest("GET", URL, nil)
96+
if err != nil {
97+
return fmt.Errorf("Cannot create HTTP request: %s", err)
98+
}
99+
100100
if initialSize > 0 {
101101
request.Header.Add("Range", fmt.Sprintf("bytes=%d-", initialSize))
102102
}
103103

104104
response, err := client.Do(request)
105-
106105
if err != nil {
107-
return fmt.Errorf("Cannot fetch %s. Response creation error", downloadLabel)
106+
return fmt.Errorf("Cannot fetch %s. Response creation error", URL)
108107
} else if response.StatusCode != 200 &&
109108
response.StatusCode != 206 &&
110109
response.StatusCode != 416 {
111110
response.Body.Close()
112-
return fmt.Errorf("Cannot fetch %s. Source responded with a status %d code", downloadLabel, response.StatusCode)
111+
return fmt.Errorf("Cannot fetch %s. Source responded with code %d",
112+
URL, response.StatusCode)
113113
}
114114
defer response.Body.Close()
115115

116-
source := response.Body
117-
if progressBar != nil {
118-
progressBar.Add(int(initialSize))
119-
source = progressBar.NewProxyReader(response.Body)
116+
if handleResultFunc == nil {
117+
_, err = io.Copy(initialData, response.Body)
118+
} else {
119+
err = handleResultFunc(response.Body, int(initialSize))
120120
}
121-
122-
_, err = io.Copy(initialData, source)
123121
if err != nil {
124-
return fmt.Errorf("Cannot read response body %s", err)
122+
return fmt.Errorf("Cannot read response body from %s : %s", URL, err)
125123
}
126124
return nil
127125
}

common/releases/helpers.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package releases
3232
import (
3333
"errors"
3434
"fmt"
35+
"io"
3536
"os"
3637
"path/filepath"
3738
"strings"
@@ -84,25 +85,37 @@ func ParseArgs(args []string) []NameVersionPair {
8485
// label -> Name used to identify the type of the Item downloaded (library, core, tool)
8586
// RETURNS:
8687
// error if any
87-
func downloadRelease(item DownloadItem, progBar *pb.ProgressBar, label string) error {
88+
func downloadRelease(item DownloadItem, progBar *pb.ProgressBar, label string) (io.Reader, error) {
8889
if item.Release == nil {
89-
return errors.New("Cannot accept nil release")
90+
return nil, errors.New("Cannot accept nil release")
9091
}
9192

9293
initialData, err := item.Release.OpenLocalArchiveForDownload()
9394
if err != nil {
94-
return fmt.Errorf("Cannot get Archive file of this release : %s", err)
95+
return nil, fmt.Errorf("Cannot get Archive file of this release : %s", err)
9596
}
9697
defer initialData.Close()
97-
err = common.DownloadPackage(item.Release.ArchiveURL(), fmt.Sprint(label, " ", item.Name), progBar, initialData, item.Release.ArchiveSize())
98+
// puts the progress bar
99+
err = common.DownloadPackage(item.Release.ArchiveURL(), initialData, item.Release.ArchiveSize(), func(source io.Reader, initialSize int) error {
100+
if progBar != nil {
101+
progBar.Add(int(initialSize))
102+
source = progBar.NewProxyReader(source)
103+
}
104+
105+
_, err = io.Copy(initialData, source)
106+
if err != nil {
107+
return fmt.Errorf("Cannot read response body %s", err)
108+
}
109+
return nil
110+
})
98111
if err != nil {
99-
return err
112+
return nil, err
100113
}
101114
err = checkLocalArchive(item.Release)
102115
if err != nil {
103-
return errors.New("Archive has been downloaded, but it seems corrupted. Try again to redownload it")
116+
return nil, errors.New("Archive has been downloaded, but it seems corrupted. Try again to redownload it")
104117
}
105-
return nil
118+
return reader, nil
106119
}
107120

108121
// downloadAndCache returns the wrapper to download something without installing it

cores/cores.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func (release Release) OpenLocalArchiveForDownload() (*os.File, error) {
139139
stats, err := os.Stat(path)
140140
if os.IsNotExist(err) || err == nil && stats.Size() >= release.Size {
141141
return os.Create(path)
142+
} else if err != nil {
143+
return nil, err
142144
}
143145
return os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
144146
}

cores/package_manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ package cores
3131

3232
//Package represents a package in the system.
3333
type Package struct {
34-
Name string
35-
Maintainer string
36-
WebsiteURL string
37-
Email string
34+
Name string // Name of the package.
35+
Maintainer string // Name of the maintainer.
36+
WebsiteURL string // Website of maintainer.
37+
Email string // Email of maintainer.
3838
Cores map[string]*Core // The cores in the system.
3939
Tools map[string]*Tool // The tools in the system.
4040
}

libraries/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type Index struct {
4343

4444
// indexRelease is an entry of a library_index.json
4545
type indexRelease struct {
46-
Name string `json:"name"`
47-
Version string `json:"version"`
46+
Name string `json:"name,required"`
47+
Version string `json:"version,required"`
4848
Author string `json:"author"`
4949
Maintainer string `json:"maintainer"`
5050
Sentence string `json:"sentence"`

0 commit comments

Comments
 (0)