diff --git a/Makefile b/Makefile index 131e199..9fe9fa5 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ test: mocks tidy fmt vet go tool cover -func=coverage.out mocks: mockgen - mockgen -destination pkg/mocks/client/mock.go github.com/bakito/adguardhome-sync/pkg/client Client + mockgen -package client -destination pkg/mocks/client/mock.go github.com/bakito/adguardhome-sync/pkg/client Client release: semver @version=$$(semver); \ diff --git a/pkg/client/client.go b/pkg/client/client.go index 982c7f0..c96edb5 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -15,8 +15,9 @@ import ( ) var ( - l = log.GetLogger("client") - SetupNeededError = errors.New("setup needed") + l = log.GetLogger("client") + // ErrSetupNeeded custom error + ErrSetupNeeded = errors.New("setup needed") ) // New create a new client @@ -124,7 +125,7 @@ func (cl *client) doGet(req *resty.Request, url string) error { if resp != nil && resp.StatusCode() == http.StatusFound { loc := resp.Header().Get("Location") if loc == "/install.html" { - return SetupNeededError + return ErrSetupNeeded } } rl.With("status", resp.StatusCode(), "body", string(resp.Body()), "error", err).Debug("error in do get") diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index da646c6..1080bea 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -107,7 +107,7 @@ bar`) Ω(fs.DNSAddresses[0]).Should(Equal("192.168.1.2")) Ω(fs.Version).Should(Equal("v0.105.2")) }) - It("should return SetupNeededError", func() { + It("should return ErrSetupNeeded", func() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Location", "/install.html") w.WriteHeader(http.StatusFound) @@ -116,7 +116,7 @@ bar`) Ω(err).ShouldNot(HaveOccurred()) _, err = cl.Status() Ω(err).Should(HaveOccurred()) - Ω(err).Should(Equal(client.SetupNeededError)) + Ω(err).Should(Equal(client.ErrSetupNeeded)) }) }) diff --git a/pkg/mocks/client/mock.go b/pkg/mocks/client/mock.go index 92c0b4b..2b958d7 100644 --- a/pkg/mocks/client/mock.go +++ b/pkg/mocks/client/mock.go @@ -1,8 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/bakito/adguardhome-sync/pkg/client (interfaces: Client) -// Package mock_client is a generated GoMock package. -package mock_client +// Package client is a generated GoMock package. +package client import ( reflect "reflect" diff --git a/pkg/sync/http.go b/pkg/sync/http.go index 9d43fc0..ec319ef 100644 --- a/pkg/sync/http.go +++ b/pkg/sync/http.go @@ -1,6 +1,7 @@ package sync import ( + // import embed for html page _ "embed" "context" diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 9348a74..3d4842a 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -237,7 +237,7 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn func (w *worker) statusWithSetup(rl *zap.SugaredLogger, replica types.AdGuardInstance, rc client.Client) (*types.Status, error) { rs, err := rc.Status() if err != nil { - if replica.AutoSetup && errors.Is(err, client.SetupNeededError) { + if replica.AutoSetup && errors.Is(err, client.ErrSetupNeeded) { if serr := rc.Setup(); serr != nil { rl.With("error", serr).Error("Error setup AdGuardHome") return nil, err diff --git a/pkg/sync/sync_test.go b/pkg/sync/sync_test.go index 03255cc..f43fb98 100644 --- a/pkg/sync/sync_test.go +++ b/pkg/sync/sync_test.go @@ -3,12 +3,11 @@ package sync import ( "errors" - "github.com/bakito/adguardhome-sync/pkg/client" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - mc "github.com/bakito/adguardhome-sync/pkg/mocks/client" + "github.com/bakito/adguardhome-sync/pkg/client" + clientmock "github.com/bakito/adguardhome-sync/pkg/mocks/client" "github.com/bakito/adguardhome-sync/pkg/types" gm "github.com/golang/mock/gomock" "github.com/google/uuid" @@ -17,14 +16,14 @@ import ( var _ = Describe("Sync", func() { var ( mockCtrl *gm.Controller - cl *mc.MockClient + cl *clientmock.MockClient w *worker te error ) BeforeEach(func() { mockCtrl = gm.NewController(GinkgoT()) - cl = mc.NewMockClient(mockCtrl) + cl = clientmock.NewMockClient(mockCtrl) w = &worker{ createClient: func(instance types.AdGuardInstance) (client.Client, error) { return cl, nil @@ -284,7 +283,7 @@ var _ = Describe("Sync", func() { Ω(st).Should(Equal(status)) }) It("should runs setup before getting replica status", func() { - cl.EXPECT().Status().Return(nil, client.SetupNeededError) + cl.EXPECT().Status().Return(nil, client.ErrSetupNeeded) cl.EXPECT().Setup() cl.EXPECT().Status().Return(status, nil) st, err := w.statusWithSetup(l, inst, cl) @@ -292,7 +291,7 @@ var _ = Describe("Sync", func() { Ω(st).Should(Equal(status)) }) It("should fail on setup", func() { - cl.EXPECT().Status().Return(nil, client.SetupNeededError) + cl.EXPECT().Status().Return(nil, client.ErrSetupNeeded) cl.EXPECT().Setup().Return(te) st, err := w.statusWithSetup(l, inst, cl) Ω(err).Should(HaveOccurred()) diff --git a/pkg/types/dhcp.go b/pkg/types/dhcp.go index d214cd3..486696f 100644 --- a/pkg/types/dhcp.go +++ b/pkg/types/dhcp.go @@ -6,6 +6,7 @@ import ( "time" ) +// DHCPServerConfig dhcp server config type DHCPServerConfig struct { V4 *V4ServerConfJSON `json:"v4"` V6 *V6ServerConfJSON `json:"v6"` @@ -23,6 +24,7 @@ func (c *DHCPServerConfig) Equals(o *DHCPServerConfig) bool { return string(a) == string(b) } +// V4ServerConfJSON v4 server conf type V4ServerConfJSON struct { GatewayIP net.IP `json:"gateway_ip"` SubnetMask net.IP `json:"subnet_mask"` @@ -31,12 +33,14 @@ type V4ServerConfJSON struct { LeaseDuration uint32 `json:"lease_duration"` } +// V6ServerConfJSON v6 server conf type V6ServerConfJSON struct { RangeStart net.IP `json:"range_start"` RangeEnd net.IP `json:"range_end"` LeaseDuration uint32 `json:"lease_duration"` } +// Leases slice of leases type type Leases []Lease // Merge the leases diff --git a/pkg/types/dns.go b/pkg/types/dns.go index 2924ad7..951766c 100644 --- a/pkg/types/dns.go +++ b/pkg/types/dns.go @@ -6,19 +6,7 @@ import ( "sort" ) -// https://ha.bakito.net:3000/control/dns_config -// {"bootstrap_dns":["1.1.1.1:53"],"upstream_mode":"parallel","upstream_dns":["https://dns10.quad9.net/dns-query"]} -// {"bootstrap_dns":["1.1.1.1:53"],"upstream_mode":"","upstream_dns":["https://dns10.quad9.net/dns-query"]} -// {"bootstrap_dns":["1.1.1.1:53"],"upstream_mode":"fastest_addr","upstream_dns":["https://dns10.quad9.net/dns-query"]} - -// {"ratelimit":20,"blocking_mode":"default","blocking_ipv4":"0.0.0.0","blocking_ipv6":"::","edns_cs_enabled":true,"disable_ipv6":false,"dnssec_enabled":false} -// {"cache_size":4194304,"cache_ttl_max":0,"cache_ttl_min":0} - -// https://ha.bakito.net:3000/control/access/set -// {"allowed_clients":["2.2.2.2"],"disallowed_clients":["1.1.1.1"],"blocked_hosts":["version.bind","id.server","hostname.bind"]} -// https://ha.bakito.net:3000/control/access/list -// {"allowed_clients":[],"disallowed_clients":[],"blocked_hosts":["version.bind","id.server","hostname.bind"]} - +// DNSConfig dns config type DNSConfig struct { Upstreams []string `json:"upstream_dns,omitempty"` UpstreamsFile string `json:"upstream_dns_file"` @@ -57,6 +45,7 @@ func (c *DNSConfig) Sort() { sort.Strings(c.LocalPTRUpstreams) } +// AccessList access list type AccessList struct { AllowedClients []string `json:"allowed_clients"` DisallowedClients []string `json:"disallowed_clients"` diff --git a/pkg/types/types.go b/pkg/types/types.go index ae03033..fc4587f 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -9,6 +9,7 @@ import ( ) const ( + // DefaultAPIPath default api path DefaultAPIPath = "/control" ) @@ -57,6 +58,7 @@ func (cfg *Config) UniqueReplicas() []AdGuardInstance { return r } +// WithBeta return true if the given beta feature is enabled func (cfg *Config) WithBeta(name string) bool { doOnce.Do(func() { cfg.enabledBeta = make(map[string]bool)