Merge lp:~sergiusens/snappy/splitDown into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Sergio Schvezov
Status: Merged
Approved by: John Lenton
Approved revision: 527
Merged at revision: 533
Proposed branch: lp:~sergiusens/snappy/splitDown
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 95 lines (+38/-21)
2 files modified
snappy/errors.go (+11/-0)
snappy/snapp.go (+27/-21)
To merge this branch: bzr merge lp:~sergiusens/snappy/splitDown
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+263294@code.launchpad.net

Commit message

Split package download logic and its generic bits

To post a comment you must log in.
Revision history for this message
John Lenton (chipaca) :
review: Approve
Revision history for this message
John Lenton (chipaca) wrote :

(oops, top apptoved by mistake -- but no harm done if it lands)

lp:~sergiusens/snappy/splitDown updated
526. By Sergio Schvezov

Better download code error reporting

527. By Sergio Schvezov

Merging trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/errors.go'
2--- snappy/errors.go 2015-06-29 23:16:31 +0000
3+++ snappy/errors.go 2015-06-30 12:31:47 +0000
4@@ -22,6 +22,7 @@
5 import (
6 "errors"
7 "fmt"
8+ "net/url"
9 "strings"
10
11 "launchpad.net/snappy/helpers"
12@@ -144,6 +145,16 @@
13 ErrNoSeccompPolicy = errors.New("no seccomp policy provided")
14 )
15
16+// ErrDownload represents a download error
17+type ErrDownload struct {
18+ code int
19+ url *url.URL
20+}
21+
22+func (e *ErrDownload) Error() string {
23+ return fmt.Sprintf("received an unexpected http response code (%v) when trying to download %s", e.code, e.url)
24+}
25+
26 // ErrArchitectureNotSupported is returned when trying to install a snappy package that
27 // is not supported on the system
28 type ErrArchitectureNotSupported struct {
29
30=== modified file 'snappy/snapp.go'
31--- snappy/snapp.go 2015-06-12 03:55:01 +0000
32+++ snappy/snapp.go 2015-06-30 12:31:47 +0000
33@@ -1432,9 +1432,34 @@
34 return p
35 }
36
37+// download writes an http.Request showing a progress.Meter
38+func download(name string, w io.Writer, req *http.Request, pbar progress.Meter) error {
39+ client := &http.Client{}
40+
41+ resp, err := client.Do(req)
42+ if err != nil {
43+ return err
44+ }
45+ defer resp.Body.Close()
46+
47+ if resp.StatusCode != 200 {
48+ return &ErrDownload{code: resp.StatusCode, url: req.URL}
49+ }
50+
51+ if pbar != nil {
52+ pbar.Start(name, float64(resp.ContentLength))
53+ mw := io.MultiWriter(w, pbar)
54+ _, err = io.Copy(mw, resp.Body)
55+ pbar.Finished()
56+ } else {
57+ _, err = io.Copy(w, resp.Body)
58+ }
59+
60+ return err
61+}
62+
63 // Download downloads the snap and returns the filename
64 func (s *RemoteSnapPart) Download(pbar progress.Meter) (string, error) {
65-
66 w, err := ioutil.TempFile("", s.pkg.Name)
67 if err != nil {
68 return "", err
69@@ -1456,26 +1481,7 @@
70 }
71 setUbuntuStoreHeaders(req)
72
73- client := &http.Client{}
74- resp, err := client.Do(req)
75- if err != nil {
76- return "", err
77- }
78- defer resp.Body.Close()
79- if resp.StatusCode != 200 {
80- return "", fmt.Errorf("Unexpected status code %v", resp.StatusCode)
81- }
82-
83- if pbar != nil {
84- pbar.Start(s.pkg.Name, float64(resp.ContentLength))
85- mw := io.MultiWriter(w, pbar)
86- _, err = io.Copy(mw, resp.Body)
87- pbar.Finished()
88- } else {
89- _, err = io.Copy(w, resp.Body)
90- }
91-
92- if err != nil {
93+ if err := download(s.Name(), w, req, pbar); err != nil {
94 return "", err
95 }
96

Subscribers

People subscribed via source and target branches