-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
490 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ linters: | |
- gofmt | ||
- gofumpt | ||
- nolintlint | ||
- tagliatelle | ||
presets: | ||
- bugs | ||
- comment | ||
|
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,7 @@ | ||
package contracts | ||
|
||
import "net/http" | ||
|
||
type HTTPClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} |
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
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,63 @@ | ||
//go:build release | ||
|
||
package ui | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/evg4b/uncors/internal/contracts" | ||
"github.com/evg4b/uncors/internal/log" | ||
version "github.com/hashicorp/go-version" | ||
) | ||
|
||
const lastVersionUrl = "https://api.github.com/repos/evg4b/uncors/releases/latest" | ||
|
||
type versionInfo struct { | ||
Version string `json:"tag_name"` | ||
} | ||
|
||
func CheckLastVersion(client contracts.HTTPClient, rewCurrectVersion string) { | ||
log.Debug("Checking new version") | ||
|
||
currectVersion, err := version.NewVersion(rewCurrectVersion) | ||
if err != nil { | ||
log.Debugf("failed to parse currect version: %v", err) | ||
|
||
return | ||
} | ||
|
||
url, _ := url.Parse(lastVersionUrl) | ||
response, err := client.Do(&http.Request{URL: url}) | ||
if err != nil { | ||
log.Debugf("http error ocupted: %v", err) | ||
|
||
return | ||
} | ||
|
||
defer response.Body.Close() | ||
decoder := json.NewDecoder(response.Body) | ||
|
||
lastVersionInfo := versionInfo{} | ||
err = decoder.Decode(&lastVersionInfo) | ||
if err != nil { | ||
log.Debugf("failed to parse last version respoce: %v", err) | ||
|
||
return | ||
} | ||
|
||
lastVersion, err := version.NewVersion(lastVersionInfo.Version) | ||
if err != nil { | ||
log.Debugf("failed to parse last version: %v", err) | ||
|
||
return | ||
} | ||
|
||
if lastVersion.GreaterThan(currectVersion) { | ||
log.Infof(NewVersionIsAvailable, currectVersion.String(), lastVersion.String()) | ||
log.Print("\n") | ||
} else { | ||
log.Debug("Version is up to date") | ||
} | ||
} |
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,12 @@ | ||
//go:build !release | ||
|
||
package ui | ||
|
||
import ( | ||
"github.com/evg4b/uncors/internal/contracts" | ||
"github.com/evg4b/uncors/internal/log" | ||
) | ||
|
||
func CheckLastVersion(client contracts.HTTPClient, rewCurrectVersion string) { | ||
log.Debug("Check new version stub") | ||
} |
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,109 @@ | ||
//go:build release | ||
|
||
package ui_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"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) { | ||
log.DisableColor() | ||
log.EnableDebugMessages() | ||
|
||
t.Run("do not panic where", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client contracts.HTTPClient | ||
version string | ||
expected string | ||
}{ | ||
{ | ||
name: "currect version is not correct", | ||
client: mocks.NewHttpClientMock(t), | ||
version: "#", | ||
expected: " DEBUG Checking new version\n DEBUG failed to parse currect version: Malformed version: #\n", | ||
}, | ||
{ | ||
name: "http error is occuped", | ||
client: mocks.NewHttpClientMock(t). | ||
DoMock.Return(nil, errors.New("some http error")), | ||
version: "0.0.3", | ||
expected: " DEBUG Checking new version\n DEBUG http error ocupted: some http error\n", | ||
}, | ||
{ | ||
name: "invalid json received", | ||
client: mocks.NewHttpClientMock(t). | ||
DoMock.Return(&http.Response{ | ||
Body: io.NopCloser(strings.NewReader(`{ "version"`)), | ||
}, nil), | ||
version: "0.0.3", | ||
expected: " DEBUG Checking new version\n DEBUG failed to parse last version respoce: unexpected EOF\n", | ||
}, | ||
{ | ||
name: "incorrect json from api received", | ||
client: mocks.NewHttpClientMock(t). | ||
DoMock.Return(&http.Response{ | ||
Body: io.NopCloser(strings.NewReader(`{ "tag_name": "#" }`)), | ||
}, nil), | ||
version: "0.0.3", | ||
expected: " DEBUG Checking new version\n DEBUG failed to parse last version: Malformed version: #\n", | ||
}, | ||
} | ||
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) | ||
|
||
outputData, err := ioutil.ReadAll(output) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, testCase.expected, string(outputData)) | ||
}) | ||
})) | ||
} | ||
}) | ||
|
||
t.Run("should print ", func(t *testing.T) { | ||
t.Run("prop1", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) { | ||
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") | ||
|
||
outputData, err := ioutil.ReadAll(output) | ||
testutils.CheckNoError(t, err) | ||
expected := ` DEBUG Checking new version | ||
INFO NEW VERSION IS Available! | ||
0.0.4 is not latest, you should upgrade to 0.0.7. | ||
See more information on https://github.com/evg4b/uncors/releases | ||
` | ||
assert.Equal(t, expected, string(outputData)) | ||
})) | ||
|
||
t.Run("prop2", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) { | ||
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") | ||
|
||
outputData, err := ioutil.ReadAll(output) | ||
testutils.CheckNoError(t, err) | ||
expected := " DEBUG Checking new version\n DEBUG Version is up to date\n" | ||
assert.Equal(t, expected, string(outputData)) | ||
})) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package ui | ||
|
||
var DisclamerMesssage = `DON'T USE IT FOR PRODUCTION! | ||
const DisclamerMesssage = `DON'T USE IT FOR PRODUCTION! | ||
This is a reverse proxy for use in testing or debugging web applications locally. | ||
It hasn't been reviewed for security issues.` | ||
|
||
const NewVersionIsAvailable = `NEW VERSION IS Available! | ||
%s is not latest, you should upgrade to %s. | ||
See more information on https://github.com/evg4b/uncors/releases | ||
` |
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
Oops, something went wrong.