-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlipstick-on-a-pig.go
211 lines (177 loc) · 5.45 KB
/
lipstick-on-a-pig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
// This generates a simple MOTD using Lipgloss by Charm. The colors are based on the hostname
// Porco 2024
import (
"fmt"
"os"
"os/exec"
"net"
"strings"
"strconv"
"runtime"
"crypto/md5"
"encoding/binary"
"github.com/charmbracelet/lipgloss"
"github.com/lucasb-eyer/go-colorful"
"golang.org/x/term"
)
// Style definitions.
var (
// General.
subtle = lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#383838"}
highlight = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
special = lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"}
)
/////////////////////////// Helper Functions
func hashToFloat(input string) float64 {
hash := md5.Sum([]byte(input))
hashInt := binary.BigEndian.Uint64(hash[:])
hashFloat := float64(hashInt % 360)
return hashFloat + 1
}
/////////////////////////// MAIN FUNCTION
func main() {
// Get OS information
osInfo := runtime.GOOS
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
// Get IP address
ipAddr := ""
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipAddr = ipnet.IP.String()
break
}
}
}
}
// Get OS version number and codename
var osVersion, osCodename string
if osInfo == "darwin" { // macOS
cmd := exec.Command("sw_vers", "-productVersion")
output, _ := cmd.Output()
osVersion = strings.TrimSpace(string(output))
cmd = exec.Command("sw_vers", "-productName")
output, _ = cmd.Output()
// productName := strings.TrimSpace(string(output))
switch osVersion {
case "10.10":
osCodename = "Yosemite"
case "10.11":
osCodename = "El Capitan"
case "10.12":
osCodename = "Sierra"
case "10.13":
osCodename = "High Sierra"
case "10.14":
osCodename = "Mojave"
case "10.15":
osCodename = "Catalina"
case "11.0":
osCodename = "Big Sur"
case "12.0":
osCodename = "Monterey"
case "13.0":
osCodename = "Ventura"
case "14.0":
osCodename = "Sonoma"
default:
osCodename = "Unknown"
}
osVersion = osVersion + " (" + osCodename + ")"
} else { // Linux
cmd := exec.Command("lsb_release", "-d")
output, _ := cmd.Output()
osVersion = strings.TrimSpace(strings.SplitN(string(output), ":", 2)[1])
}
// Get disk space information
var totalSpace, freeSpace, usedPercent string
if osInfo == "darwin" { // macOS
cmd := exec.Command("df", "-H", "/")
output, _ := cmd.Output()
diskInfo := strings.Fields(strings.Split(string(output), "\n")[1])
totalSpace = diskInfo[1]
freeSpace = diskInfo[3]
usedPercent = diskInfo[4]
} else { // Linux
cmd := exec.Command("df", "-H", "--output=size,avail,pcent", "/")
output, _ := cmd.Output()
diskInfo := strings.Fields(strings.Split(string(output), "\n")[1])
totalSpace = diskInfo[0]
freeSpace = diskInfo[1]
usedPercent = diskInfo[2]
}
var uptime string
if osInfo == "darwin" { // macOS
cmd := exec.Command("uptime")
output, _ := cmd.Output()
uptime = strings.TrimSpace(strings.SplitN(string(output), ",", 2)[0])
} else { // Linux
cmd := exec.Command("uptime", "-p")
output, _ := cmd.Output()
uptime = strings.TrimSpace(string(output))
}
// Get CPU usage
cmd := exec.Command("ps", "-A", "-o", "%cpu")
output, _ := cmd.Output()
cpuInfo := string(output)
cpuUsage := strings.Split(cpuInfo, "\n")[1] // Assuming the first line contains the CPU usage
// Get RAM usage
var ramUsage string
if osInfo == "darwin" { // macOS
cmd := exec.Command("top", "-l", "1", "-s", "0", "-n", "0")
output, _ := cmd.Output()
lines := strings.Split(string(output), "\n")
memLine := lines[4] // Assuming the fifth line contains the memory usage
memFields := strings.Fields(memLine)
usedMem := strings.Trim(memFields[1], "M")
totalMem := strings.Trim(memFields[3], "M")
used, _ := strconv.Atoi(usedMem)
total, _ := strconv.Atoi(totalMem)
ramUsage = fmt.Sprintf("Used: %d MB / Total: %d MB", used, total)
} else { // Linux
cmd := exec.Command("free", "-m")
output, _ := cmd.Output()
lines := strings.Split(string(output), "\n")
memLine := lines[1] // Assuming the second line contains the memory usage
memFields := strings.Fields(memLine)
usedMem := memFields[2]
totalMem := memFields[1]
used, _ := strconv.Atoi(usedMem)
total, _ := strconv.Atoi(totalMem)
ramUsage = fmt.Sprintf("Used: %d MB / Total: %d MB", used, total)
}
physicalWidth, _, _ := term.GetSize(int(os.Stdout.Fd()))
result := hashToFloat(hostname)
c := colorful.Hcl(result, 0.5, 0.5)
hostcolor := c.Hex()
hostStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color(hostcolor)).
Bold(true).
Width((physicalWidth/3)-2).
Padding(1).
Align(lipgloss.Center).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63"))
bannerStyle := lipgloss.NewStyle().
MarginLeft(1).
Padding(0, 1).
Width(((physicalWidth/3)*2)-2).
Align(lipgloss.Left)
infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(hostcolor)).Render
host := hostStyle.Render(hostname)
banner := bannerStyle.Render(" OS:", infoStyle(osInfo), "Version:", infoStyle(osVersion),
"\n","IP Address:", infoStyle(ipAddr),
"\n","Total:", infoStyle(totalSpace),"Free:", infoStyle(freeSpace),"Used Percent:", infoStyle(usedPercent),
"\n","Uptime:", infoStyle(uptime),
"\n","RAM Usage:", infoStyle(ramUsage),
"\n","CPU Usage:", infoStyle(cpuUsage))
row := lipgloss.JoinHorizontal(lipgloss.Center, host, banner)
fmt.Println(row)
}