cmd/torrent/announce.go | 14 ++++++-------- cmd/torrent/main.go | 21 ++++----------------- metainfo/hash.go | 10 +++++++++- diff --git a/cmd/torrent/announce.go b/cmd/torrent/announce.go index 5bb72fd12c327ea6f4a00de0fdbf963f9bba7f13..990688a2621a5cf6ee149c85b8a7e122ab5b8076 100644 --- a/cmd/torrent/announce.go +++ b/cmd/torrent/announce.go @@ -3,20 +3,18 @@ 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)) +type AnnounceCmd struct { + Tracker string `arg:"positional"` + InfoHash torrent.InfoHash +} + +func announceErr() error { response, err := tracker.Announce{ TrackerUrl: flags.Tracker, Request: tracker.AnnounceRequest{ diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 1cc7992398657dc18f083015d882fee5e249a5f9..59b2ddf7228ffdec1b974d790d2f08913b709cc4 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -159,25 +159,10 @@ *DownloadCmd `arg:"subcommand:download"` *ListFilesCmd `arg:"subcommand:list-files"` *SpewBencodingCmd `arg:"subcommand:spew-bencoding"` + *AnnounceCmd `arg:"subcommand:announce"` } -type SpewBencodingCmd struct { -} - -//DownloadCmd: &DownloadCmd{ -// UploadRate: -1, -// DownloadRate: -1, -// Progress: true, -// Dht: true, -// -// TcpPeers: true, -// UtpPeers: true, -// Webtorrent: true, -// -// Ipv4: true, -// Ipv6: true, -// Pex: true, -//}, +type SpewBencodingCmd struct{} type DownloadCmd struct { Mmap bool `help:"memory-map torrent data"` @@ -243,6 +228,8 @@ func mainErr() error { stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile) p := arg.MustParse(&flags) switch { + case flags.AnnounceCmd != nil: + return announceErr() //case : // return announceErr(flags.Args, parser) case flags.DownloadCmd != nil: diff --git a/metainfo/hash.go b/metainfo/hash.go index 41ee956613de131b5f6c3280f79992c7174e7ae8..b2bab751b6564927590562c3ab28a2fb3f5e37f7 100644 --- a/metainfo/hash.go +++ b/metainfo/hash.go @@ -2,6 +2,7 @@ package metainfo import ( "crypto/sha1" + "encoding" "encoding/hex" "fmt" ) @@ -11,7 +12,10 @@ // 20-byte SHA1 hash used for info and pieces. type Hash [HashSize]byte -var _ fmt.Formatter = (*Hash)(nil) +var ( + _ fmt.Formatter = (*Hash)(nil) + _ encoding.TextUnmarshaler = (*Hash)(nil) +) func (h Hash) Format(f fmt.State, c rune) { // TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless @@ -48,6 +52,10 @@ if n != HashSize { panic(n) } return +} + +func (h *Hash) UnmarshalText(b []byte) error { + return h.FromHexString(string(b)) } func NewHashFromHex(s string) (h Hash) {