From 1e9d382af925714420b361c875c70ed2024d0d1f Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Mon, 4 Jan 2021 13:37:44 +1100
Subject: [PATCH] Reinstate cmd/announce announce subcommand

---
 cmd/torrent/announce.go | 14 ++++++--------
 cmd/torrent/main.go     | 21 ++++-----------------
 metainfo/hash.go        | 10 +++++++++-
 3 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/cmd/torrent/announce.go b/cmd/torrent/announce.go
index 5bb72fd1..990688a2 100644
--- a/cmd/torrent/announce.go
+++ b/cmd/torrent/announce.go
@@ -3,20 +3,18 @@ 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))
+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 1cc79923..59b2ddf7 100644
--- a/cmd/torrent/main.go
+++ b/cmd/torrent/main.go
@@ -159,25 +159,10 @@ var flags struct {
 	*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 41ee9566..b2bab751 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 @@ const HashSize = 20
 // 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
@@ -50,6 +54,10 @@ func (h *Hash) FromHexString(s string) (err error) {
 	return
 }
 
+func (h *Hash) UnmarshalText(b []byte) error {
+	return h.FromHexString(string(b))
+}
+
 func NewHashFromHex(s string) (h Hash) {
 	err := h.FromHexString(s)
 	if err != nil {
-- 
2.51.0