diff --git a/cmd/root.go b/cmd/root.go index 962149d73..5a0779bc3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ package cmd import ( + "encoding/json" "encoding/xml" "errors" "fmt" @@ -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 { @@ -82,13 +83,17 @@ 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 { @@ -96,19 +101,16 @@ func getRemoteVersion(req string) (string, 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 @@ -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()) diff --git a/cmd/root_test.go b/cmd/root_test.go index 838c525f2..fc49766ea 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -134,9 +134,9 @@ 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) @@ -144,12 +144,12 @@ func (suite *rootCmdSuite) TestGetRemoteVersionInvalidContainer() { } 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) diff --git a/common/version.go b/common/version.go index dd127af77..eb84790e2 100644 --- a/common/version.go +++ b/common/version.go @@ -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"