Skip to content

Commit

Permalink
Added new version check
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 21, 2022
1 parent 2e2dc26 commit 4268734
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 6 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ linters:
- gofmt
- gofumpt
- nolintlint
- tagliatelle
presets:
- bugs
- comment
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/gojuno/minimock/v3 v3.0.10
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-version v1.6.0
github.com/pseidemann/finish v1.2.0
github.com/pterm/pterm v0.12.49
github.com/spf13/afero v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
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=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
7 changes: 7 additions & 0 deletions internal/contracts/http_client.go
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)
}
2 changes: 1 addition & 1 deletion internal/middlewares/mock/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mock
type Response struct {
Code int `yaml:"code"`
Headers map[string]string `yaml:"headers"`
RawContent string `yaml:"raw-content"` //nolint:tagliatelle
RawContent string `yaml:"raw-content"`
File string `yaml:"file"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/middlewares/proxy/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Middleware struct {
replacers contracts.URLReplacerFactory
http *http.Client
http contracts.HTTPClient
logger contracts.Logger
}

Expand Down
4 changes: 1 addition & 3 deletions internal/middlewares/proxy/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package proxy

import (
"net/http"

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

Expand All @@ -14,7 +12,7 @@ func WithURLReplacerFactory(replacerFactory contracts.URLReplacerFactory) Middle
}
}

func WithHTTPClient(http *http.Client) MiddlewareOption {
func WithHTTPClient(http contracts.HTTPClient) MiddlewareOption {
return func(m *Middleware) {
m.http = http
}
Expand Down
63 changes: 63 additions & 0 deletions internal/ui/last_version_check.go
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")
}
}
12 changes: 12 additions & 0 deletions internal/ui/last_version_check_debug.go
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")
}
109 changes: 109 additions & 0 deletions internal/ui/last_version_check_test.go
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))
}))
})
}
7 changes: 6 additions & 1 deletion internal/ui/messages.go
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
`
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func main() {
log.Info(ui.Mappings(mappings, mocksDefs))
log.Print("\n")

go ui.CheckLastVersion(httpClient, Version)

finisher.Wait()

log.Info("Server was stopped")
Expand Down
Loading

0 comments on commit 4268734

Please sign in to comment.