From 689360936bdf61e99461383eb3f656ef2d83b146 Mon Sep 17 00:00:00 2001 From: "decauwsemaecker.glen@gmail.com" Date: Thu, 6 Apr 2017 18:46:53 -0500 Subject: [PATCH] add exip (mini) cli --- README.md | 1 - cmd/exip/main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 cmd/exip/main.go diff --git a/README.md b/README.md index a0233e5..62b5a4d 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,3 @@ TODO: + Decent Logging; + README Documentation; + Unit-Tests; -+ Add a small cmd cli (named `exip`), using standard go code only; diff --git a/cmd/exip/main.go b/cmd/exip/main.go new file mode 100644 index 0000000..be12acf --- /dev/null +++ b/cmd/exip/main.go @@ -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() +}