diff --git a/cmd/torrent/announce.go b/cmd/torrent/announce.go new file mode 100644 index 00000000..5bb72fd1 --- /dev/null +++ b/cmd/torrent/announce.go @@ -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 +} diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 12705405..5e423d02 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -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