@@ -32,51 +32,39 @@ type ContentProvider interface {
32
32
}
33
33
34
34
// urlTo builds the URL to download a file from the remote hub.
35
- func (d * Downloader ) urlTo (remotePath string ) (string , error ) {
35
+ func (d * Downloader ) urlTo (remotePath string ) (* url. URL , error ) {
36
36
// the template must contain two string placeholders
37
37
if fmt .Sprintf (d .URLTemplate , "%s" , "%s" ) != d .URLTemplate {
38
- return "" , fmt .Errorf ("invalid URL template '%s'" , d .URLTemplate )
38
+ return nil , fmt .Errorf ("invalid URL template '%s'" , d .URLTemplate )
39
39
}
40
40
41
- return fmt .Sprintf (d .URLTemplate , d .Branch , remotePath ), nil
42
- }
41
+ raw := fmt .Sprintf (d .URLTemplate , d .Branch , remotePath )
43
42
44
- // addURLParam adds a parameter with a value (ex. "with_content=true") to the URL if it's not already present.
45
- func addURLParam (rawURL string , param string , value string ) (string , error ) {
46
- parsedURL , err := url .Parse (rawURL )
43
+ parsed , err := url .Parse (raw )
47
44
if err != nil {
48
- return "" , fmt .Errorf ("failed to parse URL: %w" , err )
45
+ return nil , fmt .Errorf ("failed to parse URL: %w" , err )
49
46
}
50
47
51
- query := parsedURL .Query ()
52
-
53
- if _ , exists := query [param ]; ! exists {
54
- query .Add (param , value )
55
- }
56
-
57
- parsedURL .RawQuery = query .Encode ()
58
-
59
- return parsedURL .String (), nil
48
+ return parsed , nil
60
49
}
61
50
62
51
// FetchIndex downloads the index from the hub and writes it to the filesystem.
63
52
// It uses a temporary file to avoid partial downloads, and won't overwrite the original
64
53
// if it has not changed.
65
54
// Return true if the file has been updated, false if already up to date.
66
- func (d * Downloader ) FetchIndex (ctx context.Context , destPath string , withContent bool , logger * logrus.Logger ) (bool , error ) {
55
+ func (d * Downloader ) FetchIndex (ctx context.Context , destPath string , withContent bool , logger * logrus.Logger ) (downloaded bool , err error ) {
67
56
url , err := d .urlTo (".index.json" )
68
57
if err != nil {
69
58
return false , fmt .Errorf ("failed to build hub index request: %w" , err )
70
59
}
71
60
72
61
if withContent {
73
- url , err = addURLParam (url , "with_content" , "true" )
74
- if err != nil {
75
- return false , fmt .Errorf ("failed to add 'with_content' parameter to URL: %w" , err )
76
- }
62
+ q := url .Query ()
63
+ q .Set ("with_content" , "true" )
64
+ url .RawQuery = q .Encode ()
77
65
}
78
66
79
- downloaded , err : = downloader .
67
+ downloaded , err = downloader .
80
68
New ().
81
69
WithHTTPClient (HubClient ).
82
70
ToFile (destPath ).
@@ -86,7 +74,7 @@ func (d *Downloader) FetchIndex(ctx context.Context, destPath string, withConten
86
74
BeforeRequest (func (_ * http.Request ) {
87
75
fmt .Println ("Downloading " + destPath )
88
76
}).
89
- Download (ctx , url )
77
+ Download (ctx , url . String () )
90
78
if err != nil {
91
79
return false , err
92
80
}
@@ -97,13 +85,13 @@ func (d *Downloader) FetchIndex(ctx context.Context, destPath string, withConten
97
85
// FetchContent downloads the content to the specified path, through a temporary file
98
86
// to avoid partial downloads.
99
87
// If the hash does not match, it will not overwrite and log a warning.
100
- func (d * Downloader ) FetchContent (ctx context.Context , remotePath , destPath , wantHash string , logger * logrus.Logger ) (bool , string , error ) {
101
- url , err := d .urlTo (remotePath )
88
+ func (d * Downloader ) FetchContent (ctx context.Context , remotePath , destPath , wantHash string , logger * logrus.Logger ) (downloaded bool , url string , err error ) {
89
+ u , err := d .urlTo (remotePath )
102
90
if err != nil {
103
91
return false , "" , fmt .Errorf ("failed to build request: %w" , err )
104
92
}
105
93
106
- downloaded , err : = downloader .
94
+ downloaded , err = downloader .
107
95
New ().
108
96
WithHTTPClient (HubClient ).
109
97
ToFile (destPath ).
@@ -112,7 +100,7 @@ func (d *Downloader) FetchContent(ctx context.Context, remotePath, destPath, wan
112
100
WithLogger (logger .WithField ("url" , url )).
113
101
CompareContent ().
114
102
VerifyHash ("sha256" , wantHash ).
115
- Download (ctx , url )
103
+ Download (ctx , u . String () )
116
104
117
105
var hasherr downloader.HashMismatchError
118
106
@@ -123,5 +111,5 @@ func (d *Downloader) FetchContent(ctx context.Context, remotePath, destPath, wan
123
111
return false , "" , err
124
112
}
125
113
126
- return downloaded , url , nil
114
+ return downloaded , u . String () , nil
127
115
}
0 commit comments