From 49a3e1ace3c4075946191f637542b757c87a90f3 Mon Sep 17 00:00:00 2001 From: B4D_US3R Date: Sun, 4 Aug 2024 02:19:33 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 main.go 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()) +}