github.com/haraldrudell/parl@v0.4.176/pos/hostname.go (about)

     1  /*
     2  © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pos
     7  
     8  import (
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/haraldrudell/parl/perrors"
    13  )
    14  
    15  // ShortHostname gets hostname without domain part
    16  // This should never fail, when it does, panic is thrown
    17  func ShortHostname() (host string) {
    18  	var err error
    19  	if host, err = Hostname(); err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	return
    24  }
    25  
    26  // hostname without domain part
    27  func Hostname() (host string, err error) {
    28  	if host, err = os.Hostname(); perrors.IsPF(&err, "os.Hostname: %w", err) {
    29  		return
    30  	} else if index := strings.Index(host, "."); index != -1 {
    31  		host = host[:index]
    32  	}
    33  
    34  	return
    35  }