Skip to content

Commit

Permalink
version: use inmemory.Download to fetch release-info.json
Browse files Browse the repository at this point in the history
  • Loading branch information
anjannath committed Jun 2, 2023
1 parent 4308f3f commit c627d21
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions pkg/crc/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"net/http"
"os"
"path/filepath"
"time"

"github.com/Masterminds/semver/v3"
"github.com/crc-org/crc/pkg/crc/logging"
crcPreset "github.com/crc-org/crc/pkg/crc/preset"
"github.com/crc-org/crc/pkg/download/inmemory"
)

// The following variables are private fields and should be set when compiling with ldflags, for example --ldflags="-X github.com/crc-org/crc/pkg/version.crcVersion=vX.Y.Z
Expand Down Expand Up @@ -111,26 +111,13 @@ func InstallPath() string {
}

func GetCRCLatestVersionFromMirror(transport http.RoundTripper) (*CrcReleaseInfo, error) {
client := &http.Client{
Timeout: 5 * time.Second,
Transport: transport,
}
req, err := http.NewRequest(http.MethodGet, releaseInfoLink, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("crc/%s", crcVersion))
response, err := client.Do(req)
response, err := inmemory.Download(releaseInfoLink, UserAgent(), transport)
if err != nil {
return nil, err
}
defer response.Body.Close()
defer response.Close()

if response.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error: %s: %d", response.Status, response.StatusCode)
}

releaseMetaData, err := io.ReadAll(response.Body)
releaseMetaData, err := io.ReadAll(response)
if err != nil {
return nil, err
}
Expand All @@ -151,3 +138,7 @@ func GetDefaultPreset() crcPreset.Preset {
}
return preset
}

func UserAgent() string {
return fmt.Sprintf("crc/%s", crcVersion)
}

0 comments on commit c627d21

Please sign in to comment.