Skip to content

Commit

Permalink
feat: (WIP) support for version package
Browse files Browse the repository at this point in the history
  • Loading branch information
scortier committed Aug 30, 2021
1 parent 4ff2e6b commit 0aff91d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/odpf/salt
go 1.16

require (
github.com/go-resty/resty/v2 v2.6.0
github.com/hashicorp/go-version v1.3.0
github.com/jeremywohl/flatten v1.0.1
github.com/mcuadros/go-defaults v1.2.0
github.com/mitchellh/mapstructure v1.4.1
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-resty/resty/v2 v2.6.0 h1:joIR5PNLM2EFqqESUjCMGXrWmXNHEU9CEiK813oKYS4=
github.com/go-resty/resty/v2 v2.6.0/go.mod h1:PwvJS6hvaPkjtjNg9ph+VrSD92bi5Zq73w/BIH7cC3Q=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -152,6 +154,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -345,6 +349,7 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
69 changes: 69 additions & 0 deletions version/check.go
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
}
19 changes: 19 additions & 0 deletions version/check_test.go
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")
}
})
}

0 comments on commit 0aff91d

Please sign in to comment.