Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support: net #157

Merged
merged 3 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/jaypipes/ghw
go 1.12

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d
github.com/ghodss/yaml v1.0.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jaypipes/pcidb v0.5.0
github.com/pkg/errors v0.8.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jaypipes/pcidb v0.5.0 h1:4W5gZ+G7QxydevI8/MmmKdnIPJpURqJ2JNXTzfLxF5c=
Expand Down
2 changes: 1 addition & 1 deletion net_stub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux
// +build !linux,!windows
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
Expand Down
65 changes: 65 additions & 0 deletions net_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package ghw

import (
"strings"

"github.com/StackExchange/wmi"
)

const wqlNetworkAdapter = "SELECT Description, DeviceID, Index, InterfaceIndex, MACAddress, Manufacturer, Name, NetConnectionID, ProductName, ServiceName FROM Win32_NetworkAdapter"

type win32NetworkAdapter struct {
Description string
DeviceID string
Index uint32
InterfaceIndex uint32
MACAddress string
Manufacturer string
Name string
NetConnectionID string
ProductName string
ServiceName string
}

func (ctx *context) netFillInfo(info *NetworkInfo) error {
// Getting info from WMI
var win32NetDescriptions []win32NetworkAdapter
if err := wmi.Query(wqlNetworkAdapter, &win32NetDescriptions); err != nil {
return err
}

info.NICs = ctx.nics(win32NetDescriptions)
return nil
}

func (ctx *context) nics(win32NetDescriptions []win32NetworkAdapter) []*NIC {
// Converting into standard structures
nics := make([]*NIC, 0)
for _, nicDescription := range win32NetDescriptions {
nic := &NIC{
Name: ctx.netDeviceName(nicDescription),
MacAddress: nicDescription.MACAddress,
IsVirtual: false,
Capabilities: []*NICCapability{},
}
// Appenging NIC to NICs
nics = append(nics, nic)
}

return nics
}

func (ctx *context) netDeviceName(description win32NetworkAdapter) string {
var name string
if strings.TrimSpace(description.NetConnectionID) != "" {
name = description.NetConnectionID + " - " + description.Description
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would probably be better to just use NetConnectionID here and not append the Description field if NetConnectionID is non-null. Otherwise it's likely you'll end up with NICs named "Wired Connection - Wired Connection" or something like that..

Copy link
Contributor Author

@KingRial KingRial Feb 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was at the same idea at first, but during some tests on the field I found out that their union was more usefull (and sometimes, even mandatory).

Here a quick example on Windows Machine using the command "go run .\cmd\ghwc\main.go -f yaml net":

image

As you can see, their union often deepens the description of the device.

The Latins said: "Melius abundare quam deficere"
We could leave their union in the "name" field or hypothesize a new field.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) OK, you'd convinced me, Riccardo!

} else {
name = description.Description
}
return name
}