]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent: Add spew-bencoding command
authorMatt Joiner <anacrolix@gmail.com>
Wed, 9 Dec 2020 09:01:53 +0000 (20:01 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 9 Dec 2020 09:01:53 +0000 (20:01 +1100)
cmd/torrent/main.go

index d860ca4fddbc7d51b35c84ab05fbd3272cfedec8..1cc7992398657dc18f083015d882fee5e249a5f9 100644 (file)
@@ -4,6 +4,7 @@ package main
 import (
        "expvar"
        "fmt"
+       "io"
        stdLog "log"
        "net"
        "net/http"
@@ -15,6 +16,8 @@ import (
 
        "github.com/alexflint/go-arg"
        "github.com/anacrolix/missinggo"
+       "github.com/anacrolix/torrent/bencode"
+       "github.com/davecgh/go-spew/spew"
        "github.com/dustin/go-humanize"
        "golang.org/x/xerrors"
 
@@ -153,8 +156,12 @@ var flags struct {
        Debug bool
        Stats *bool
 
-       *DownloadCmd  `arg:"subcommand:download"`
-       *ListFilesCmd `arg:"subcommand:list-files"`
+       *DownloadCmd      `arg:"subcommand:download"`
+       *ListFilesCmd     `arg:"subcommand:list-files"`
+       *SpewBencodingCmd `arg:"subcommand:spew-bencoding"`
+}
+
+type SpewBencodingCmd struct {
 }
 
 //DownloadCmd: &DownloadCmd{
@@ -253,6 +260,20 @@ func mainErr() error {
                        fmt.Println(f.DisplayPath(&info))
                }
                return nil
+       case flags.SpewBencodingCmd != nil:
+               d := bencode.NewDecoder(os.Stdin)
+               for i := 0; ; i++ {
+                       var v interface{}
+                       err := d.Decode(&v)
+                       if err == io.EOF {
+                               break
+                       }
+                       if err != nil {
+                               return fmt.Errorf("decoding message index %d: %w", i, err)
+                       }
+                       spew.Dump(v)
+               }
+               return nil
        default:
                p.Fail(fmt.Sprintf("unexpected subcommand: %v", p.Subcommand()))
                panic("unreachable")