From d7e9724c5da3c7582cc570059b47d760a9d4702c Mon Sep 17 00:00:00 2001 From: James Tucker Date: Tue, 25 Feb 2025 15:56:45 -0800 Subject: [PATCH] cmd/stunc: improve CLI interface for diagnostics Write to stdout by default, not stderr. Add quiet flag to only output on error. Add a host:port prefix to output. Updates tailscale/corp#26778 Signed-off-by: James Tucker --- cmd/stunc/stunc.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/stunc/stunc.go b/cmd/stunc/stunc.go index c4b2eedd3..182c621ab 100644 --- a/cmd/stunc/stunc.go +++ b/cmd/stunc/stunc.go @@ -17,11 +17,13 @@ import ( func main() { log.SetFlags(0) + log.SetOutput(os.Stdout) // CLI tool, write to stdout. var host string port := "3478" var readTimeout time.Duration flag.DurationVar(&readTimeout, "timeout", 3*time.Second, "response wait timeout") + quiet := flag.Bool("quiet", false, "only print errors") flag.Parse() @@ -44,8 +46,9 @@ func main() { if err != nil { log.Fatalf("invalid port: %v", err) } - - uaddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, port)) + hostPort := net.JoinHostPort(host, port) + log.SetPrefix(hostPort + ": ") + uaddr, err := net.ResolveUDPAddr("udp", hostPort) if err != nil { log.Fatal(err) } @@ -80,7 +83,9 @@ func main() { log.Fatalf("txid mismatch: got %v, want %v", tid, txID) } - log.Printf("sent addr: %v", uaddr) - log.Printf("from addr: %v", raddr) - log.Printf("stun addr: %v", saddr) + if !*quiet { + log.Printf("sent addr: %v", uaddr) + log.Printf("from addr: %v", raddr) + log.Printf("stun addr: %v", saddr) + } }