Skip to content

Commit

Permalink
fix: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jul 5, 2023
1 parent 453fca8 commit 85e2eb7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions srv/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import (
"github.com/charmbracelet/wishlist"
)

const (
service = "_ssh._tcp"
txtPrefix = "wishlist.name "
)

// Endpoints returns the _ssh._tcp SRV records on the given domain as
// Wishlist endpoints.
func Endpoints(ctx context.Context, domain string) ([]*wishlist.Endpoint, error) {
log.Info("discovering _ssh._tcp SRV records", "domain", domain)
log.Debug("discovering SRV records", "service", service, "domain", domain)
_, srvs, err := net.DefaultResolver.LookupSRV(ctx, "ssh", "tcp", domain)
if err != nil {
return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err)
Expand All @@ -22,11 +27,11 @@ func Endpoints(ctx context.Context, domain string) ([]*wishlist.Endpoint, error)
if err != nil {
return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err)
}
return fromRecords(srvs, txts), nil
endpoints := fromRecords(srvs, txts)
log.Info("discovered from SRV records", "service", service, "domain", domain, "devices", len(endpoints))
return endpoints, nil
}

const txtPrefix = "wishlist.name "

func fromRecords(srvs []*net.SRV, txts []string) []*wishlist.Endpoint {
result := make([]*wishlist.Endpoint, 0, len(srvs))
for _, entry := range srvs {
Expand Down
4 changes: 3 additions & 1 deletion tailscale/tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Endpoints returns the found endpoints from tailscale.
func Endpoints(ctx context.Context, tailnet, key string) ([]*wishlist.Endpoint, error) {
log.Info("discovering from tailscale", "tailnet", tailnet)
log.Debug("discovering from tailscale", "tailnet", tailnet)
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
Expand Down Expand Up @@ -50,6 +50,8 @@ func Endpoints(ctx context.Context, tailnet, key string) ([]*wishlist.Endpoint,
Address: net.JoinHostPort(device.Addresses[0], "22"),
})
}

log.Info("discovered from tailscale", "tailnet", tailnet, "devices", len(endpoints))
return endpoints, nil
}

Expand Down
3 changes: 2 additions & 1 deletion zeroconf/zeroconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const service = "_ssh._tcp"

// Endpoints returns the found endpoints from zeroconf.
func Endpoints(ctx context.Context, domain string, timeout time.Duration) ([]*wishlist.Endpoint, error) {
log.Info("discovering from zeroconf", "service", service, "domain", domain)
log.Debug("discovering from zeroconf", "service", service, "domain", domain)
r, err := zeroconf.NewResolver()
if err != nil {
return nil, fmt.Errorf("zeroconf: could not create resolver: %w", err)
Expand All @@ -40,5 +40,6 @@ func Endpoints(ctx context.Context, domain string, timeout time.Duration) ([]*wi
Address: net.JoinHostPort(hostname, port),
})
}
log.Info("discovered from zeroconf", "service", service, "domain", domain, "devices", len(endpoints))
return endpoints, nil
}

0 comments on commit 85e2eb7

Please sign in to comment.