Add announce and download commands to cmd/torrent

This commit is contained in:
Matt Joiner 2020-04-10 15:27:30 +10:00
parent 6920298770
commit d3b6bcef97
2 changed files with 50 additions and 1 deletions

32
cmd/torrent/announce.go Normal file
View File

@ -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
}

View 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