mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
63
modules/system/system_info.go
Normal file
63
modules/system/system_info.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// +build !windows
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type SystemInfo struct {
|
||||
ProductName string
|
||||
ProductVersion string
|
||||
BuildVersion string
|
||||
}
|
||||
|
||||
func NewSystemInfo() *SystemInfo {
|
||||
m := make(map[string]string)
|
||||
|
||||
arg := []string{}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
arg = append(arg, "-a")
|
||||
cmd = exec.Command("lsb_release", arg...)
|
||||
case "darwin":
|
||||
cmd = exec.Command("sw_vers", arg...)
|
||||
default:
|
||||
cmd = exec.Command("sw_vers", arg...)
|
||||
}
|
||||
|
||||
raw := wtf.ExecuteCommand(cmd)
|
||||
|
||||
for _, row := range strings.Split(raw, "\n") {
|
||||
parts := strings.Split(row, ":")
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
m[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
var sysInfo *SystemInfo
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
sysInfo = &SystemInfo{
|
||||
ProductName: m["Distributor ID"],
|
||||
ProductVersion: m["Description"],
|
||||
BuildVersion: m["Release"],
|
||||
}
|
||||
default:
|
||||
sysInfo = &SystemInfo{
|
||||
ProductName: m["ProductName"],
|
||||
ProductVersion: m["ProductVersion"],
|
||||
BuildVersion: m["BuildVersion"],
|
||||
}
|
||||
|
||||
}
|
||||
return sysInfo
|
||||
}
|
||||
36
modules/system/system_info_windows.go
Normal file
36
modules/system/system_info_windows.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// +build windows
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SystemInfo struct {
|
||||
ProductName string
|
||||
ProductVersion string
|
||||
BuildVersion string
|
||||
}
|
||||
|
||||
func NewSystemInfo() *SystemInfo {
|
||||
m := make(map[string]string)
|
||||
|
||||
cmd := exec.Command("powershell.exe", "(Get-CimInstance Win32_OperatingSystem).version")
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s := strings.Split(string(out), ".")
|
||||
m["ProductName"] = "Windows"
|
||||
m["ProductVersion"] = "Windows " + s[0] + "." + s[1]
|
||||
m["BuildVersion"] = s[2]
|
||||
|
||||
sysInfo := SystemInfo{
|
||||
ProductName: m["ProductName"],
|
||||
ProductVersion: m["ProductVersion"],
|
||||
BuildVersion: m["BuildVersion"],
|
||||
}
|
||||
|
||||
return &sysInfo
|
||||
}
|
||||
56
modules/system/widget.go
Normal file
56
modules/system/widget.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
systemInfo *SystemInfo
|
||||
Date string
|
||||
Version string
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, date, version string) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, "System", "system", false),
|
||||
|
||||
Date: date,
|
||||
Version: version,
|
||||
}
|
||||
|
||||
widget.systemInfo = NewSystemInfo()
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.View.SetText(
|
||||
fmt.Sprintf(
|
||||
"%8s: %s\n%8s: %s\n\n%8s: %s\n%8s: %s",
|
||||
"Built",
|
||||
widget.prettyDate(),
|
||||
"Vers",
|
||||
widget.Version,
|
||||
"OS",
|
||||
widget.systemInfo.ProductVersion,
|
||||
"Build",
|
||||
widget.systemInfo.BuildVersion,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) prettyDate() string {
|
||||
str, err := time.Parse(wtf.TimestampFormat, widget.Date)
|
||||
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return str.Format("Jan _2, 15:04")
|
||||
}
|
||||
Reference in New Issue
Block a user