Skip to content

Commit

Permalink
discovery/dns: Add prefix and suffix options
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Feb 23, 2024
1 parent 05fb016 commit ecc1b1e
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions discovery/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,38 @@ import (
)

type dnsDiscovery struct {
prefix, suffix string
}

func Discovery() options.Option {
type Option func(*dnsDiscovery)

func Prefix(p string) Option {
return func(dd *dnsDiscovery) {
dd.prefix = p
}
}

func Suffix(s string) Option {
return func(dd *dnsDiscovery) {
dd.suffix = s
}
}

func Discovery(opts ...Option) options.Option {
return func(o *options.Options) {
o.Discovery = &dnsDiscovery{}
dd := &dnsDiscovery{}

for _, opt := range opts {
opt(dd)
}

o.Discovery = dd
}
}

func (d *dnsDiscovery) Find(svc string) (host string, err error) {
svc = fmt.Sprintf("%s%s%s", d.prefix, svc, d.suffix)

ips, err := net.LookupIP(svc)
if err != nil {
return "", fmt.Errorf("lookup host: %w", err)
Expand Down

0 comments on commit ecc1b1e

Please sign in to comment.