-
Notifications
You must be signed in to change notification settings - Fork 180
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
Windows support: net #157
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} else { | ||
name = description.Description | ||
} | ||
return name | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theDescription
field ifNetConnectionID
is non-null. Otherwise it's likely you'll end up with NICs named "Wired Connection - Wired Connection" or something like that..There was a problem hiding this comment.
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":
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.
There was a problem hiding this comment.
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!