diff --git a/main.go b/main.go new file mode 100644 index 0000000..f7e0727 --- /dev/null +++ b/main.go @@ -0,0 +1,120 @@ +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() + + fmt.Printf("%v@%v\n", user, hostname) + fmt.Printf("\033[%vm%v\033[0m\n", GetAnsiColor(), GenBreaker(user, hostname)) + fmt.Printf("OS:\t%v\n", GetOsInfo()) + fmt.Printf("KERNEL:\t%v", GetKernelVersion()) + fmt.Printf("SHELL:\t%v\n", GetShell()) + fmt.Printf("UPTIME:\t%v\n", GetUptime()) +}