Skip to content

Commit

Permalink
fix(versions): fix reading cache file (#53)
Browse files Browse the repository at this point in the history
Fix reading cache file.
Save one using link on update cache.
  • Loading branch information
mariiatuzovska committed Jan 18, 2023
1 parent b619be7 commit fb2da14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .changes/unreleased/🐛 Bug Fix-20230118-161022.yaml
@@ -0,0 +1,10 @@
kind: "\U0001F41B Bug Fix"
body:
Fix reading cache file for checking cli versions. Save just one using link on
update.
time: 2023-01-18T16:10:22.406877401+02:00
custom:
azure-boards-workitemid-fixed: '482447'
azure-boards-workitemid-related: '482427'
github-contributor: ''
github-link: ''
22 changes: 14 additions & 8 deletions version/update.go
Expand Up @@ -41,26 +41,31 @@ func CheckLatestVersion() (*update, error) {
return nil, err
}
cacheFilePath := filepath.Join(thyDir, cacheFileName)

platform := runtime.GOOS + "/" + runtime.GOARCH
latest := readCache(cacheFilePath)

if latest == nil {
pageContent, err := fetchContent(versionsURL)
if err != nil {
return nil, err
}

latest = &latestInfo{}
err = json.Unmarshal(pageContent, latest)
if err != nil {
return nil, err
}

actualLink, ok := latest.Links[platform]
if !ok {
return nil, errors.New("links malformed")
}
latest.Links = map[string]string{platform: actualLink}
pageContent, err = json.Marshal(latest)
if err != nil {
return nil, err
}
updateCache(cacheFilePath, pageContent)
}

if isVersionOutdated(Version, latest.Latest) {
actualLink, ok := latest.Links[runtime.GOOS+"/"+runtime.GOARCH]
actualLink, ok := latest.Links[platform]
if !ok {
return nil, errors.New("links malformed")
}
Expand All @@ -82,8 +87,9 @@ func readCache(updateFilePath string) *latestInfo {
return nil
}

contents := strings.Split(string(fileContent), "\n")
if len(contents) < 2 {
const fileParts int = 2
contents := strings.SplitN(string(fileContent), "\n", fileParts)
if len(contents) < fileParts {
log.Printf("Wrong file %s format", updateFilePath)
return nil
}
Expand Down

0 comments on commit fb2da14

Please sign in to comment.