cmd/torrent/main.go | 55 ++++++++++++++++++++++++++--------------------------- config.go | 4 ++-- diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 1b06efb64b8d57594ff44e3b47d3ad4d88a04844..0bbd0ec44e8a455ff1b2eac536eb637fb82672b0 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -12,8 +12,8 @@ "strings" "time" _ "github.com/anacrolix/envpprof" + "github.com/anacrolix/tagflag" "github.com/dustin/go-humanize" - "github.com/jessevdk/go-flags" "github.com/anacrolix/torrent" "github.com/anacrolix/torrent/data/mmap" @@ -70,27 +70,16 @@ } func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) - var rootGroup struct { - Client torrent.Config `group:"Client Options"` - TestPeers []string `long:"test-peer" description:"address of peer to inject to every torrent"` - MMap bool `long:"mmap" description:"memory-map the torrent files"` - } - // Don't pass flags.PrintError because it's inconsistent with printing. - // https://github.com/jessevdk/go-flags/issues/132 - parser := flags.NewParser(&rootGroup, flags.HelpFlag|flags.PassDoubleDash) - parser.Usage = "[OPTIONS] (magnet URI or .torrent file path)..." - posArgs, err := parser.Parse() - if err != nil { - fmt.Fprintln(os.Stderr, "Download from the BitTorrent network.") - fmt.Println(err) - os.Exit(2) + var opts struct { + torrent.Config `name:"Client"` + Mmap bool `help:"memory-map torrent data"` + TestPeer []*net.TCPAddr `short:"p" help:"addresses of some starting peers"` + Torrent []string `type:"pos" arity:"+" help:"torrent file path or magnet uri"` } - testPeers, err := resolvedPeerAddrs(rootGroup.TestPeers) - if err != nil { - log.Fatal(err) - } - if rootGroup.MMap { - rootGroup.Client.TorrentDataOpener = func(info *metainfo.Info) torrent.Data { + tagflag.Parse(&opts, tagflag.SkipBadTypes()) + clientConfig := opts.Config + if opts.Mmap { + clientConfig.TorrentDataOpener = func(info *metainfo.Info) torrent.Data { ret, err := mmap.TorrentData(info, "") if err != nil { log.Fatalf("error opening torrent data for %q: %s", info.Name, err) @@ -99,11 +88,12 @@ return ret } } - if len(posArgs) == 0 { - fmt.Fprintln(os.Stderr, "no torrents specified") + torrents := opts.Torrent + if len(torrents) == 0 { + fmt.Fprintf(os.Stderr, "no torrents specified\n") return } - client, err := torrent.NewClient(&rootGroup.Client) + client, err := torrent.NewClient(&clientConfig) if err != nil { log.Fatalf("error creating client: %s", err) } @@ -111,7 +101,7 @@ http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { client.WriteStatus(w) }) defer client.Close() - for _, arg := range posArgs { + for _, arg := range torrents { t := func() torrent.Torrent { if strings.HasPrefix(arg, "magnet:") { t, err := client.AddMagnet(arg) @@ -122,7 +112,8 @@ return t } else { metaInfo, err := metainfo.LoadFromFile(arg) if err != nil { - log.Fatal(err) + fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err) + os.Exit(1) } t, err := client.AddTorrent(metaInfo) if err != nil { @@ -131,7 +122,15 @@ } return t } }() - err := t.AddPeers(testPeers) + err := t.AddPeers(func() (ret []torrent.Peer) { + for _, ta := range opts.TestPeer { + ret = append(ret, torrent.Peer{ + IP: ta.IP, + Port: ta.Port, + }) + } + return + }()) if err != nil { log.Fatal(err) } @@ -159,7 +158,7 @@ case <-ticker.C: os.Stdout.WriteString(progressLine(client)) } } - if rootGroup.Client.Seed { + if opts.Seed { select {} } } diff --git a/config.go b/config.go index d6b79424407b6b43ff5e27e4d3ba9ee4f6e4ca2a..be0222d183256fd6f421babd7c32aaf5064035cb 100644 --- a/config.go +++ b/config.go @@ -46,7 +46,7 @@ TorrentDataOpener DisableEncryption bool `long:"disable-encryption"` IPBlocklist *iplist.IPList - DisableIPv6 bool + DisableIPv6 bool `long:"disable-ipv6"` // Perform logging and any other behaviour that will help debug. - Debug bool + Debug bool `help:"enable debug logging"` }