Skip to content

Commit

Permalink
Add FQDNWithContext functions to prevent breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jan 25, 2024
1 parent f64eb1b commit 8ef547d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
8 changes: 6 additions & 2 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ func (*host) Memory() (*types.HostMemoryInfo, error) {
return &mem, nil
}

func (h *host) FQDN(ctx context.Context) (string, error) {
return shared.FQDN(ctx)
func (h *host) FQDNWithContext(ctx context.Context) (string, error) {
return shared.FQDNWithContext(ctx)
}

func (h *host) FQDN() (string, error) {
return h.FQDNWithContext(context.Background())
}

func newHost() (*host, error) {
Expand Down
8 changes: 6 additions & 2 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ func (h *host) Memory() (*types.HostMemoryInfo, error) {
return &mem, nil
}

func (h *host) FQDN(ctx context.Context) (string, error) {
return shared.FQDN(ctx)
func (h *host) FQDNWithContext(ctx context.Context) (string, error) {
return shared.FQDNWithContext(ctx)
}

func (h *host) FQDN() (string, error) {
return h.FQDNWithContext(context.Background())
}

func (h *host) LoadAverage() (*types.LoadAverageInfo, error) {
Expand Down
8 changes: 6 additions & 2 deletions providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ func (h *host) Memory() (*types.HostMemoryInfo, error) {
return parseMemInfo(content)
}

func (h *host) FQDN(ctx context.Context) (string, error) {
return shared.FQDN(ctx)
func (h *host) FQDNWithContext(ctx context.Context) (string, error) {
return shared.FQDNWithContext(ctx)
}

func (h *host) FQDN() (string, error) {
return h.FQDNWithContext(context.Background())
}

// VMStat reports data from /proc/vmstat on linux.
Expand Down
9 changes: 7 additions & 2 deletions providers/shared/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"
)

// FQDN attempts to lookup the host's fully-qualified domain name and returns it.
// FQDNWithContext attempts to lookup the host's fully-qualified domain name and returns it.
// It does so using the following algorithm:
//
// 1. It gets the hostname from the OS. If this step fails, it returns an error.
Expand All @@ -41,7 +41,7 @@ import (
//
// 4. If steps 2 and 3 both fail, an empty string is returned as the FQDN along with
// errors from those steps.
func FQDN(ctx context.Context) (string, error) {
func FQDNWithContext(ctx context.Context) (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("could not get hostname to look for FQDN: %w", err)
Expand All @@ -50,6 +50,11 @@ func FQDN(ctx context.Context) (string, error) {
return fqdn(ctx, hostname)
}

// FQDN just calls FQDNWithContext with a background context.
func FQDN() (string, error) {
return FQDNWithContext(context.Background())
}

func fqdn(ctx context.Context, hostname string) (string, error) {
var errs error
cname, err := net.DefaultResolver.LookupCNAME(ctx, hostname)
Expand Down
6 changes: 5 additions & 1 deletion providers/windows/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *host) Memory() (*types.HostMemoryInfo, error) {
}, nil
}

func (h *host) FQDN(_ context.Context) (string, error) {
func (h *host) FQDNWithContext(_ context.Context) (string, error) {
fqdn, err := getComputerNameEx(stdwindows.ComputerNamePhysicalDnsFullyQualified)
if err != nil {
return "", fmt.Errorf("could not get windows FQDN: %s", err)
Expand All @@ -94,6 +94,10 @@ func (h *host) FQDN(_ context.Context) (string, error) {
return strings.ToLower(strings.TrimSuffix(fqdn, ".")), nil
}

func (h *host) FQDN() (string, error) {
return h.FQDNWithContext(context.Background())
}

func newHost() (*host, error) {
h := &host{}
r := &reader{}
Expand Down
8 changes: 6 additions & 2 deletions types/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ type Host interface {
Info() HostInfo
Memory() (*HostMemoryInfo, error)

// FQDN returns the fully-qualified domain name of the host, lowercased.
FQDN(ctx context.Context) (string, error)
// FQDNWithContext returns the fully-qualified domain name of the host, lowercased.
FQDNWithContext(ctx context.Context) (string, error)

// FQDN calls FQDNWithContext with a background context.
// Deprecated: Use FQDNWithContext instead.
FQDN() (string, error)
}

// NetworkCounters represents network stats from /proc/net
Expand Down

0 comments on commit 8ef547d

Please sign in to comment.