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

@@ -0,0 +1,33 @@
package updater
import (
"context"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider"
)
type Providers interface {
Get(providerName string) provider.Provider
}
type Storage interface {
SetServers(provider string, servers []models.Server) (err error)
GetServersCount(provider string) (count int)
ServersAreEqual(provider string, servers []models.Server) (equal bool)
// Extra methods to match the provider.New storage interface
FilterServers(provider string, selection settings.ServerSelection) (filtered []models.Server, err error)
GetServerByName(provider string, name string) (server models.Server, ok bool)
}
type Unzipper interface {
FetchAndExtract(ctx context.Context, url string) (
contents map[string][]byte, err error)
}
type Logger interface {
Info(s string)
Warn(s string)
Error(s string)
}

View File

@@ -12,21 +12,11 @@ import (
"github.com/qdm12/gluetun/internal/updater"
)
type Looper interface {
Run(ctx context.Context, done chan<- struct{})
RunRestartTicker(ctx context.Context, done chan<- struct{})
GetStatus() (status models.LoopStatus)
SetStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
GetSettings() (settings settings.Updater)
SetSettings(settings settings.Updater) (outcome string)
}
type Updater interface {
UpdateServers(ctx context.Context, providers []string) (err error)
}
type looper struct {
type Loop struct {
state state
// Objects
updater Updater
@@ -52,9 +42,9 @@ type Logger interface {
Error(s string)
}
func NewLooper(settings settings.Updater, providers updater.Providers,
storage updater.Storage, client *http.Client, logger Logger) Looper {
return &looper{
func NewLoop(settings settings.Updater, providers updater.Providers,
storage updater.Storage, client *http.Client, logger Logger) *Loop {
return &Loop{
state: state{
status: constants.Stopped,
settings: settings,
@@ -72,7 +62,7 @@ func NewLooper(settings settings.Updater, providers updater.Providers,
}
}
func (l *looper) logAndWait(ctx context.Context, err error) {
func (l *Loop) logAndWait(ctx context.Context, err error) {
if err != nil {
l.logger.Error(err.Error())
}
@@ -88,7 +78,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
}
}
func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
crashed := false
select {
@@ -156,7 +146,7 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
}
}
func (l *looper) RunRestartTicker(ctx context.Context, done chan<- struct{}) {
func (l *Loop) RunRestartTicker(ctx context.Context, done chan<- struct{}) {
defer close(done)
timer := time.NewTimer(time.Hour)
timer.Stop()

View File

@@ -25,7 +25,7 @@ func (s *state) setStatusWithLock(status models.LoopStatus) {
s.status = status
}
func (l *looper) GetStatus() (status models.LoopStatus) {
func (l *Loop) GetStatus() (status models.LoopStatus) {
l.state.statusMu.RLock()
defer l.state.statusMu.RUnlock()
return l.state.status
@@ -33,7 +33,7 @@ func (l *looper) GetStatus() (status models.LoopStatus) {
var ErrInvalidStatus = errors.New("invalid status")
func (l *looper) SetStatus(ctx context.Context, status models.LoopStatus) (outcome string, err error) {
func (l *Loop) SetStatus(ctx context.Context, status models.LoopStatus) (outcome string, err error) {
l.state.statusMu.Lock()
defer l.state.statusMu.Unlock()
existingStatus := l.state.status
@@ -84,13 +84,13 @@ func (l *looper) SetStatus(ctx context.Context, status models.LoopStatus) (outco
}
}
func (l *looper) GetSettings() (settings settings.Updater) {
func (l *Loop) GetSettings() (settings settings.Updater) {
l.state.periodMu.RLock()
defer l.state.periodMu.RUnlock()
return l.state.settings
}
func (l *looper) SetSettings(settings settings.Updater) (outcome string) {
func (l *Loop) SetSettings(settings settings.Updater) (outcome string) {
l.state.periodMu.Lock()
defer l.state.periodMu.Unlock()
settingsUnchanged := reflect.DeepEqual(settings, l.state.settings)

View File

@@ -12,7 +12,7 @@ var (
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
)
func (u *unzipper) FetchAndExtract(ctx context.Context, url string) (
func (u *Unzipper) FetchAndExtract(ctx context.Context, url string) (
contents map[string][]byte, err error) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {

View File

@@ -1,50 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/qdm12/gluetun/internal/updater/unzip (interfaces: Unzipper)
// Package mock_unzip is a generated GoMock package.
package mock_unzip
import (
context "context"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// 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

@@ -3,22 +3,15 @@
package unzip
import (
"context"
"net/http"
)
//go:generate mockgen -destination=mock_$GOPACKAGE/$GOFILE . Unzipper
type Unzipper interface {
FetchAndExtract(ctx context.Context, url string) (contents map[string][]byte, err error)
}
type unzipper struct {
type Unzipper struct {
client *http.Client
}
func New(client *http.Client) Unzipper {
return &unzipper{
func New(client *http.Client) *Unzipper {
return &Unzipper{
client: client,
}
}

View File

@@ -6,9 +6,6 @@ import (
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/gluetun/internal/updater/unzip"
"golang.org/x/text/cases"
"golang.org/x/text/language"
@@ -24,26 +21,7 @@ type Updater struct {
logger Logger
timeNow func() time.Time
client *http.Client
unzipper unzip.Unzipper
}
type Providers interface {
Get(providerName string) provider.Provider
}
type Storage interface {
SetServers(provider string, servers []models.Server) (err error)
GetServersCount(provider string) (count int)
ServersAreEqual(provider string, servers []models.Server) (equal bool)
// Extra methods to match the provider.New storage interface
FilterServers(provider string, selection settings.ServerSelection) (filtered []models.Server, err error)
GetServerByName(provider string, name string) (server models.Server, ok bool)
}
type Logger interface {
Info(s string)
Warn(s string)
Error(s string)
unzipper Unzipper
}
func New(httpClient *http.Client, storage Storage,