github.com/sentienttechnologies/studio-go-runner@v0.0.0-20201118202441-6d21f2ced8ee/internal/runner/host.go (about) 1 // Copyright 2018-2020 (c) Cognizant Digital Business, Evolutionary AI. All rights reserved. Issued under the Apache 2.0 License. 2 3 package runner 4 5 import ( 6 "net" 7 "os" 8 9 "github.com/karlmutch/go-fqdn" 10 ) 11 12 // This file contains networking and hosting related functions used by the runner for reporting etc 13 // 14 15 // GetHostName returns a human readable host name that contains as much useful context 16 // as can be gathered 17 // 18 func GetHostName() (name string) { 19 20 name = fqdn.Get() 21 if 0 != len(name) && name != "unknown" { 22 return name 23 } 24 25 name, _ = os.Hostname() 26 27 if 0 != len(name) { 28 return name 29 } 30 31 addrs, err := net.InterfaceAddrs() 32 if err != nil { 33 os.Stderr.WriteString("Oops: " + err.Error() + "\n") 34 os.Exit(1) 35 } 36 37 for _, a := range addrs { 38 if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 39 if ipnet.IP.To4() != nil { 40 return (ipnet.IP.String()) 41 } 42 } 43 } 44 return "unknown" 45 }