From 8ef547df3c2983524b030b6e38e308f95598299b Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 25 Jan 2024 15:44:56 -0800 Subject: [PATCH] Add FQDNWithContext functions to prevent breaking change --- providers/aix/host_aix_ppc64.go | 8 ++++++-- providers/darwin/host_darwin.go | 8 ++++++-- providers/linux/host_linux.go | 8 ++++++-- providers/shared/fqdn.go | 9 +++++++-- providers/windows/host_windows.go | 6 +++++- types/host.go | 8 ++++++-- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/providers/aix/host_aix_ppc64.go b/providers/aix/host_aix_ppc64.go index f65e58e..dafe062 100644 --- a/providers/aix/host_aix_ppc64.go +++ b/providers/aix/host_aix_ppc64.go @@ -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) { diff --git a/providers/darwin/host_darwin.go b/providers/darwin/host_darwin.go index 201e98b..70862e8 100644 --- a/providers/darwin/host_darwin.go +++ b/providers/darwin/host_darwin.go @@ -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) { diff --git a/providers/linux/host_linux.go b/providers/linux/host_linux.go index e23edd1..c2bdeb4 100644 --- a/providers/linux/host_linux.go +++ b/providers/linux/host_linux.go @@ -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. diff --git a/providers/shared/fqdn.go b/providers/shared/fqdn.go index ae84704..48043bf 100644 --- a/providers/shared/fqdn.go +++ b/providers/shared/fqdn.go @@ -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. @@ -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) @@ -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) diff --git a/providers/windows/host_windows.go b/providers/windows/host_windows.go index 7bda006..f7b527b 100644 --- a/providers/windows/host_windows.go +++ b/providers/windows/host_windows.go @@ -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) @@ -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{} diff --git a/types/host.go b/types/host.go index f995718..27ea488 100644 --- a/types/host.go +++ b/types/host.go @@ -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