Skip to content

Commit

Permalink
fix #20 report errors while executing arp command
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Sep 10, 2020
1 parent 75b5eb4 commit bc0a838
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *cmd) call(showProgress bool, msg string) (string, error) {
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
return "", errors.New(string(stderr.Bytes()))
return "", errors.New(err.Error() + " " + string(stderr.Bytes()))
}

if showProgress {
Expand All @@ -50,7 +50,7 @@ func (c *cmd) call(showProgress bool, msg string) (string, error) {
}

if err := cmd.Wait(); err != nil {
return "", errors.New(string(stderr.Bytes()))
return "", errors.New(err.Error() + " " + string(stderr.Bytes()))
}

return string(stdout.Bytes()), nil
Expand Down
9 changes: 6 additions & 3 deletions ip/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"strings"
)

func getArpTable() []addr {
func getArpTable() ([]addr, error) {

addrs := make([]addr, 0)

out, _ := command.Arp("-an").Call()
out, err := command.Arp("-an").Call()
if err != nil {
return nil, err
}
entries := strings.Split(out, "\n")

for _, entry := range entries {
Expand All @@ -27,5 +30,5 @@ func getArpTable() []addr {
}
}

return addrs
return addrs, nil
}
9 changes: 6 additions & 3 deletions ip/arp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"strings"
)

func getArpTable() []addr {
func getArpTable() ([]addr, error) {

addrs := make([]addr, 0)

s, _ := command.Arp("-a").Call()
s, err := command.Arp("-a").Call()
if err != nil {
return nil, err
}
ss := strings.Split(s, "\n\r")

for _, out := range ss {
Expand All @@ -25,5 +28,5 @@ func getArpTable() []addr {
}
}
}
return addrs
return addrs, nil
}
6 changes: 5 additions & 1 deletion ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func Find(vmName string, purge bool) (string, error) {
}

for {
arp := getArpTable()
arp, err := getArpTable()
if err != nil {
return "", err
}

for i := len(arp) - 1; i >= 0; i-- {
a := arp[i]
if a.mac == mac {
Expand Down

0 comments on commit bc0a838

Please sign in to comment.