Skip to content

Commit

Permalink
Use Github latest release for version checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
foodprocessor committed Oct 18, 2023
1 parent f7da2fa commit 2737c58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
32 changes: 17 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package cmd

import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand All @@ -41,9 +42,9 @@ import (
"github.com/spf13/cobra"
)

type VersionFilesList struct {
XMLName xml.Name `xml:"EnumerationResults"`
Blobs []Blob `xml:"Blobs>Blob"`
type GithubApiReleaseData struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
}

type Blob struct {
Expand Down Expand Up @@ -82,33 +83,34 @@ func checkVersionExists(versionUrl string) bool {
return resp.StatusCode != 404
}

// getRemoteVersion : From public container get the latest blobfuse version
// getRemoteVersion : From public release get the latest cloudfuse version
func getRemoteVersion(req string) (string, error) {
resp, err := http.Get(req)
if err != nil {
log.Err("getRemoteVersion: error listing version file from container [%s]", err.Error())
log.Err("getRemoteVersion: error getting release version from Github: [%s]", err.Error())
return "", err
}
if resp.StatusCode != 200 {
log.Err("getRemoteVersion: [got status %d from URL %s]", resp.StatusCode, req)
return "", fmt.Errorf("unable to get latest version: GET %s failed with status %d", req, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Err("getRemoteVersion: error reading body of response [%s]", err.Error())
return "", err
}

var versionList VersionFilesList
err = xml.Unmarshal(body, &versionList)
var releaseData GithubApiReleaseData
err = json.Unmarshal(body, &releaseData)
if err != nil {
log.Err("getRemoteVersion: error unmarshalling xml response [%s]", err.Error())
log.Err("getRemoteVersion: error parsing json response [%s]", err.Error())
return "", err
}

if len(versionList.Blobs) != 1 {
return "", fmt.Errorf("unable to get latest version")
}

versionName := strings.Split(versionList.Blobs[0].Name, "/")[1]
return versionName, nil
// trim the leading "v"
versionNumber := strings.TrimPrefix(releaseData.Name, "v")
return versionNumber, nil
}

// beginDetectNewVersion : Get latest release version and compare if user needs an upgrade or not
Expand All @@ -118,7 +120,7 @@ func beginDetectNewVersion() chan interface{} {
go func() {
defer close(completed)

latestVersionUrl := common.CloudfuseListContainerURL + "?restype=container&comp=list&prefix=latest/"
latestVersionUrl := common.CloudfuseReleaseURL + "/latest"
remoteVersion, err := getRemoteVersion(latestVersionUrl)
if err != nil {
log.Err("beginDetectNewVersion: error getting latest version [%s]", err.Error())
Expand Down
10 changes: 5 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ func (suite *rootCmdSuite) TestGetRemoteVersionInvalidURL() {
suite.assert.NotNil(err)
}

func (suite *rootCmdSuite) TestGetRemoteVersionInvalidContainer() {
func (suite *rootCmdSuite) TestGetRemoteVersionInvalidRelease() {
defer suite.cleanupTest()
latestVersionUrl := common.CloudfuseListContainerURL + "?restype=container&comp=list&prefix=latest1/"
latestVersionUrl := common.CloudfuseReleaseURL + "/latest1"
out, err := getRemoteVersion(latestVersionUrl)
suite.assert.Empty(out)
suite.assert.NotNil(err)
suite.assert.Contains(err.Error(), "unable to get latest version")
}

func getDummyVersion() string {
return "1.0.0"
return "0.0.0"
}

func (suite *rootCmdSuite) TestGetRemoteVersionValidContainer() {
func (suite *rootCmdSuite) TestGetRemoteVersionValidURL() {
defer suite.cleanupTest()
latestVersionUrl := common.CloudfuseListContainerURL + "?restype=container&comp=list&prefix=latest/"
latestVersionUrl := common.CloudfuseReleaseURL + "/latest"
out, err := getRemoteVersion(latestVersionUrl)
suite.assert.NotEmpty(out)
suite.assert.Nil(err)
Expand Down
1 change: 1 addition & 0 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
)

const CloudfuseReleaseURL = "https://api.github.com/repos/Seagate/cloudfuse/releases"
const CloudfuseListContainerURL = "https://blobfuse2.blob.core.windows.net/release"
const CloudfuseWarningsURL = "https://aka.ms/blobfuse2warnings"

Expand Down

0 comments on commit 2737c58

Please sign in to comment.