Skip to content

Commit

Permalink
Make struct members public
Browse files Browse the repository at this point in the history
  • Loading branch information
rakelkar-msft committed Sep 12, 2017
1 parent cc3f121 commit 6cc32fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
41 changes: 23 additions & 18 deletions netsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ type runner struct {

// Ipv4Interface models IPv4 interface output from: netsh interface ipv4 show addresses
type Ipv4Interface struct {
name string
interfaceMetric int
dhcpEnabled bool
ipAddress string
subnetPrefix int
gatewayMetric int
defaultGatewayAddress string
Name string
InterfaceMetric int
DhcpEnabled bool
IpAddress string
SubnetPrefix int
GatewayMetric int
DefaultGatewayAddress string
}

// New returns a new Interface which will exec netsh.
func New(exec utilexec.Interface) Interface {

if exec == nil {
exec = utilexec.New()
}

runner := &runner{
exec: exec,
}
Expand Down Expand Up @@ -101,7 +106,7 @@ func (runner *runner) GetInterfaces() ([]Ipv4Interface, error) {
}
match := quotedPattern.FindStringSubmatch(outputLine)
currentInterface = Ipv4Interface{
name: match[1],
Name: match[1],
}
} else {
parts := strings.SplitN(outputLine, ":", 2)
Expand All @@ -112,25 +117,25 @@ func (runner *runner) GetInterfaces() ([]Ipv4Interface, error) {
value := strings.TrimSpace(parts[1])
if strings.HasPrefix(key, "DHCP enabled") {
if value == "Yes" {
currentInterface.dhcpEnabled = true
currentInterface.DhcpEnabled = true
}
} else if strings.HasPrefix(key, "InterfaceMetric") {
if val, err := strconv.Atoi(value); err == nil {
currentInterface.interfaceMetric = val
currentInterface.InterfaceMetric = val
}
} else if strings.HasPrefix(key, "Gateway Metric") {
if val, err := strconv.Atoi(value); err == nil {
currentInterface.gatewayMetric = val
currentInterface.GatewayMetric = val
}
} else if strings.HasPrefix(key, "Subnet Prefix") {
match := cidrPattern.FindStringSubmatch(value)
if val, err := strconv.Atoi(match[1]); err == nil {
currentInterface.subnetPrefix = val
currentInterface.SubnetPrefix = val
}
} else if strings.HasPrefix(key, "IP Address") {
currentInterface.ipAddress = value
currentInterface.IpAddress = value
} else if strings.HasPrefix(key, "Default Gateway") {
currentInterface.defaultGatewayAddress = value
currentInterface.DefaultGatewayAddress = value
}
}
}
Expand Down Expand Up @@ -209,8 +214,8 @@ func (runner *runner) GetDefaultGatewayIfaceName() (string, error) {
}

for _, iface := range interfaces {
if iface.defaultGatewayAddress != "" {
return iface.name, nil
if iface.DefaultGatewayAddress != "" {
return iface.Name, nil
}
}

Expand All @@ -225,7 +230,7 @@ func (runner *runner) GetInterfaceByName(name string) (Ipv4Interface, error) {
}

for _, iface := range interfaces {
if iface.name == name {
if iface.Name == name {
return iface, nil
}
}
Expand All @@ -241,7 +246,7 @@ func (runner *runner) GetInterfaceByIP(ipAddr string) (Ipv4Interface, error) {
}

for _, iface := range interfaces {
if iface.ipAddress == ipAddr {
if iface.IpAddress == ipAddr {
return iface, nil
}
}
Expand Down
14 changes: 7 additions & 7 deletions netsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ Configuration for interface "Loopback Pseudo-Interface 1"
assert.EqualValues(t, strings.Split("netsh interface ipv4 show addresses", " "), fakeCmd.CombinedOutputLog[0])
assert.EqualValues(t, 5, len(interfaces))
assert.EqualValues(t, Ipv4Interface{
dhcpEnabled: true,
ipAddress: "10.88.48.68",
subnetPrefix: 22,
defaultGatewayAddress: "10.88.48.1",
gatewayMetric: 0,
interfaceMetric: 35,
name: "Wi-Fi",
DhcpEnabled: true,
IpAddress: "10.88.48.68",
SubnetPrefix: 22,
DefaultGatewayAddress: "10.88.48.1",
GatewayMetric: 0,
InterfaceMetric: 35,
Name: "Wi-Fi",
}, interfaces[2])
}

Expand Down

0 comments on commit 6cc32fb

Please sign in to comment.