summaryrefslogtreecommitdiff
path: root/get_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'get_linux.go')
-rw-r--r--get_linux.go33
1 files changed, 31 insertions, 2 deletions
diff --git a/get_linux.go b/get_linux.go
index 9db8611..e7cd6d0 100644
--- a/get_linux.go
+++ b/get_linux.go
@@ -4,10 +4,39 @@ package hostname
// data to and from config files
import (
+ "bytes"
+ "fmt"
"os"
+ "os/exec"
+ "strings"
)
func osGetHostname() (string, error) {
- hostname, err := os.Hostname()
- return hostname, err
+ host, err := os.Hostname()
+ if err != nil {
+ return host, fmt.Errorf("failed to get hostname: %w", err)
+ }
+
+ domain, err := getDomainName()
+ if err != nil || domain == "" {
+ return host, err
+ }
+
+ return fmt.Sprintf("%s.%s", host, domain), nil
+}
+
+// getDomainName executes the 'domainname' command and returns its output.
+func getDomainName() (string, error) {
+ cmd := exec.Command("domainname")
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ if err := cmd.Run(); err != nil {
+ return "", fmt.Errorf("failed to execute 'domainname': %w", err)
+ }
+ domain := strings.TrimSpace(out.String())
+ if domain == "(none)" {
+ return "", ErrorDomainNameMisconfigured
+ }
+
+ return domain, nil
}