Skip to content

Commit

Permalink
cli: Lookup host on init unless ip given
Browse files Browse the repository at this point in the history
  • Loading branch information
tonobo committed Feb 20, 2019
1 parent c96811f commit 57a3d9f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cli

import (
"errors"
"fmt"
"log"
"net"
"sync"
"time"

Expand Down Expand Up @@ -30,16 +31,22 @@ var (

// rootCmd represents the root command
var RootCmd = &cobra.Command{
Use: "mtr TARGET",
Use: "mtr TARGET",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if versionFlag {
fmt.Printf("MTR Version: %s, build date: %s\n", version, date)
return nil
}
if len(args) != 1 {
return errors.New("No target provided")
ip := args[0]
if net.ParseIP(args[0]) == nil {
addrs, err := net.LookupHost(args[0])
if err != nil || len(addrs) == 0 {
log.Fatalf("invalid host or ip provided: %s", err)
}
ip = addrs[0]
}
m, ch := mtr.NewMTR(args[0], srcAddr, TIMEOUT, INTERVAL, HOP_SLEEP, MAX_HOPS, MAX_UNKNOWN_HOPS, RING_BUFFER_SIZE)
m, ch := mtr.NewMTR(ip, srcAddr, TIMEOUT, INTERVAL, HOP_SLEEP, MAX_HOPS, MAX_UNKNOWN_HOPS, RING_BUFFER_SIZE)
if jsonFmt {
go func(ch chan struct{}) {
for {
Expand Down

0 comments on commit 57a3d9f

Please sign in to comment.