You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.8 KiB
135 lines
2.8 KiB
1 year ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
|
"io/fs"
|
||
|
"path/filepath"
|
||
|
|
||
|
"darvaza.org/resolver"
|
||
1 year ago
|
"darvaza.org/slog"
|
||
|
"darvaza.org/slog/handlers/discard"
|
||
1 year ago
|
"github.com/hack-pad/hackpadfs/os"
|
||
|
)
|
||
|
|
||
1 year ago
|
// A ScanOption pre-configures the Zones before scanning
|
||
1 year ago
|
type ScanOption func(*Zones, *ScanOptions) error
|
||
|
|
||
|
// ScanOptions contains flags used by the initial scan
|
||
1 year ago
|
type ScanOptions struct {
|
||
|
// DontResolvePublicAddresses indicates we shouldn't
|
||
|
// pre-populate Machine.PublicAddresses during the
|
||
|
// initial scan
|
||
|
DontResolvePublicAddresses bool
|
||
1 year ago
|
|
||
|
// Logger specifies the logger to be used. otherwise
|
||
|
// the scanner will be mute
|
||
|
slog.Logger
|
||
1 year ago
|
}
|
||
|
|
||
|
// ResolvePublicAddresses instructs the scanner to use
|
||
|
// the DNS resolver to get PublicAddresses of nodes.
|
||
|
// Default is true
|
||
|
func ResolvePublicAddresses(resolve bool) ScanOption {
|
||
|
return func(m *Zones, opt *ScanOptions) error {
|
||
|
opt.DontResolvePublicAddresses = !resolve
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
// WithLookuper specifies what resolver.Lookuper to use to
|
||
|
// find public addresses
|
||
|
func WithLookuper(h resolver.Lookuper) ScanOption {
|
||
|
return func(m *Zones, opt *ScanOptions) error {
|
||
|
if h == nil {
|
||
|
return fs.ErrInvalid
|
||
|
}
|
||
|
m.resolver = resolver.NewResolver(h)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// WithResolver specifies what resolver to use to find
|
||
|
// public addresses. if nil is passed, the [net.Resolver] will be used.
|
||
|
// The default is using Cloudflare's 1.1.1.1.
|
||
|
func WithResolver(h resolver.Resolver) ScanOption {
|
||
|
return func(m *Zones, opt *ScanOptions) error {
|
||
|
if h == nil {
|
||
|
h = resolver.SystemResolver(true)
|
||
|
}
|
||
|
|
||
|
m.resolver = h
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// 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
|
||
1 year ago
|
m.log = log
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||
|
if m.resolver == nil {
|
||
|
h := resolver.NewCloudflareLookuper()
|
||
|
|
||
|
if err := WithLookuper(h)(m, opt); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
if opt.Logger == nil {
|
||
|
if err := WithLogger(nil)(m, opt); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
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
|
||
|
|
||
|
z := &Zones{
|
||
1 year ago
|
dir: dir,
|
||
|
domain: domain,
|
||
1 year ago
|
}
|
||
|
|
||
|
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...)
|
||
|
}
|