@@ -57,12 +57,12 @@ const updatesUrl = "https://register.cockroachdb.com/api/updates"
5757
5858var muslRE = regexp .MustCompile (`(?i)\bmusl\b` )
5959
60- // GetDownloadResponse return the http response of a CRDB download.
61- // It creates the url for downloading a CRDB binary for current runtime OS,
62- // makes a request to this url, and return the response.
63- // nonStable should only be used if desiredVersion is not specified . If nonStable
64- // is used , the latest cockroach binary will be used.
65- func GetDownloadResponse (desiredVersion string , nonStable bool ) (* http. Response , string , error ) {
60+ // GetDownloadURL returns the URL of a CRDB download. It creates the URL for
61+ // downloading a CRDB binary for current runtime OS. If desiredVersion is
62+ // specified, it will return the URL of the specified version. Otherwise, it
63+ // will return the URL of the latest stable cockroach binary . If nonStable is
64+ // true , the latest cockroach binary will be used.
65+ func GetDownloadURL (desiredVersion string , nonStable bool ) (string , string , error ) {
6666 goos := runtime .GOOS
6767 if goos == "linux" {
6868 goos += func () string {
@@ -100,21 +100,31 @@ func GetDownloadResponse(desiredVersion string, nonStable bool) (*http.Response,
100100 // For the latest stable CRDB, we use the url provided in the CRDB release page.
101101 dbUrl , desiredVersion , err = getLatestStableVersionInfo ()
102102 if err != nil {
103- return nil , "" , err
103+ return dbUrl , "" , err
104104 }
105105 }
106106
107- log .Printf ("GET %s" , dbUrl )
108- response , err := http .Get (dbUrl )
107+ return dbUrl , desiredVersion , nil
108+ }
109+
110+ // DownloadFromURL starts a download of the cockroach binary from the given URL.
111+ func DownloadFromURL (downloadURL string ) (* http.Response , error ) {
112+ log .Printf ("GET %s" , downloadURL )
113+ response , err := http .Get (downloadURL )
109114 if err != nil {
110- return nil , "" , err
115+ return nil , err
111116 }
112117
113118 if response .StatusCode != 200 {
114- return nil , "" , fmt .Errorf ("error downloading %s: %d (%s)" , dbUrl ,
115- response .StatusCode , response .Status )
119+ return nil , fmt .Errorf (
120+ "error downloading %s: %d (%s)" ,
121+ downloadURL ,
122+ response .StatusCode ,
123+ response .Status ,
124+ )
116125 }
117- return response , desiredVersion , nil
126+
127+ return response , nil
118128}
119129
120130// DownloadBinary saves the latest version of CRDB into a local binary file,
@@ -124,19 +134,30 @@ func GetDownloadResponse(desiredVersion string, nonStable bool) (*http.Response,
124134// To download the latest STABLE version of CRDB, set `nonStable` to false.
125135// To download the bleeding edge version of CRDB, set `nonStable` to true.
126136func DownloadBinary (tc * TestConfig , desiredVersion string , nonStable bool ) (string , error ) {
127- response , desiredVersion , err := GetDownloadResponse (desiredVersion , nonStable )
137+ dbUrl , desiredVersion , err := GetDownloadURL (desiredVersion , nonStable )
128138 if err != nil {
129139 return "" , err
130140 }
131141
132- defer func () { _ = response .Body .Close () }()
142+ filename , err := GetDownloadFilename (desiredVersion )
143+ if err != nil {
144+ return "" , err
145+ }
146+ localFile := filepath .Join (os .TempDir (), filename )
147+
148+ // Short circuit if the file already exists and is in the finished state.
149+ info , err := os .Stat (localFile )
150+ if err == nil && info .Mode ().Perm () == finishedFileMode {
151+ return localFile , nil
152+ }
133153
134- filename , err := GetDownloadFilename ( response , nonStable , desiredVersion )
154+ response , err := DownloadFromURL ( dbUrl )
135155 if err != nil {
136156 return "" , err
137157 }
138158
139- localFile := filepath .Join (os .TempDir (), filename )
159+ defer func () { _ = response .Body .Close () }()
160+
140161 for {
141162 info , err := os .Stat (localFile )
142163 if os .IsNotExist (err ) {
@@ -229,7 +250,7 @@ func DownloadBinary(tc *TestConfig, desiredVersion string, nonStable bool) (stri
229250
230251// GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
231252func GetDownloadFilename (
232- response * http. Response , nonStableDB bool , desiredVersion string ,
253+ desiredVersion string ,
233254) (string , error ) {
234255 filename := fmt .Sprintf ("cockroach-%s" , desiredVersion )
235256 if runtime .GOOS == "windows" {
0 commit comments