Browse Source

zones: introduce ScanOption/ScanOptions for New()/NewFS()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/5/head
Alejandro Mery 1 year ago
parent
commit
9af88f6593
  1. 63
      pkg/zones/options.go
  2. 16
      pkg/zones/scan.go
  3. 35
      pkg/zones/zones.go

63
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...)
}

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

35
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)
}

Loading…
Cancel
Save