]> Sergey Matveev's repositories - btrtrc.git/blob - cmd/torrent/announce.go
Add --port flag to announce
[btrtrc.git] / cmd / torrent / announce.go
1 package main
2
3 import (
4         "fmt"
5
6         "github.com/davecgh/go-spew/spew"
7
8         "github.com/anacrolix/torrent"
9         "github.com/anacrolix/torrent/tracker"
10         "github.com/anacrolix/torrent/tracker/udp"
11 )
12
13 type AnnounceCmd struct {
14         Event    udp.AnnounceEvent
15         Port     *uint16
16         Tracker  string           `arg:"positional"`
17         InfoHash torrent.InfoHash `arg:"positional"`
18 }
19
20 func announceErr(flags AnnounceCmd) error {
21         req := tracker.AnnounceRequest{
22                 InfoHash: flags.InfoHash,
23                 Port:     uint16(torrent.NewDefaultClientConfig().ListenPort),
24                 NumWant:  -1,
25                 Event:    flags.Event,
26         }
27         if flags.Port != nil {
28                 req.Port = *flags.Port
29         }
30         response, err := tracker.Announce{
31                 TrackerUrl: flags.Tracker,
32                 Request:    req,
33         }.Do()
34         if err != nil {
35                 return fmt.Errorf("doing announce: %w", err)
36         }
37         spew.Dump(response)
38         return nil
39 }