Skip to content

Commit

Permalink
extract to sync package
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Mar 28, 2021
1 parent 785c23a commit 1bb31c3
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 152 deletions.
154 changes: 2 additions & 152 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/bakito/adguardhome-sync/pkg/sync"
"os"

"github.com/bakito/adguardhome-sync/pkg/log"
Expand Down Expand Up @@ -31,158 +32,7 @@ func main() {
if err != nil {
panic(err)
}

logger := l.With("from", origin.Host(), "to", replica.Host())

logger.Info("Start sync")
defer func() { logger.Info("Sync done") }()

os, err := origin.Status()
if err != nil {
panic(err)
}

rs, err := replica.Status()
if err != nil {
panic(err)
}

if os.Version != rs.Version {
panic("Versions do not match")
}

err = syncRewrites(origin, replica)
if err != nil {
panic(err)
}
err = syncFilters(origin, replica)
if err != nil {
if err = sync.Sync(origin, replica); err != nil {
panic(err)
}

err = syncServices(origin, replica)
if err != nil {
panic(err)
}

err = syncClients(origin, replica)
if err != nil {
panic(err)
}
}

func syncServices(origin client.Client, replica client.Client) error {
os, err := origin.Services()
if err != nil {
return err
}
rs, err := replica.Services()
if err != nil {
return err
}

if !os.Equals(rs) {
if err := replica.SetServices(os); err != nil {
return err
}
}
return nil
}

func syncFilters(origin client.Client, replica client.Client) error {
of, err := origin.Filtering()
if err != nil {
return err
}
rf, err := replica.Filtering()
if err != nil {
return err
}

fa, fd := rf.Filters.Merge(of.Filters)

if err = replica.AddFilters(false, fa...); err != nil {
return err
}

if len(fa) > 0 {
if err = replica.RefreshFilters(false); err != nil {
return err
}
}

if err = replica.DeleteFilters(false, fd...); err != nil {
return err
}

fa, fd = rf.WhitelistFilters.Merge(of.WhitelistFilters)
if err = replica.AddFilters(true, fa...); err != nil {
return err
}

if len(fa) > 0 {
if err = replica.RefreshFilters(true); err != nil {
return err
}
}

if err = replica.DeleteFilters(true, fd...); err != nil {
return err
}

if of.UserRules.String() != rf.UserRules.String() {
return replica.SetCustomRules(of.UserRules)
}

if of.Enabled != rf.Enabled || of.Interval != rf.Interval {
if err = replica.ToggleFiltering(of.Enabled, of.Interval); err != nil {
return err
}
}
return nil
}

func syncRewrites(origin client.Client, replica client.Client) error {
originRewrites, err := origin.RewriteList()
if err != nil {
return err
}
replicaRewrites, err := replica.RewriteList()
if err != nil {
return err
}

a, r := replicaRewrites.Merge(originRewrites)

if err = replica.AddRewriteEntries(a...); err != nil {
return err
}
if err = replica.DeleteRewriteEntries(r...); err != nil {
return err
}
return nil
}

func syncClients(origin client.Client, replica client.Client) error {
oc, err := origin.Clients()
if err != nil {
return err
}
rc, err := replica.Clients()
if err != nil {
return err
}

a, u, r := rc.Merge(oc)

if err = replica.AddClients(a...); err != nil {
return err
}
if err = replica.UpdateClients(u...); err != nil {
return err
}
if err = replica.DeleteClients(r...); err != nil {
return err
}
return nil
}
160 changes: 160 additions & 0 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package sync

import (
"github.com/bakito/adguardhome-sync/pkg/client"
"github.com/bakito/adguardhome-sync/pkg/log"
)

// Sync config from origin to replica
func Sync(origin client.Client, replica client.Client) error {

l := log.GetLogger("sync").With("from", origin.Host(), "to", replica.Host())
l.Info("Start sync")
defer func() { l.Info("Sync done") }()

os, err := origin.Status()
if err != nil {
return err
}

rs, err := replica.Status()
if err != nil {
return err
}

if os.Version != rs.Version {
panic("Versions do not match")
}

err = syncRewrites(origin, replica)
if err != nil {
return err
}
err = syncFilters(origin, replica)
if err != nil {
return err
}

err = syncServices(origin, replica)
if err != nil {
return err
}

return syncClients(origin, replica)
}

func syncServices(origin client.Client, replica client.Client) error {
os, err := origin.Services()
if err != nil {
return err
}
rs, err := replica.Services()
if err != nil {
return err
}

if !os.Equals(rs) {
if err := replica.SetServices(os); err != nil {
return err
}
}
return nil
}

func syncFilters(origin client.Client, replica client.Client) error {
of, err := origin.Filtering()
if err != nil {
return err
}
rf, err := replica.Filtering()
if err != nil {
return err
}

fa, fd := rf.Filters.Merge(of.Filters)

if err = replica.AddFilters(false, fa...); err != nil {
return err
}

if len(fa) > 0 {
if err = replica.RefreshFilters(false); err != nil {
return err
}
}

if err = replica.DeleteFilters(false, fd...); err != nil {
return err
}

fa, fd = rf.WhitelistFilters.Merge(of.WhitelistFilters)
if err = replica.AddFilters(true, fa...); err != nil {
return err
}

if len(fa) > 0 {
if err = replica.RefreshFilters(true); err != nil {
return err
}
}

if err = replica.DeleteFilters(true, fd...); err != nil {
return err
}

if of.UserRules.String() != rf.UserRules.String() {
return replica.SetCustomRules(of.UserRules)
}

if of.Enabled != rf.Enabled || of.Interval != rf.Interval {
if err = replica.ToggleFiltering(of.Enabled, of.Interval); err != nil {
return err
}
}
return nil
}

func syncRewrites(origin client.Client, replica client.Client) error {
originRewrites, err := origin.RewriteList()
if err != nil {
return err
}
replicaRewrites, err := replica.RewriteList()
if err != nil {
return err
}

a, r := replicaRewrites.Merge(originRewrites)

if err = replica.AddRewriteEntries(a...); err != nil {
return err
}
if err = replica.DeleteRewriteEntries(r...); err != nil {
return err
}
return nil
}

func syncClients(origin client.Client, replica client.Client) error {
oc, err := origin.Clients()
if err != nil {
return err
}
rc, err := replica.Clients()
if err != nil {
return err
}

a, u, r := rc.Merge(oc)

if err = replica.AddClients(a...); err != nil {
return err
}
if err = replica.UpdateClients(u...); err != nil {
return err
}
if err = replica.DeleteClients(r...); err != nil {
return err
}
return nil
}

0 comments on commit 1bb31c3

Please sign in to comment.