diff --git a/go.mod b/go.mod index 1dcfccb..1188237 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module termiboard go 1.14 require ( - github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect + github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d github.com/go-ole/go-ole v1.2.4 // indirect + github.com/jaypipes/ghw v0.6.1 // indirect github.com/shirou/gopsutil v2.20.8+incompatible golang.org/x/sys v0.0.0-20200929083018-4d22bbb62b3c // indirect ) diff --git a/main.go b/main.go index d9387e5..2e12e1d 100644 --- a/main.go +++ b/main.go @@ -10,19 +10,20 @@ const ( BannerBlueColor = "\033[1;36m%s\033[0m" WarningYellowColor = "\033[1;33m%s\033[0m" ErrorRedColor = "\033[1;31m%s\033[0m" - BoldWhite = "\033[1;37m%s\033[0m" + BoldWhiteColor = "\033[1;37m%s\033[0m" None = "\033[0m%s\033[0m" ) var ( - showCPUInfo *bool - showCPUUsage *bool - showRAM *bool - showDisk *bool - showLocalIP *bool - showPublicIP *bool - showAll *bool - show5TopRAM *bool + showCPUInfo *bool + showCPUUsage *bool + showRAM *bool + showDisk *bool + showLocalIP *bool + showPublicIP *bool + showAll *bool + show5TopRAM *bool + showPCIDevices *bool ) func main() { @@ -34,12 +35,14 @@ func main() { showLocalIP = flag.Bool("local-ip", false, "Show local IP address") showPublicIP = flag.Bool("public-ip", false, "Show public IP address") show5TopRAM = flag.Bool("top5-ram", false, "Show top 5 process that consume the most memory") + showPCIDevices = flag.Bool("pci-devices", false, "Show all PCI Devices") showAll = flag.Bool("all", false, "Show all stats") flag.Parse() if flag.NFlag() == 0 { *showAll = true } + var functionsWithConditions = []struct { condition bool function func() @@ -59,6 +62,19 @@ func main() { pair.function() } } + + //For Function called using their flags only + var functionCallArgs = []struct { + condition bool + function func() + }{ + {*showPCIDevices, GetPCIDevices}, + } + for _, pair := range functionCallArgs { + if pair.condition { + pair.function() + } + } } func printBanner() { diff --git a/memory.go b/memory.go index ef219f2..c1fd8a4 100644 --- a/memory.go +++ b/memory.go @@ -64,13 +64,13 @@ func GetTopProcesses() { return memoryPercentOfIthProcess > memoryPercentOfJthProcess }) - //Windows Compatiblilty(Leave out the System Idle Process), for more info refer #30 - if runtime.GOOS == "windows" { - if processes[0].Pid == 0 { - processes = processes[1:] - } - } + //Windows Compatiblilty(Leave out the System Idle Process), for more info refer #30 + if runtime.GOOS == "windows" { + if processes[0].Pid == 0 { + processes = processes[1:] + } + } sort.Slice(processes, func(i, j int) bool { memoryPercentOfIthProcess, err := processes[i].MemoryPercent() diff --git a/network.go b/network.go index 8d7b610..6df75b2 100644 --- a/network.go +++ b/network.go @@ -32,7 +32,7 @@ func GetLocalIPAddress() { } ResultPrinter("Local IP Address:", "") for _, networkInterface := range networkInterfaces { //Iterate over each Network Interface Card - StandardPrinter(BoldWhite, "\t["+string(networkInterface.Name)+"] "+networkInterface.HardwareAddr.String()) // Print Name and Hardware Address + StandardPrinter(BoldWhiteColor, "\t["+string(networkInterface.Name)+"] "+networkInterface.HardwareAddr.String()) // Print Name and Hardware Address interfaceAddresses, err := networkInterface.Addrs() //Get the Internet Addresses of a Network Interface if err == nil { for _, interfaceAddress := range interfaceAddresses { diff --git a/pci.go b/pci.go new file mode 100644 index 0000000..c7450af --- /dev/null +++ b/pci.go @@ -0,0 +1,60 @@ +// +build !windows + +package main + +import ( + "fmt" + "os" + "text/tabwriter" + + "github.com/jaypipes/ghw" +) + +func GetPCIDevices() { + + ResultPrinter("Host PCI devices:", "") + + //Create a Tab writer to STDout with padding of 3, space between column and Left Align column + const padding = 3 + w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', 0) + //Always Flush the Tab Writer because it is line buffered + defer w.Flush() + + //TODO: Implement StandardPrinter for tabwriter + //Print Header Row of the table + fmt.Fprintln(w, "Class\tManufacturer\tName\t") + + ////TODO: Implement StandardPrinter for tabwriter + //Print Seperator for Header and Data of the table + fmt.Fprintln(w, "-----\t------------\t----\t") + + pci, err := ghw.PCI() + if err != nil { + fmt.Printf("Error getting PCI info: %v", err) + } + + devices := pci.ListDevices() + if len(devices) == 0 { + fmt.Printf("error: could not retrieve PCI devices\n") + return + } + + for _, device := range devices { + vendor := device.Vendor + vendorName := vendor.Name + if len(vendor.Name) > 20 { + //Product Name Shorten + vendorName = string([]byte(vendorName)[0:17]) + "..." + } + product := device.Product + productName := product.Name + if len(product.Name) > 40 { + //Product Name Shorten + productName = string([]byte(productName)[0:37]) + "..." + } + //TODO: Implement StandardPrinter for tabwriter + //Print Data Row of the table + fmt.Fprintln(w, device.Class.Name+"\t"+vendorName+"\t"+productName+"\t") + } + +} diff --git a/pci_windows.go b/pci_windows.go new file mode 100644 index 0000000..7dcf239 --- /dev/null +++ b/pci_windows.go @@ -0,0 +1,53 @@ +// Reference Documentation: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity +package main + +import ( + "fmt" + "os" + "text/tabwriter" + + "github.com/StackExchange/wmi" +) + +//Parameters are selected such that they match with Linux Counterpart as well +type PnPEntity struct { + PNPClass string + Name string + Manufacturer string +} + +func GetPCIDevices() { + + ResultPrinter("Host PCI devices:", "") + + //Create a Tab writer to STDout with padding of 3, space between column and Left Align column + const padding = 3 + w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', 0) + //Always Flush the Tab Writer because it is line buffered + defer w.Flush() + + //Create a slice for holding info about PCIDevices with PnPEntity properties + var PCIDevices []PnPEntity + //Query to select only the PCI Devices from Win32_PnPEntity + err := wmi.Query("Select * from Win32_PnPEntity where PNPDeviceID like '%PCI%'", &PCIDevices) + if err != nil { + StandardPrinter(ErrorRedColor, "Could not retrieve PCI Devices") + panic(err) + return + } + + //TODO: Implement StandardPrinter for tabwriter + //Print Header Row of the table + fmt.Fprintln(w, "Class\tManufacturer\tName\t") + + //TODO: Implement StandardPrinter for tabwriter + //Print Seperator for Header and Data of the table + fmt.Fprintln(w, "-----\t------------\t----\t") + + for _, PCIDevice := range PCIDevices { + //TODO: Implement StandardPrinter for tabwriter + //Print Data Row of the table + fmt.Fprintln(w, PCIDevice.PNPClass+"\t"+PCIDevice.Manufacturer+"\t"+PCIDevice.Name+"\t") + } + +}