From 9af88f659373d5063bca83ab145a6d22e3ed1ef9 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 15:35:37 +0000 Subject: [PATCH] zones: introduce ScanOption/ScanOptions for New()/NewFS() Signed-off-by: Alejandro Mery --- pkg/zones/options.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ pkg/zones/scan.go | 16 +++++------ pkg/zones/zones.go | 35 ------------------------ 3 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 pkg/zones/options.go diff --git a/pkg/zones/options.go b/pkg/zones/options.go new file mode 100644 index 0000000..11b2afb --- /dev/null +++ b/pkg/zones/options.go @@ -0,0 +1,63 @@ +package zones + +import ( + "io/fs" + "path/filepath" + + "darvaza.org/resolver" + "github.com/hack-pad/hackpadfs/os" +) + +// A ScanOption preconfigures the Zones before scanning +type ScanOption func(*Zones, *ScanOptions) error + +// ScanOptions contains flags used by the initial scan +type ScanOptions struct{} + +func (*Zones) setDefaults(_ *ScanOptions) error { + return nil +} + +// NewFS builds a [Zones] tree using the given directory +func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Zones, error) { + var scanOptions ScanOptions + + lockuper := resolver.NewCloudflareLookuper() + + z := &Zones{ + dir: dir, + resolver: resolver.NewResolver(lockuper), + domain: domain, + } + + for _, opt := range opts { + if err := opt(z, &scanOptions); err != nil { + return nil, err + } + } + + if err := z.setDefaults(&scanOptions); err != nil { + return nil, err + } + + if err := z.scan(&scanOptions); err != nil { + return nil, err + } + + return z, nil +} + +// New builds a [Zones] tree using the given directory +func New(dir, domain string, opts ...ScanOption) (*Zones, error) { + dir, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + + base, err := os.NewFS().Sub(dir[1:]) + if err != nil { + return nil, err + } + + return NewFS(base, domain, opts...) +} diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index 8faf8a8..2aa24d7 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -5,15 +5,15 @@ import ( "sort" ) -func (m *Zones) scan() error { - for _, fn := range []func() error{ +func (m *Zones) scan(opts *ScanOptions) error { + for _, fn := range []func(*ScanOptions) error{ m.scanDirectory, m.scanMachines, m.scanZoneIDs, m.scanSort, m.scanGateways, } { - if err := fn(); err != nil { + if err := fn(opts); err != nil { return err } } @@ -21,7 +21,7 @@ func (m *Zones) scan() error { return nil } -func (m *Zones) scanDirectory() error { +func (m *Zones) scanDirectory(_ *ScanOptions) error { // each directory is a zone entries, err := fs.ReadDir(m.dir, ".") if err != nil { @@ -46,7 +46,7 @@ func (m *Zones) scanDirectory() error { return nil } -func (m *Zones) scanMachines() error { +func (m *Zones) scanMachines(_ *ScanOptions) error { var err error m.ForEachMachine(func(p *Machine) bool { err = p.scan() @@ -55,7 +55,7 @@ func (m *Zones) scanMachines() error { return err } -func (m *Zones) scanZoneIDs() error { +func (m *Zones) scanZoneIDs(_ *ScanOptions) error { var hasMissing bool var lastZoneID int @@ -85,7 +85,7 @@ func (m *Zones) scanZoneIDs() error { return nil } -func (m *Zones) scanSort() error { +func (m *Zones) scanSort(_ *ScanOptions) error { sort.SliceStable(m.Zones, func(i, j int) bool { id1 := m.Zones[i].ID id2 := m.Zones[j].ID @@ -111,7 +111,7 @@ func (m *Zones) scanSort() error { return nil } -func (m *Zones) scanGateways() error { +func (m *Zones) scanGateways(_ *ScanOptions) error { var err error m.ForEachZone(func(z *Zone) bool { diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index ae9eb33..f25f264 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -3,11 +3,8 @@ package zones import ( "io/fs" - "path/filepath" "sort" - "github.com/hack-pad/hackpadfs/os" - "darvaza.org/resolver" ) @@ -190,35 +187,3 @@ func (m *Zones) GetMachineByName(name string) (*Machine, bool) { return out, out != nil } - -// NewFS builds a [Zones] tree using the given directory -func NewFS(dir fs.FS, domain string) (*Zones, error) { - lockuper := resolver.NewCloudflareLookuper() - - z := &Zones{ - dir: dir, - resolver: resolver.NewResolver(lockuper), - domain: domain, - } - - if err := z.scan(); err != nil { - return nil, err - } - - return z, nil -} - -// New builds a [Zones] tree using the given directory -func New(dir, domain string) (*Zones, error) { - dir, err := filepath.Abs(dir) - if err != nil { - return nil, err - } - - base, err := os.NewFS().Sub(dir[1:]) - if err != nil { - return nil, err - } - - return NewFS(base, domain) -}