Skip to content

Commit

Permalink
Reduce function complexity
Browse files Browse the repository at this point in the history
to satisfy gocyclo
  • Loading branch information
corny committed Jun 17, 2018
1 parent 6aedb0a commit 48b5bf6
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions cmd/ping-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (
)

var (
args []string
attempts uint = 3
timeout = time.Second
proto4, proto6 bool
size uint = 56
bind string

destination string
remoteAddr *net.IPAddr
pinger *ping.Pinger
)

func main() {
Expand Down Expand Up @@ -46,15 +51,13 @@ func main() {
}

args := flag.Args()

var pinger *ping.Pinger
var remote *net.IPAddr
destination := args[0]

if proto4 {
if r, err := net.ResolveIPAddr("ip4", args[0]); err != nil {
if r, err := net.ResolveIPAddr("ip4", destination); err != nil {
panic(err)
} else {
remote = r
remoteAddr = r
}

if p, err := ping.New(bind, ""); err != nil {
Expand All @@ -63,10 +66,10 @@ func main() {
pinger = p
}
} else if proto6 {
if r, err := net.ResolveIPAddr("ip6", args[0]); err != nil {
if r, err := net.ResolveIPAddr("ip6", destination); err != nil {
panic(err)
} else {
remote = r
remoteAddr = r
}

if p, err := ping.New("", bind); err != nil {
Expand All @@ -81,29 +84,35 @@ func main() {
pinger.SetPayloadSize(uint16(size))
}

if remote.IP.IsLinkLocalMulticast() {
fmt.Printf("multicast ping to %s (%s)\n", args[0], remote)
if remoteAddr.IP.IsLinkLocalMulticast() {
multicastPing()
} else {
unicastPing()
}
}

responses, err := pinger.PingMulticast(remote, timeout)
func unicastPing() {
rtt, err := pinger.PingAttempts(remoteAddr, timeout, int(attempts))

if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}

for response := range responses {
fmt.Printf("%+v\n", response)
}
fmt.Printf("ping %s (%s) rtt=%v\n", destination, remoteAddr, rtt)
}

} else {
// non-multicast
rtt, err := pinger.PingAttempts(remote, timeout, int(attempts))
func multicastPing() {
fmt.Printf("multicast ping to %s (%s)\n", args[0], destination)

if err != nil {
fmt.Println(err)
os.Exit(1)
}
responses, err := pinger.PingMulticast(remoteAddr, timeout)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Printf("ping %s (%s) rtt=%v\n", args[0], remote, rtt)
for response := range responses {
fmt.Printf("%+v\n", response)
}
}

0 comments on commit 48b5bf6

Please sign in to comment.