From 45447275a76c151c9b8c15c6d811b6d4f24efe87 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 13:44:18 +0000 Subject: [PATCH] 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 }