From 45447275a76c151c9b8c15c6d811b6d4f24efe87 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 13:44:18 +0000 Subject: [PATCH 1/3] zones: introduce WithLogger() scan option Signed-off-by: Alejandro Mery --- cmd/jpictl/config.go | 1 + cmd/jpictl/log.go | 1 + go.mod | 1 + go.sum | 2 ++ pkg/zones/options.go | 26 +++++++++++++++++++++++++- 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cmd/jpictl/config.go b/cmd/jpictl/config.go index ec20b7e..d58439d 100644 --- a/cmd/jpictl/config.go +++ b/cmd/jpictl/config.go @@ -17,5 +17,6 @@ var cfg = &Config{ func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) { return zones.New(cfg.Base, cfg.Domain, zones.ResolvePublicAddresses(resolve), + zones.WithLogger(log), ) } diff --git a/cmd/jpictl/log.go b/cmd/jpictl/log.go index 963fad5..bb5f65c 100644 --- a/cmd/jpictl/log.go +++ b/cmd/jpictl/log.go @@ -7,6 +7,7 @@ import ( "darvaza.org/slog" ) +// TODO: make log level configurable via flags var ( log = zerolog.New(nil, slog.Debug) ) diff --git a/go.mod b/go.mod index 019ccee..31f8fb3 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( darvaza.org/resolver v0.5.4 darvaza.org/sidecar v0.0.2 darvaza.org/slog v0.5.3 + darvaza.org/slog/handlers/discard v0.4.5 github.com/burntSushi/toml v0.3.1 github.com/gofrs/uuid/v5 v5.0.0 github.com/hack-pad/hackpadfs v0.2.1 diff --git a/go.sum b/go.sum index e2f357a..0d816c7 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0= darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo= darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0= darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0= +darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g= +darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8= darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc= darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g= darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4= diff --git a/pkg/zones/options.go b/pkg/zones/options.go index 4c1e806..faefb9f 100644 --- a/pkg/zones/options.go +++ b/pkg/zones/options.go @@ -5,10 +5,12 @@ import ( "path/filepath" "darvaza.org/resolver" + "darvaza.org/slog" + "darvaza.org/slog/handlers/discard" "github.com/hack-pad/hackpadfs/os" ) -// A ScanOption preconfigures the Zones before scanning +// A ScanOption pre-configures the Zones before scanning type ScanOption func(*Zones, *ScanOptions) error // ScanOptions contains flags used by the initial scan @@ -17,6 +19,10 @@ type ScanOptions struct { // pre-populate Machine.PublicAddresses during the // initial scan DontResolvePublicAddresses bool + + // Logger specifies the logger to be used. otherwise + // the scanner will be mute + slog.Logger } // ResolvePublicAddresses instructs the scanner to use @@ -55,6 +61,18 @@ func WithResolver(h resolver.Resolver) ScanOption { } } +// WithLogger specifies what to use for logging +func WithLogger(log slog.Logger) ScanOption { + return func(m *Zones, opt *ScanOptions) error { + if log == nil { + log = discard.New() + } + + opt.Logger = log + return nil + } +} + func (m *Zones) setDefaults(opt *ScanOptions) error { if m.resolver == nil { h := resolver.NewCloudflareLookuper() @@ -64,6 +82,12 @@ func (m *Zones) setDefaults(opt *ScanOptions) error { } } + if opt.Logger == nil { + if err := WithLogger(nil)(m, opt); err != nil { + return err + } + } + return nil } -- 2.17.1 From 5e5958d22e3fe6e6a14c72964f20ffddf56f8a79 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 13:55:32 +0000 Subject: [PATCH 2/3] zones: introduce (private) logger interface Signed-off-by: Alejandro Mery --- pkg/zones/log.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ pkg/zones/options.go | 1 + pkg/zones/zones.go | 2 ++ 3 files changed, 52 insertions(+) create mode 100644 pkg/zones/log.go diff --git a/pkg/zones/log.go b/pkg/zones/log.go new file mode 100644 index 0000000..ceaf5ee --- /dev/null +++ b/pkg/zones/log.go @@ -0,0 +1,49 @@ +package zones + +import "darvaza.org/slog" + +type logger interface { + withDebug() (slog.Logger, bool) + withInfo() (slog.Logger, bool) + + debug() slog.Logger + info() slog.Logger + warn(error) slog.Logger + error(error) slog.Logger +} + +var ( + _ logger = (*Zones)(nil) +) + +func (z *Zones) withDebug() (slog.Logger, bool) { + return z.debug().WithEnabled() +} + +func (z *Zones) withInfo() (slog.Logger, bool) { + return z.debug().WithEnabled() +} + +func (z *Zones) debug() slog.Logger { + return z.log.Debug() +} + +func (z *Zones) info() slog.Logger { + return z.log.Info() +} + +func (z *Zones) warn(err error) slog.Logger { + l := z.log.Warn() + if err != nil { + l = l.WithField(slog.ErrorFieldName, err) + } + return l +} + +func (z *Zones) error(err error) slog.Logger { + l := z.log.Error() + if err != nil { + l = l.WithField(slog.ErrorFieldName, err) + } + return l +} diff --git a/pkg/zones/options.go b/pkg/zones/options.go index faefb9f..e7a66f2 100644 --- a/pkg/zones/options.go +++ b/pkg/zones/options.go @@ -69,6 +69,7 @@ func WithLogger(log slog.Logger) ScanOption { } opt.Logger = log + m.log = log return nil } } diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index fad4c30..89d2509 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -6,6 +6,7 @@ import ( "sort" "darvaza.org/resolver" + "darvaza.org/slog" "github.com/gofrs/uuid/v5" ) @@ -141,6 +142,7 @@ func (z *Zone) GatewayIDs() ([]int, int) { // Zones represents all zones in a cluster type Zones struct { dir fs.FS + log slog.Logger resolver resolver.Resolver domain string -- 2.17.1 From 1bca1f7da166f07736fad977e02bf4beccd8f9df Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 14:51:01 +0000 Subject: [PATCH 3/3] zones: add logger to Zone and Machine Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 2 ++ pkg/zones/scan.go | 10 ++++++---- pkg/zones/zones.go | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index 8500e9e..2e84f6d 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -10,6 +10,8 @@ import ( // A Machine is a machine on a Zone type Machine struct { zone *Zone + logger + ID int `toml:"id"` Name string `toml:"-" json:"-" yaml:"-"` diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index d13879a..bb07afc 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -32,8 +32,9 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error { for _, e := range entries { if e.IsDir() { z := &Zone{ - zones: m, - Name: e.Name(), + zones: m, + logger: m, + Name: e.Name(), } if err := z.scan(); err != nil { @@ -132,8 +133,9 @@ func (z *Zone) scan() error { for _, e := range entries { if e.IsDir() { m := &Machine{ - zone: z, - Name: e.Name(), + zone: z, + logger: z, + Name: e.Name(), } if err := m.init(); err != nil { diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index 89d2509..3872971 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -88,6 +88,7 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) // Zone represents one zone in a cluster type Zone struct { zones *Zones + logger ID int `toml:"id"` Name string `toml:"name"` -- 2.17.1