From 533fec840a5199dd83931d10a0fd5a824c3c7247 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 12 May 2020 11:31:56 +1000 Subject: [PATCH] cmd/tracker-announce: Rework for better utility in shell scripts --- cmd/tracker-announce/main.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/tracker-announce/main.go b/cmd/tracker-announce/main.go index bfe12143..6f63b295 100644 --- a/cmd/tracker-announce/main.go +++ b/cmd/tracker-announce/main.go @@ -3,8 +3,10 @@ package main import ( "log" "net/url" + "os" "strings" "sync" + "sync/atomic" "github.com/davecgh/go-spew/spew" @@ -36,39 +38,47 @@ func main() { Port: 50007, } tagflag.Parse(&flags) - ar := tracker.AnnounceRequest{ - NumWant: -1, - Left: -1, - Port: flags.Port, - } + var exitCode int32 var wg sync.WaitGroup for _, arg := range flags.Torrents { ts, err := argSpec(arg) if err != nil { log.Fatal(err) } - ar.InfoHash = ts.InfoHash for _, tier := range ts.Trackers { for _, tURI := range tier { + ar := tracker.AnnounceRequest{ + NumWant: -1, + Left: -1, + Port: flags.Port, + InfoHash: ts.InfoHash, + } wg.Add(1) - go doTracker(tURI, wg.Done, ar) + go func(tURI string) { + defer wg.Done() + if doTracker(tURI, ar) { + atomic.StoreInt32(&exitCode, 1) + } + }(tURI) } } } wg.Wait() + os.Exit(int(exitCode)) } -func doTracker(tURI string, done func(), ar tracker.AnnounceRequest) { - defer done() +func doTracker(tURI string, ar tracker.AnnounceRequest) (hadError bool) { for _, res := range announces(tURI, ar) { err := res.error resp := res.AnnounceResponse if err != nil { + hadError = true log.Printf("error announcing to %q: %s", tURI, err) continue } - log.Printf("tracker response from %q: %s", tURI, spew.Sdump(resp)) + spew.Dump(resp) } + return } type announceResult struct {