Skip to content

Commit

Permalink
Pull getting latest GH release info to a separate function for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
amisevsk committed Jul 19, 2024
1 parent f874f24 commit 1181c4a
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions pkg/lib/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,13 @@ func CheckForUpdate(configHome string) {
if constants.Version == "unknown" || !versionTagRegexp.MatchString(constants.Version) {
return
}

if !shouldShowNotification(configHome) {
return
}

client := &http.Client{
Timeout: 1 * time.Second,
}
resp, err := client.Get(releaseUrl)
if err != nil {
output.Debugf("Failed to check for updates: %s", err)
return
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
info, err := getLatestReleaseInfo()
if err != nil {
output.Debugf("Failed to read GitHub response body: %s", err)
return
}
info := &ghReleaseInfo{}
if err := json.Unmarshal(respBody, info); err != nil {
output.Debugf("Failed to parse GitHub response body: %s", err)
output.Debugf("Error checking for CLI updates: %s", err)
return
}
if info.Prerelease || info.Draft {
Expand Down Expand Up @@ -131,3 +115,24 @@ func shouldShowNotification(configHome string) bool {
}
return false
}

func getLatestReleaseInfo() (*ghReleaseInfo, error) {
client := &http.Client{
Timeout: 1 * time.Second,
}
resp, err := client.Get(releaseUrl)
if err != nil {
return nil, fmt.Errorf("failed to check for updates: %w", err)
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read GitHub response body: %w", err)
}
info := &ghReleaseInfo{}
if err := json.Unmarshal(respBody, info); err != nil {
return nil, fmt.Errorf("failed to parse GitHub response body: %w", err)
}
return info, nil
}

0 comments on commit 1181c4a

Please sign in to comment.