]> go.fuhry.dev Git - runtime.git/commitdiff
utils/hostname: fix build on darwin, support openbsd
authorDan Fuhry <dan@fuhry.com>
Tue, 27 Feb 2024 15:31:16 +0000 (10:31 -0500)
committerDan Fuhry <dan@fuhry.com>
Tue, 27 Feb 2024 15:31:29 +0000 (10:31 -0500)
utils/hostname/hostname.go
utils/hostname/hostname_macos.go
utils/hostname/hostname_openbsd.go [new file with mode: 0644]

index f99e32d3101370829bd19919afbf4738b2badb9f..65431fb9fb71a8961428bc536f4ade7f9e3f144c 100644 (file)
@@ -1,4 +1,4 @@
-//go:build !darwin
+//go:build !darwin && !openbsd
 
 package hostname
 
index 75e07dac383f70362393918e7f86584dabb98351..73337bd4f58bc83f62d38e11f63f70ee998bf8dd 100644 (file)
@@ -16,9 +16,9 @@ import (
        "howett.net/plist"
 )
 
-const (
-       defaultDomainName = constants.DefaultHostDomain
+var defaultDomainName = constants.DefaultHostDomain
 
+const (
        systemPreferencesPlist = "/Library/Preferences/SystemConfiguration/preferences.plist"
 )
 
diff --git a/utils/hostname/hostname_openbsd.go b/utils/hostname/hostname_openbsd.go
new file mode 100644 (file)
index 0000000..caf8ebe
--- /dev/null
@@ -0,0 +1,91 @@
+// go:build openbsd
+
+package hostname
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "net"
+       "os"
+       "strings"
+       "time"
+
+       "go.fuhry.dev/runtime/constants"
+       "go.fuhry.dev/runtime/utils/log"
+)
+
+var defaultDomainName = constants.DefaultHostDomain
+var cachedFqdn string
+
+func Hostname() string {
+       fqdn := Fqdn()
+       return strings.Split(fqdn, ".")[0]
+}
+
+func DomainName() string {
+       fqdn := strings.Split(Fqdn(), ".")
+       if len(fqdn) > 1 {
+               return strings.Join(fqdn[1:], ".")
+       }
+       return defaultDomainName
+}
+
+func RegionName() string {
+       return strings.Split(DomainName(), ".")[0]
+}
+
+func Fqdn() string {
+       if cachedFqdn != "" {
+               return cachedFqdn
+       }
+       cachedFqdn = fqdn()
+       return cachedFqdn
+}
+
+func fqdn() string {
+       myName, myNameErr := os.ReadFile("/etc/myname")
+       if myNameErr == nil {
+               return maybeAppendDefaultDomainName(strings.Trim(string(myName), " \r\n"))
+       }
+
+       dnsFqdn, dnsErr := fqdnFromDns()
+       if dnsErr == nil {
+               return maybeAppendDefaultDomainName(dnsFqdn)
+       }
+
+       log.Panicf("failed to get fqdn: myname failed because %q, and dns failed because %q", myNameErr.Error(), dnsErr.Error())
+       return ""
+}
+
+func maybeAppendDefaultDomainName(name string) string {
+       parts := strings.Split(name, ".")
+       if len(parts) < 2 {
+               name = fmt.Sprintf("%s.%s", parts[0], defaultDomainName)
+       }
+
+       log.Default().V(1).Noticef("detected system hostname: %q", name)
+
+       return name
+}
+
+func fqdnFromDns() (string, error) {
+       resolver := &net.Resolver{}
+
+       ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+       defer cancel()
+
+       result, err := resolver.LookupAddr(ctx, "127.0.0.1")
+       if err != nil {
+               return "", fmt.Errorf("failed to lookup hostname for 127.0.0.1: %v", err)
+       }
+
+       for _, name := range result {
+               parts := strings.Split(name, ".")
+               if parts[0] != "localhost" {
+                       return parts[0], nil
+               }
+       }
+
+       return "", errors.New("failed to lookup any hostname for 127.0.0.1 that does not resolve to just \"localhost\"")
+}