chore(all): return concrete types, accept interfaces

- Remove exported interfaces unused locally
- Define interfaces to accept arguments
- Return concrete types, not interfaces
This commit is contained in:
Quentin McGaw
2022-06-11 01:34:30 +00:00
parent 0378fe4a7b
commit 578ef768ab
132 changed files with 594 additions and 935 deletions

View File

@@ -1,5 +1,5 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/qdm12/gluetun/internal/provider/common (interfaces: ParallelResolver,Storage)
// Source: github.com/qdm12/gluetun/internal/provider/common (interfaces: ParallelResolver,Storage,Unzipper)
// Package common is a generated GoMock package.
package common
@@ -105,3 +105,41 @@ func (mr *MockStorageMockRecorder) GetServerByName(arg0, arg1 interface{}) *gomo
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetServerByName", reflect.TypeOf((*MockStorage)(nil).GetServerByName), arg0, arg1)
}
// MockUnzipper is a mock of Unzipper interface.
type MockUnzipper struct {
ctrl *gomock.Controller
recorder *MockUnzipperMockRecorder
}
// MockUnzipperMockRecorder is the mock recorder for MockUnzipper.
type MockUnzipperMockRecorder struct {
mock *MockUnzipper
}
// NewMockUnzipper creates a new mock instance.
func NewMockUnzipper(ctrl *gomock.Controller) *MockUnzipper {
mock := &MockUnzipper{ctrl: ctrl}
mock.recorder = &MockUnzipperMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockUnzipper) EXPECT() *MockUnzipperMockRecorder {
return m.recorder
}
// FetchAndExtract mocks base method.
func (m *MockUnzipper) FetchAndExtract(arg0 context.Context, arg1 string) (map[string][]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FetchAndExtract", arg0, arg1)
ret0, _ := ret[0].(map[string][]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FetchAndExtract indicates an expected call of FetchAndExtract.
func (mr *MockUnzipperMockRecorder) FetchAndExtract(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FetchAndExtract", reflect.TypeOf((*MockUnzipper)(nil).FetchAndExtract), arg0, arg1)
}

View File

@@ -2,4 +2,4 @@ package common
// Exceptionally, these mocks are exported since they are used by all
// provider subpackages tests, and it reduces test code duplication a lot.
//go:generate mockgen -destination=mocks.go -package $GOPACKAGE . ParallelResolver,Storage
//go:generate mockgen -destination=mocks.go -package $GOPACKAGE . ParallelResolver,Storage,Unzipper

View File

@@ -20,7 +20,8 @@ type ParallelResolver interface {
}
type Unzipper interface {
FetchAndExtract(ctx context.Context, url string) (contents map[string][]byte, err error)
FetchAndExtract(ctx context.Context, url string) (
contents map[string][]byte, err error)
}
type Warner interface {

View File

@@ -8,7 +8,6 @@ import (
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/extract"
)
var (
@@ -28,7 +27,7 @@ func (p *Provider) GetConnection(selection settings.ServerSelection) (
}
}
func getOpenVPNConnection(extractor extract.Interface,
func getOpenVPNConnection(extractor extractor,
selection settings.ServerSelection) (
connection models.Connection, err error) {
_, connection, err = extractor.Data(*selection.OpenVPN.ConfFile)

View File

@@ -0,0 +1,8 @@
package custom
import "github.com/qdm12/gluetun/internal/models"
type extractor interface {
Data(filepath string) (lines []string,
connection models.Connection, err error)
}

View File

@@ -8,7 +8,7 @@ import (
)
type Provider struct {
extractor extract.Interface
extractor extractor
utils.NoPortForwarder
common.Fetcher
}

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -9,14 +9,10 @@ import (
type Updater struct {
client *http.Client
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, warner Warner) *Updater {
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
presolver: newParallelResolver(),

View File

@@ -10,7 +10,6 @@ import (
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip/mock_unzip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -22,7 +21,7 @@ func Test_Updater_GetServers(t *testing.T) {
minServers int
// Mocks
warnerBuilder func(ctrl *gomock.Controller) Warner
warnerBuilder func(ctrl *gomock.Controller) common.Warner
// Unzip
unzipContents map[string][]byte
@@ -40,25 +39,25 @@ func Test_Updater_GetServers(t *testing.T) {
err error
}{
"unzipper error": {
warnerBuilder: func(ctrl *gomock.Controller) Warner { return nil },
warnerBuilder: func(ctrl *gomock.Controller) common.Warner { return nil },
unzipErr: errors.New("dummy"),
err: errors.New("dummy"),
},
"not enough unzip contents": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner { return nil },
warnerBuilder: func(ctrl *gomock.Controller) common.Warner { return nil },
unzipContents: map[string][]byte{},
err: errors.New("not enough servers found: 0 and expected at least 1"),
},
"no openvpn file": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner { return nil },
warnerBuilder: func(ctrl *gomock.Controller) common.Warner { return nil },
unzipContents: map[string][]byte{"somefile.txt": {}},
err: errors.New("not enough servers found: 0 and expected at least 1"),
},
"invalid proto": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("unknown protocol: invalid in badproto.ovpn")
return warner
@@ -68,7 +67,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"no host": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("remote host not found in nohost.ovpn")
return warner
@@ -78,7 +77,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"multiple hosts": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("only using the first host \"hosta\" and discarding 1 other hosts")
return warner
@@ -91,7 +90,7 @@ func Test_Updater_GetServers(t *testing.T) {
err: errors.New("not enough servers found: 0 and expected at least 1"),
},
"resolve error": {
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("resolve warning")
return warner
@@ -107,7 +106,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"filename parsing error": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("country code is unknown: unknown in ipvanish-unknown-City-A-hosta.ovpn")
return warner
@@ -119,7 +118,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"success": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("resolve warning")
return warner
@@ -163,7 +162,7 @@ func Test_Updater_GetServers(t *testing.T) {
ctx := context.Background()
unzipper := mock_unzip.NewMockUnzipper(ctrl)
unzipper := common.NewMockUnzipper(ctrl)
const zipURL = "https://www.ipvanish.com/software/configs/configs.zip"
unzipper.EXPECT().FetchAndExtract(ctx, zipURL).
Return(testCase.unzipContents, testCase.unzipErr)

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
warner Warner
unzipper common.Unzipper
warner common.Warner
presolver common.ParallelResolver
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
warner: warner,

View File

@@ -25,7 +25,7 @@ func Test_Updater_GetServers(t *testing.T) {
minServers int
// Mocks
warnerBuilder func(ctrl *gomock.Controller) Warner
warnerBuilder func(ctrl *gomock.Controller) common.Warner
// From API
responseBody string
@@ -43,12 +43,12 @@ func Test_Updater_GetServers(t *testing.T) {
err error
}{
"http response error": {
warnerBuilder: func(ctrl *gomock.Controller) Warner { return nil },
warnerBuilder: func(ctrl *gomock.Controller) common.Warner { return nil },
responseStatus: http.StatusNoContent,
err: errors.New("failed fetching API: HTTP status code not OK: 204 No Content"),
},
"resolve error": {
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("resolve warning")
return warner
@@ -65,7 +65,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"not enough servers": {
minServers: 2,
warnerBuilder: func(ctrl *gomock.Controller) Warner { return nil },
warnerBuilder: func(ctrl *gomock.Controller) common.Warner { return nil },
responseBody: `{"servers":[
{"hostnames":{"openvpn":"hosta"}}
]}`,
@@ -74,7 +74,7 @@ func Test_Updater_GetServers(t *testing.T) {
},
"success": {
minServers: 1,
warnerBuilder: func(ctrl *gomock.Controller) Warner {
warnerBuilder: func(ctrl *gomock.Controller) common.Warner {
warner := NewMockWarner(ctrl)
warner.EXPECT().Warn("resolve warning")
return warner

View File

@@ -9,14 +9,10 @@ import (
type Updater struct {
client *http.Client
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, warner Warner) *Updater {
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
presolver: newParallelResolver(),

View File

@@ -2,18 +2,16 @@ package updater
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
warner Warner
warner common.Warner
}
type Warner interface {
Warn(message string)
}
func New(client *http.Client, warner Warner) *Updater {
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
warner: warner,

View File

@@ -1,17 +1,15 @@
package updater
import "github.com/qdm12/gluetun/internal/updater/unzip"
import (
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
unzipper unzip.Unzipper
warner Warner
unzipper common.Unzipper
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
warner: warner,

View File

@@ -4,22 +4,17 @@ import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
client *http.Client
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, unzipper unzip.Unzipper,
warner Warner) *Updater {
func New(client *http.Client, unzipper common.Unzipper,
warner common.Warner) *Updater {
return &Updater{
client: client,
unzipper: unzipper,

View File

@@ -8,10 +8,6 @@ type Updater struct {
client *http.Client
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client) *Updater {
return &Updater{
client: client,

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -2,18 +2,16 @@ package updater
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, warner Warner) *Updater {
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
warner: warner,

View File

@@ -82,7 +82,7 @@ func NewProviders(storage Storage, timeNow func() time.Time,
}
}
func (p *Providers) Get(providerName string) (provider Provider) {
func (p *Providers) Get(providerName string) (provider Provider) { //nolint:ireturn
provider, ok := p.providerNameToProvider[providerName]
if !ok {
panic(fmt.Sprintf("provider %q not found", providerName))

View File

@@ -4,22 +4,17 @@ import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
client *http.Client
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, unzipper unzip.Unzipper,
warner Warner) *Updater {
func New(client *http.Client, unzipper common.Unzipper,
warner common.Warner) *Updater {
return &Updater{
client: client,
unzipper: unzipper,

View File

@@ -4,22 +4,17 @@ import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
client *http.Client
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, unzipper unzip.Unzipper,
warner Warner) *Updater {
func New(client *http.Client, unzipper common.Unzipper,
warner common.Warner) *Updater {
return &Updater{
client: client,
unzipper: unzipper,

View File

@@ -4,13 +4,13 @@ import (
"context"
"strings"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
"github.com/qdm12/gluetun/internal/updater/openvpn"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
func addOpenVPNServersFromZip(ctx context.Context,
unzipper unzip.Unzipper, hts hostToServer) (
unzipper common.Unzipper, hts hostToServer) (
warnings []string, err error) {
const url = "https://my.surfshark.com/vpn/api/v1/server/configurations"
contents, err := unzipper.FetchAndExtract(ctx, url)

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -2,20 +2,15 @@ package updater
import (
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
type Updater struct {
unzipper unzip.Unzipper
unzipper common.Unzipper
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(unzipper unzip.Unzipper, warner Warner) *Updater {
func New(unzipper common.Unzipper, warner common.Warner) *Updater {
return &Updater{
unzipper: unzipper,
presolver: newParallelResolver(),

View File

@@ -4,14 +4,10 @@ import "github.com/qdm12/gluetun/internal/provider/common"
type Updater struct {
presolver common.ParallelResolver
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(warner Warner) *Updater {
func New(warner common.Warner) *Updater {
return &Updater{
presolver: newParallelResolver(),
warner: warner,

View File

@@ -2,18 +2,16 @@ package updater
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
warner Warner
warner common.Warner
}
type Warner interface {
Warn(s string)
}
func New(client *http.Client, warner Warner) *Updater {
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
warner: warner,