-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (WIP) support for version package
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const versionTimeout = time.Second * 1 | ||
|
||
// GetLatest returns the latest version if the current version is less than the latest version | ||
// For Example : | ||
// githubReleaseURL = "https://api.github.com/repos/odpf/optimus/releases/latest" | ||
// currVersion = 0.0.1 | ||
// the curVersion is the version of the binary installed in user system and the | ||
// githubReleaseURL will be used to extract the latest version from `json:"tag_name"` | ||
// suppose, the latestV is 0.0.2 which is greater than currentV, then | ||
// the GetLatest returns v0.0.2 | ||
func GetLatest(currVersion string, githubReleaseURL string) (string, error) { | ||
gitClient := http.Client{ | ||
Timeout: versionTimeout, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, githubReleaseURL, nil) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to create request for latest version") | ||
} | ||
req.Header.Set("User-Agent", "salt") | ||
resp, err := gitClient.Do(req) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get latest version from github") | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return "", errors.Wrapf(err, "failed to get latest version from github") | ||
} | ||
if resp.Body != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to read response body") | ||
} | ||
|
||
authorType := struct { | ||
TagName string `json:"tag_name"` | ||
}{} | ||
if err = json.Unmarshal(body, &authorType); err != nil { | ||
return "", errors.Wrapf(err, "failed to parse: %s", string(body)) | ||
} | ||
|
||
currentV, err := version.NewVersion(currVersion) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to parse current version") | ||
} | ||
latestV, err := version.NewVersion(authorType.TagName) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to parse latest version") | ||
} | ||
|
||
if currentV.LessThan(latestV) { | ||
return authorType.TagName, nil | ||
} | ||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TestGetLatest tests the GetLatest function. | ||
func TestGetLatest(t *testing.T) { | ||
t.Run("basic check for both valid and non-valid version ", func(t *testing.T) { | ||
// Test with a valid version. | ||
if _, err := GetLatest("1.0.0", "https://api.github.com/repos/odpf/optimus/releases/latest"); err != nil { | ||
t.Errorf("CheckLatestVersion(\"1.0.0\") = %v, want nil", err) | ||
} | ||
//Test with an invalid version. | ||
if _, err := GetLatest("1.0.0.0", ""); err == nil { | ||
t.Errorf("CheckLatestVersion(\"1.0.0.0\") = nil, want non-nil") | ||
} | ||
}) | ||
} |