]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add announce and download commands to cmd/torrent
authorMatt Joiner <anacrolix@gmail.com>
Fri, 10 Apr 2020 05:27:30 +0000 (15:27 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 10 Apr 2020 05:27:30 +0000 (15:27 +1000)
cmd/torrent/announce.go [new file with mode: 0644]
cmd/torrent/main.go

diff --git a/cmd/torrent/announce.go b/cmd/torrent/announce.go
new file mode 100644 (file)
index 0000000..5bb72fd
--- /dev/null
@@ -0,0 +1,32 @@
+package main
+
+import (
+       "fmt"
+
+       "github.com/anacrolix/tagflag"
+       "github.com/davecgh/go-spew/spew"
+
+       "github.com/anacrolix/torrent"
+       "github.com/anacrolix/torrent/tracker"
+)
+
+func announceErr(args []string, parent *tagflag.Parser) error {
+       var flags struct {
+               tagflag.StartPos
+               Tracker  string
+               InfoHash torrent.InfoHash
+       }
+       tagflag.ParseArgs(&flags, args, tagflag.Parent(parent))
+       response, err := tracker.Announce{
+               TrackerUrl: flags.Tracker,
+               Request: tracker.AnnounceRequest{
+                       InfoHash: flags.InfoHash,
+                       Port:     uint16(torrent.NewDefaultClientConfig().ListenPort),
+               },
+       }.Do()
+       if err != nil {
+               return fmt.Errorf("doing announce: %w", err)
+       }
+       spew.Dump(response)
+       return nil
+}
index 1270540571e4334c0878379ee95dc93c1f222f2e..5e423d02f0578d45e256ebd1174847cf5220efa9 100644 (file)
@@ -178,7 +178,24 @@ func main() {
 }
 
 func mainErr() error {
-       tagflag.Parse(&flags)
+       var flags struct {
+               tagflag.StartPos
+               Command string
+               Args    tagflag.ExcessArgs
+       }
+       parser := tagflag.Parse(&flags, tagflag.ParseIntermixed(false))
+       switch flags.Command {
+       case "announce":
+               return announceErr(flags.Args, parser)
+       case "download":
+               return downloadErr(flags.Args, parser)
+       default:
+               return fmt.Errorf("unknown command %q", flags.Command)
+       }
+}
+
+func downloadErr(args []string, parent *tagflag.Parser) error {
+       tagflag.ParseArgs(&flags, args, tagflag.Parent(parent))
        defer envpprof.Stop()
        clientConfig := torrent.NewDefaultClientConfig()
        clientConfig.DisableAcceptRateLimiting = true