Skip to content

Commit

Permalink
Revise package structure (#18)
Browse files Browse the repository at this point in the history
* Revise package structure
* Fix .gitignore
* Disable logging during test execution
* Refactor receiver name
  • Loading branch information
bitflipp authored May 11, 2024
1 parent d243a4a commit 07d28c3
Show file tree
Hide file tree
Showing 30 changed files with 127 additions and 116 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
dist/
*.coverage
*.save
gerberos
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ run: dist
dist/gerberos

test: clean
go test -v -coverprofile=gerberos.coverage
go test -v -coverprofile=gerberos.coverage ./...
go tool cover -html=gerberos.coverage

test_system: clean
go test -v -tags=system -coverprofile=gerberos.coverage
go test -v -tags=system -coverprofile=gerberos.coverage ./...
go tool cover -html=gerberos.coverage
15 changes: 8 additions & 7 deletions main.go → cmd/gerberos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"runtime/debug"

gerberos "github.com/bitflipp/gerberos/internal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -38,7 +39,7 @@ func logVersionAndBuildInfo() {
ev.Msg("")
}

func setGlobalLogLevel(c *configuration) {
func setGlobalLogLevel(c *gerberos.Configuration) {
switch c.LogLevel {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
Expand All @@ -58,22 +59,22 @@ func main() {
cfp := flag.String("c", "./gerberos.toml", "Path to TOML configuration file")
flag.Parse()

c := &configuration{}
if err := c.readFile(*cfp); err != nil {
c := &gerberos.Configuration{}
if err := c.ReadFile(*cfp); err != nil {
log.Fatal().Err(err).Msg("failed to read configuration file")
}

setGlobalLogLevel(c)
logVersionAndBuildInfo()

rn := newRunner(c)
if err := rn.initialize(); err != nil {
rn := gerberos.NewRunner(c)
if err := rn.Initialize(); err != nil {
log.Fatal().Err(err).Msg("failed to initialize runner")
}
defer func() {
if err := rn.finalize(); err != nil {
if err := rn.Finalize(); err != nil {
log.Fatal().Err(err).Msg("failed to finalize runner")
}
}()
rn.run(true)
rn.Run(true)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.18

require (
github.com/BurntSushi/toml v1.3.2
github.com/rs/zerolog v1.29.1
github.com/rs/zerolog v1.32.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.10.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.20.0 // indirect
)
18 changes: 8 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2 changes: 1 addition & 1 deletion actions.go → internal/actions.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion aggregate.go → internal/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"net"
Expand Down
8 changes: 4 additions & 4 deletions backends.go → internal/backends.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"errors"
Expand All @@ -18,7 +18,7 @@ type backend interface {
}

type ipsetBackend struct {
runner *runner
runner *Runner
chainName string
ipset4Name string
ipset6Name string
Expand Down Expand Up @@ -203,7 +203,7 @@ func (b *ipsetBackend) finalize() error {
}

type nftBackend struct {
runner *runner
runner *Runner
table4Name string
table6Name string
set4Name string
Expand Down Expand Up @@ -347,7 +347,7 @@ func (b *nftBackend) finalize() error {
}

type testBackend struct {
runner *runner
runner *Runner
initializeErr error
banErr error
finalizeErr error
Expand Down
8 changes: 4 additions & 4 deletions configuration.go → internal/configuration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"errors"
Expand All @@ -9,14 +9,14 @@ import (
"github.com/BurntSushi/toml"
)

type configuration struct {
type Configuration struct {
Backend string
SaveFilePath string
LogLevel string
Rules map[string]*rule
}

func (c *configuration) readFile(path string) error {
func (c *Configuration) ReadFile(path string) error {
cf, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open configuration file: %w", err)
Expand All @@ -26,7 +26,7 @@ func (c *configuration) readFile(path string) error {
return c.read(cf)
}

func (c *configuration) read(r io.Reader) error {
func (c *Configuration) read(r io.Reader) error {
if _, err := toml.NewDecoder(r).Decode(&c); err != nil {
var terr toml.ParseError
if errors.As(err, &terr) {
Expand Down
8 changes: 4 additions & 4 deletions configuration_test.go → internal/configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"errors"
Expand All @@ -8,8 +8,8 @@ import (

func TestConfigurationReadFileInvalid(t *testing.T) {
rc := func(n string) {
c := &configuration{}
testError(t, c.readFile(n))
c := &Configuration{}
testError(t, c.ReadFile(n))
}

rc("")
Expand All @@ -18,6 +18,6 @@ func TestConfigurationReadFileInvalid(t *testing.T) {

func TestConfigurationReadFileError(t *testing.T) {
r := iotest.ErrReader(errors.New(""))
c := &configuration{}
c := &Configuration{}
testError(t, c.read(r))
}
2 changes: 1 addition & 1 deletion executor.go → internal/executor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion executor_test.go → internal/executor_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"strings"
Expand Down
13 changes: 13 additions & 0 deletions internal/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gerberos

import (
"os"
"testing"

"github.com/rs/zerolog"
)

func TestMain(m *testing.M) {
zerolog.SetGlobalLevel(zerolog.Disabled)
os.Exit(m.Run())
}
2 changes: 1 addition & 1 deletion match.go → internal/match.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion match_test.go → internal/match_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"regexp"
Expand Down
24 changes: 12 additions & 12 deletions occurrences.go → internal/occurrences.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"net"
Expand All @@ -13,26 +13,26 @@ type occurrences struct {
count int
}

func (r *occurrences) add(ip net.IP) bool {
func (o *occurrences) add(ip net.IP) bool {
ips := ip.String()
t := time.Now()

log.Debug().IPAddr("ip", ip).Int("length", len(r.registry[ips])).Msg("updating occurrences")
log.Debug().IPAddr("ip", ip).Int("length", len(o.registry[ips])).Msg("updating occurrences")

if _, f := r.registry[ips]; !f {
r.registry[ips] = []time.Time{t}
if _, f := o.registry[ips]; !f {
o.registry[ips] = []time.Time{t}
return false
}

r.registry[ips] = append(r.registry[ips], t)
if len(r.registry[ips]) > r.count {
r.registry[ips] = r.registry[ips][1:]
o.registry[ips] = append(o.registry[ips], t)
if len(o.registry[ips]) > o.count {
o.registry[ips] = o.registry[ips][1:]
}

if len(r.registry[ips]) == r.count {
d := r.registry[ips][r.count-1].Sub(r.registry[ips][0])
if d <= r.interval {
delete(r.registry, ips)
if len(o.registry[ips]) == o.count {
d := o.registry[ips][o.count-1].Sub(o.registry[ips][0])
if d <= o.interval {
delete(o.registry, ips)
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion occurrences_test.go → internal/occurrences_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"net"
Expand Down
6 changes: 3 additions & 3 deletions rule.go → internal/rule.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"bufio"
Expand Down Expand Up @@ -35,7 +35,7 @@ type rule struct {
Aggregate []string
Occurrences []string

runner *runner
runner *Runner
name string
source source
regexp []*regexp.Regexp
Expand Down Expand Up @@ -201,7 +201,7 @@ func (r *rule) initializeOccurrences() error {
return nil
}

func (r *rule) initialize(rn *runner) error {
func (r *rule) initialize(rn *Runner) error {
r.runner = rn

if err := r.initializeSource(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rule_test.go → internal/rule_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"testing"
Expand Down
18 changes: 9 additions & 9 deletions runner.go → internal/runner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gerberos

import (
"context"
Expand All @@ -12,8 +12,8 @@ import (
"github.com/rs/zerolog/log"
)

type runner struct {
configuration *configuration
type Runner struct {
configuration *Configuration
backend backend
respawnWorkerDelay time.Duration
respawnWorkerChan chan *rule
Expand All @@ -22,7 +22,7 @@ type runner struct {
stopped context.Context
}

func (rn *runner) initialize() error {
func (rn *Runner) Initialize() error {
if rn.configuration == nil {
return errors.New("configuration has not been set")
}
Expand Down Expand Up @@ -55,15 +55,15 @@ func (rn *runner) initialize() error {
return nil
}

func (rn *runner) finalize() error {
func (rn *Runner) Finalize() error {
if err := rn.backend.finalize(); err != nil {
return fmt.Errorf("failed to finalize backend: %w", err)
}

return nil
}

func (rn *runner) spawnWorker(r *rule, requeue bool) {
func (rn *Runner) spawnWorker(r *rule, requeue bool) {
go func() {
select {
case <-rn.stopped.Done():
Expand All @@ -74,7 +74,7 @@ func (rn *runner) spawnWorker(r *rule, requeue bool) {
log.Info().Str("rule", r.name).Msg("spawned worker")
}

func (rn *runner) run(requeueWorkers bool) {
func (rn *Runner) Run(requeueWorkers bool) {
for _, r := range rn.configuration.Rules {
rn.spawnWorker(r, requeueWorkers)
}
Expand Down Expand Up @@ -103,9 +103,9 @@ func (rn *runner) run(requeueWorkers bool) {
}
}

func newRunner(c *configuration) *runner {
func NewRunner(c *Configuration) *Runner {
ctx, cancel := context.WithCancel(context.Background())
return &runner{
return &Runner{
configuration: c,
respawnWorkerDelay: 5 * time.Second,
respawnWorkerChan: make(chan *rule),
Expand Down
Loading

0 comments on commit 07d28c3

Please sign in to comment.