Make the progressbar take the image size into consideration

This commit is contained in:
Guillaume J. Charmes 2013-06-06 18:16:16 -07:00
parent e9ef3f43b7
commit deddb3c757

View file

@ -12,6 +12,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
) )
@ -106,40 +107,45 @@ func (r *Registry) getImagesInRepository(repository string, authConfig *auth.Aut
} }
// Retrieve an image from the Registry. // Retrieve an image from the Registry.
// Returns the Image object as well as the layer as an Archive (io.Reader) func (r *Registry) GetRemoteImageJSON(imgId, registry string, token []string) ([]byte, int, error) {
func (r *Registry) GetRemoteImageJSON(imgId, registry string, token []string) ([]byte, error) {
// Get the JSON // Get the JSON
req, err := http.NewRequest("GET", registry+"/images/"+imgId+"/json", nil) req, err := http.NewRequest("GET", registry+"/images/"+imgId+"/json", nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP code %d", res.StatusCode) return nil, -1, fmt.Errorf("HTTP code %d", res.StatusCode)
} }
imageSize, err := strconv.Atoi(res.Header.Get("X-Docker-Size"))
if err != nil {
return nil, -1, err
}
jsonString, err := ioutil.ReadAll(res.Body) jsonString, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to parse downloaded json: %s (%s)", err, jsonString) return nil, -1, fmt.Errorf("Failed to parse downloaded json: %s (%s)", err, jsonString)
} }
return jsonString, nil return jsonString, imageSize, nil
} }
func (r *Registry) GetRemoteImageLayer(imgId, registry string, token []string) (io.ReadCloser, int, error) { func (r *Registry) GetRemoteImageLayer(imgId, registry string, token []string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", registry+"/images/"+imgId+"/layer", nil) req, err := http.NewRequest("GET", registry+"/images/"+imgId+"/layer", nil)
if err != nil { if err != nil {
return nil, -1, fmt.Errorf("Error while getting from the server: %s\n", err) return nil, fmt.Errorf("Error while getting from the server: %s\n", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, -1, err return nil, err
} }
return res.Body, int(res.ContentLength), nil return res.Body, nil
} }
func (r *Registry) GetRemoteTags(registries []string, repository string, token []string) (map[string]string, error) { func (r *Registry) GetRemoteTags(registries []string, repository string, token []string) (map[string]string, error) {
@ -479,7 +485,7 @@ func NewRegistry(root string) *Registry {
httpTransport := &http.Transport{ httpTransport := &http.Transport{
DisableKeepAlives: true, DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
} }
r := &Registry{ r := &Registry{