Skip to content

Commit

Permalink
Moved new version check login in separated package
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Apr 2, 2023
1 parent 0595bdb commit d70a1f6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
//go:build release

package ui
package version

import (
"context"
"encoding/json"
"net/http"
"net/url"

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/log"
version "github.com/hashicorp/go-version"
"github.com/evg4b/uncors/internal/ui"
"github.com/hashicorp/go-version"
)

const lastVersionUrl = "https://api.github.com/repos/evg4b/uncors/releases/latest"
const lastVersionURL = "https://api.github.com/repos/evg4b/uncors/releases/latest"

type versionInfo struct {
Version string `json:"tag_name"`
}

func CheckLastVersion(client contracts.HTTPClient, rawCurrentVersion string) {
func CheckNewVersion(ctx context.Context, client contracts.HTTPClient, rawCurrentVersion string) {
log.Debug("Checking new version")

currentVersion, err := version.NewVersion(rawCurrentVersion)
Expand All @@ -28,8 +29,14 @@ func CheckLastVersion(client contracts.HTTPClient, rawCurrentVersion string) {
return
}

url, _ := url.Parse(lastVersionUrl)
response, err := client.Do(&http.Request{URL: url})
request, err := http.NewRequestWithContext(ctx, http.MethodGet, lastVersionURL, nil)
if err != nil {
log.Debugf("failed to generate new version check request: %v", err)

return
}

response, err := client.Do(request)
if err != nil {
log.Debugf("http error occupied: %v", err)

Expand All @@ -55,8 +62,8 @@ func CheckLastVersion(client contracts.HTTPClient, rawCurrentVersion string) {
}

if lastVersion.GreaterThan(currentVersion) {
log.Infof(NewVersionIsAvailable, currentVersion.String(), lastVersion.String())
log.Print("\n")
log.Infof(ui.NewVersionIsAvailable, currentVersion.String(), lastVersion.String())
log.Info("\n")
} else {
log.Debug("Version is up to date")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//go:build !release

package ui
package version

import (
"context"

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/log"
)

func CheckLastVersion(_ contracts.HTTPClient, _ string) {
func CheckNewVersion(_ context.Context, _ contracts.HTTPClient, _ string) {
log.Debug("Check new version stub")
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//go:build release

package ui_test
package version_test

import (
"bytes"
"errors"
"github.com/evg4b/uncors/internal/version"
"io"
"io/ioutil"
"net/http"
Expand All @@ -13,13 +14,12 @@ import (

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/log"
"github.com/evg4b/uncors/internal/ui"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)

func TestCheckLastVersion(t *testing.T) {
func TestCheckNewVersion(t *testing.T) {
log.DisableColor()
log.EnableDebugMessages()

Expand Down Expand Up @@ -65,7 +65,7 @@ func TestCheckLastVersion(t *testing.T) {
for _, testCase := range tests {
t.Run(testCase.name, testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
assert.NotPanics(t, func() {
ui.CheckLastVersion(testCase.client, testCase.version)
version.CheckNewVersion(testCase.client, testCase.version)

outputData, err := ioutil.ReadAll(output)
testutils.CheckNoError(t, err)
Expand All @@ -81,7 +81,7 @@ func TestCheckLastVersion(t *testing.T) {
httpClient := mocks.NewHttpClientMock(t).
DoMock.Return(&http.Response{Body: io.NopCloser(strings.NewReader(`{ "tag_name": "0.0.7" }`))}, nil)

ui.CheckLastVersion(httpClient, "0.0.4")
version.CheckNewVersion(httpClient, "0.0.4")

outputData, err := ioutil.ReadAll(output)
testutils.CheckNoError(t, err)
Expand All @@ -98,7 +98,7 @@ func TestCheckLastVersion(t *testing.T) {
httpClient := mocks.NewHttpClientMock(t).
DoMock.Return(&http.Response{Body: io.NopCloser(strings.NewReader(`{ "tag_name": "0.0.7" }`))}, nil)

ui.CheckLastVersion(httpClient, "0.0.7")
version.CheckNewVersion(httpClient, "0.0.7")

outputData, err := ioutil.ReadAll(output)
testutils.CheckNoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strconv"

"github.com/evg4b/uncors/internal/version"

"github.com/evg4b/uncors/internal/server"
"golang.org/x/net/context"

Expand Down Expand Up @@ -123,7 +125,7 @@ func main() {
log.Info(ui.Mappings(mappings, config.Mocks))
log.Print("\n")

go ui.CheckLastVersion(httpClient, Version)
go version.CheckNewVersion(ctx, httpClient, Version)

finisher.Wait()

Expand Down

0 comments on commit d70a1f6

Please sign in to comment.