add exip (mini) cli

This commit is contained in:
decauwsemaecker.glen@gmail.com 2017-04-06 18:46:53 -05:00
parent 71ad8c93d9
commit 689360936b
2 changed files with 56 additions and 1 deletions

View File

@ -5,4 +5,3 @@ TODO:
+ Decent Logging; + Decent Logging;
+ README Documentation; + README Documentation;
+ Unit-Tests; + Unit-Tests;
+ Add a small cmd cli (named `exip`), using standard go code only;

56
cmd/exip/main.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/glendc/go-external-ip"
)
// CLI Flags
var (
timeout = flag.Duration("t", time.Second*2, "consensus's voting timeout")
verbose = flag.Bool("v", false, "verbose logging")
)
func main() {
// configure the consensus
cfg := externalip.DefaultConsensusConfig()
if timeout != nil {
cfg.WithTimeout(*timeout)
}
// TODO: Add Logging (and use the verbose flag)
// create the consensus
consensus := externalip.DefaultConsensus(
externalip.DefaultConsensusConfig().WithTimeout(*timeout))
// retrieve the external ip
ip, err := consensus.ExternalIP()
// simple error handling
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// success, simply output the IP in string format
fmt.Println(ip.String())
}
func init() {
// Define customized usage output
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Retrieve your external IP.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n %s [flags]\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Flags:")
fmt.Fprintf(os.Stderr, " -h help\n \tshow this usage message\n")
flag.PrintDefaults()
}
// Parse CLI Flags
flag.Parse()
}