Browse Source

Merge pull request 'zones: pass logger from cmd to Zones, Zone, and Machine' (#12)

Reviewed-on: #12
pull/13/head v0.5.2
Alejandro Mery 1 year ago
parent
commit
8c32b88e24
  1. 1
      cmd/jpictl/config.go
  2. 1
      cmd/jpictl/log.go
  3. 1
      go.mod
  4. 2
      go.sum
  5. 49
      pkg/zones/log.go
  6. 2
      pkg/zones/machine.go
  7. 27
      pkg/zones/options.go
  8. 10
      pkg/zones/scan.go
  9. 3
      pkg/zones/zones.go

1
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),
)
}

1
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)
)

1
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

2
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=

49
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
}

2
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:"-"`

27
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,19 @@ 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
m.log = log
return nil
}
}
func (m *Zones) setDefaults(opt *ScanOptions) error {
if m.resolver == nil {
h := resolver.NewCloudflareLookuper()
@ -64,6 +83,12 @@ func (m *Zones) setDefaults(opt *ScanOptions) error {
}
}
if opt.Logger == nil {
if err := WithLogger(nil)(m, opt); err != nil {
return err
}
}
return nil
}

10
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 {

3
pkg/zones/zones.go

@ -6,6 +6,7 @@ import (
"sort"
"darvaza.org/resolver"
"darvaza.org/slog"
"github.com/gofrs/uuid/v5"
)
@ -87,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"`
@ -141,6 +143,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

Loading…
Cancel
Save