@@ -62,7 +62,9 @@ var muslRE = regexp.MustCompile(`(?i)\bmusl\b`)
6262// GetDownloadResponse return the http response of a CRDB download.
6363// It creates the url for downloading a CRDB binary for current runtime OS,
6464// makes a request to this url, and return the response.
65- func GetDownloadResponse (nonStable bool ) (* http.Response , string , error ) {
65+ // nonStable should only be used if desiredVersion is not specified. If nonStable
66+ // is used, the latest cockroach binary will be used.
67+ func GetDownloadResponse (desiredVersion string , nonStable bool ) (* http.Response , string , error ) {
6668 goos := runtime .GOOS
6769 if goos == "linux" {
6870 goos += func () string {
@@ -86,9 +88,10 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
8688 var dbUrl string
8789 var err error
8890
89- latestStableVersion := ""
90- // For the latest (beta) CRDB, we use the `edge-binaries.cockroachdb.com` host.
91- if nonStable {
91+ if desiredVersion != "" {
92+ dbUrl = getDownloadUrlForVersion (desiredVersion )
93+ } else if nonStable {
94+ // For the latest (beta) CRDB, we use the `edge-binaries.cockroachdb.com` host.
9295 u := & url.URL {
9396 Scheme : "https" ,
9497 Host : "edge-binaries.cockroachdb.com" ,
@@ -97,7 +100,7 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
97100 dbUrl = u .String ()
98101 } else {
99102 // For the latest stable CRDB, we use the url provided in the CRDB release page.
100- dbUrl , latestStableVersion , err = getLatestStableVersionInfo ()
103+ dbUrl , desiredVersion , err = getLatestStableVersionInfo ()
101104 if err != nil {
102105 return nil , "" , err
103106 }
@@ -113,22 +116,24 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
113116 return nil , "" , fmt .Errorf ("error downloading %s: %d (%s)" , dbUrl ,
114117 response .StatusCode , response .Status )
115118 }
116- return response , latestStableVersion , nil
119+ return response , desiredVersion , nil
117120}
118121
119122// downloadBinary saves the latest version of CRDB into a local binary file,
120123// and returns the path for this local binary.
124+ // To download a specific cockroach version, specify desiredVersion. Otherwise,
125+ // the latest stable or non-stable version will be chosen.
121126// To download the latest STABLE version of CRDB, set `nonStable` to false.
122127// To download the bleeding edge version of CRDB, set `nonStable` to true.
123- func downloadBinary (tc * TestConfig , nonStable bool ) (string , error ) {
124- response , latestStableVersion , err := GetDownloadResponse (nonStable )
128+ func downloadBinary (tc * TestConfig , desiredVersion string , nonStable bool ) (string , error ) {
129+ response , desiredVersion , err := GetDownloadResponse (desiredVersion , nonStable )
125130 if err != nil {
126131 return "" , err
127132 }
128133
129134 defer func () { _ = response .Body .Close () }()
130135
131- filename , err := GetDownloadFilename (response , nonStable , latestStableVersion )
136+ filename , err := GetDownloadFilename (response , nonStable , desiredVersion )
132137 if err != nil {
133138 return "" , err
134139 }
@@ -226,7 +231,7 @@ func downloadBinary(tc *TestConfig, nonStable bool) (string, error) {
226231
227232// GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
228233func GetDownloadFilename (
229- response * http.Response , nonStableDB bool , latestStableVersion string ,
234+ response * http.Response , nonStableDB bool , desiredVersion string ,
230235) (string , error ) {
231236 if nonStableDB {
232237 const contentDisposition = "Content-Disposition"
@@ -240,7 +245,7 @@ func GetDownloadFilename(
240245 }
241246 return filename , nil
242247 }
243- filename := fmt .Sprintf ("cockroach-%s" , latestStableVersion )
248+ filename := fmt .Sprintf ("cockroach-%s" , desiredVersion )
244249 if runtime .GOOS == "windows" {
245250 filename += ".exe"
246251 }
@@ -264,17 +269,24 @@ func getLatestStableVersionInfo() (string, string, error) {
264269 if ! ok {
265270 return "" , "" , fmt .Errorf ("api/updates response is of wrong format" )
266271 }
267- var downloadUrl string
272+
273+ downloadUrl := getDownloadUrlForVersion (latestStableVersion )
274+
275+ latestStableVerFormatted := strings .ReplaceAll (latestStableVersion , "." , "-" )
276+ return downloadUrl , latestStableVerFormatted , nil
277+ }
278+
279+ func getDownloadUrlForVersion (version string ) string {
268280 switch runtime .GOOS {
269281 case "linux" :
270- downloadUrl = fmt .Sprintf (linuxUrlpat , latestStableVersion )
282+ return fmt .Sprintf (linuxUrlpat , version )
271283 case "darwin" :
272- downloadUrl = fmt .Sprintf (macUrlpat , latestStableVersion )
284+ return fmt .Sprintf (macUrlpat , version )
273285 case "windows" :
274- downloadUrl = fmt .Sprintf (winUrlpat , latestStableVersion )
286+ return fmt .Sprintf (winUrlpat , version )
275287 }
276- latestStableVerFormatted := strings . ReplaceAll ( latestStableVersion , "." , "-" )
277- return downloadUrl , latestStableVerFormatted , nil
288+
289+ panic ( errors . New ( "could not get supported go os version" ))
278290}
279291
280292// downloadBinaryFromResponse copies the http response's body directly into a local binary.
0 commit comments