package util import ( "bufio" "errors" "os" "strings" ) // HostnameFromFile : Returns the contents of the /etc/hostname file func HostnameFromFile() (string, error) { file, err := os.Open("/etc/hostname") if err != nil { return "", err } defer file.Close() s := bufio.NewScanner(file) for s.Scan() { readLine := s.Text() hostname := strings.TrimRightFunc(readLine, func(c rune) bool { return c == '\r' || c == '\n' }) return hostname, nil } return "", errors.New("no lines in /etc/hostname") }