package main import ( "fmt" "os" "os/exec" "runtime" "strings" ) func GetOsInfo() string { var osInfo string osInfoByte, err := os.ReadFile("/etc/os-release") if err != nil { fmt.Println(err) } osInfoStr := string(osInfoByte) for _, envStr := range strings.Split(osInfoStr, "\n") { envStrArr := strings.Split(envStr, "=") if envStrArr[0] == "PRETTY_NAME" { osInfo = envStrArr[1] break } } return osInfo } func GetUser() string { var user string for _, envStr := range os.Environ() { envStrArr := strings.Split(envStr, "=") if envStrArr[0] == "USER" { user = envStrArr[1] break } } return user } func GetShell() string { var shell string for _, envStr := range os.Environ() { envStrArr := strings.Split(envStr, "=") if envStrArr[0] == "SHELL" { shell = envStrArr[1] break } } return shell } func GetKernelVersion() string { var out strings.Builder cmd := exec.Command("uname", "-r") cmd.Stdout = &out err := cmd.Run() if err != nil { fmt.Println(err) } return out.String() } func GetHostname() string { hostnameByte, err := os.ReadFile("/etc/hostname") if err != nil { fmt.Println(err) } hostname := string(hostnameByte) return strings.Fields(hostname)[0] } func GetUptime() string { hostnameByte, err := os.ReadFile("/proc/uptime") if err != nil { fmt.Println(err) } hostname := string(hostnameByte) return strings.Fields(hostname)[0] } func GetAnsiColor() string { var ansiColor string ansiColorByte, err := os.ReadFile("/etc/os-release") if err != nil { fmt.Println(err) } ansiColorStr := string(ansiColorByte) for _, envStr := range strings.Split(ansiColorStr, "\n") { envStrArr := strings.Split(envStr, "=") if envStrArr[0] == "ANSI_COLOR" { ansiColor = strings.Split(envStrArr[1], "\"")[1] break } } return ansiColor } func GenBreaker(user string, hostname string) string { var breaker strings.Builder for i := 0; i < (len(user) + len(hostname) + 1); i++ { fmt.Fprintf(&breaker, "=") } return breaker.String() } func main() { if runtime.GOOS != "linux" { fmt.Println("OS not support") os.Exit(0) } user := GetUser() hostname := GetHostname() ansiColor := GetAnsiColor() fmt.Printf("%v@%v\n", user, hostname) fmt.Printf("\033[%vm%v\033[0m\n", ansiColor, GenBreaker(user, hostname)) fmt.Printf("\033[%vmOS\033[0m:\t%v\n", ansiColor, GetOsInfo()) fmt.Printf("\033[%vmKERNEL\033[0m:\t%v", ansiColor, GetKernelVersion()) fmt.Printf("\033[%vmSHELL\033[0m:\t%v\n", ansiColor, GetShell()) fmt.Printf("\033[%vmUPTIME\033[0m:\t%v\n", ansiColor, GetUptime()) }