Remove xerrors dependency (#600)

* Remove xerrors dependency

* Update main.go
This commit is contained in:
YenForYang 2021-09-12 20:38:08 -05:00 committed by GitHub
parent 3b4d4a64ff
commit 1bb6724020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 17 deletions

View File

@ -2,6 +2,7 @@
package main package main
import ( import (
"errors"
"expvar" "expvar"
"fmt" "fmt"
"io" "io"
@ -22,7 +23,6 @@ import (
"github.com/anacrolix/torrent/version" "github.com/anacrolix/torrent/version"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"golang.org/x/xerrors"
"github.com/anacrolix/log" "github.com/anacrolix/log"
@ -100,23 +100,23 @@ func addTorrents(client *torrent.Client) error {
if strings.HasPrefix(arg, "magnet:") { if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg) t, err := client.AddMagnet(arg)
if err != nil { if err != nil {
return nil, xerrors.Errorf("error adding magnet: %w", err) return nil, fmt.Errorf("error adding magnet: %w", err)
} }
return t, nil return t, nil
} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { } else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
response, err := http.Get(arg) response, err := http.Get(arg)
if err != nil { if err != nil {
return nil, xerrors.Errorf("Error downloading torrent file: %s", err) return nil, fmt.Errorf("Error downloading torrent file: %s", err)
} }
metaInfo, err := metainfo.Load(response.Body) metaInfo, err := metainfo.Load(response.Body)
defer response.Body.Close() defer response.Body.Close()
if err != nil { if err != nil {
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err) return nil, fmt.Errorf("error loading torrent file %q: %s\n", arg, err)
} }
t, err := client.AddTorrent(metaInfo) t, err := client.AddTorrent(metaInfo)
if err != nil { if err != nil {
return nil, xerrors.Errorf("adding torrent: %w", err) return nil, fmt.Errorf("adding torrent: %w", err)
} }
return t, nil return t, nil
} else if strings.HasPrefix(arg, "infohash:") { } else if strings.HasPrefix(arg, "infohash:") {
@ -125,17 +125,17 @@ func addTorrents(client *torrent.Client) error {
} else { } else {
metaInfo, err := metainfo.LoadFromFile(arg) metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil { if err != nil {
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err) return nil, fmt.Errorf("error loading torrent file %q: %s\n", arg, err)
} }
t, err := client.AddTorrent(metaInfo) t, err := client.AddTorrent(metaInfo)
if err != nil { if err != nil {
return nil, xerrors.Errorf("adding torrent: %w", err) return nil, fmt.Errorf("adding torrent: %w", err)
} }
return t, nil return t, nil
} }
}() }()
if err != nil { if err != nil {
return xerrors.Errorf("adding torrent for %q: %w", arg, err) return fmt.Errorf("adding torrent for %q: %w", arg, err)
} }
if flags.Progress { if flags.Progress {
torrentBar(t, flags.PieceStates) torrentBar(t, flags.PieceStates)
@ -302,7 +302,7 @@ func downloadErr() error {
if flags.PackedBlocklist != "" { if flags.PackedBlocklist != "" {
blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist) blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
if err != nil { if err != nil {
return xerrors.Errorf("loading blocklist: %v", err) return fmt.Errorf("loading blocklist: %v", err)
} }
defer blocklist.Close() defer blocklist.Close()
clientConfig.IPBlocklist = blocklist clientConfig.IPBlocklist = blocklist
@ -331,7 +331,7 @@ func downloadErr() error {
client, err := torrent.NewClient(clientConfig) client, err := torrent.NewClient(clientConfig)
if err != nil { if err != nil {
return xerrors.Errorf("creating client: %v", err) return fmt.Errorf("creating client: %v", err)
} }
var clientClose sync.Once //In certain situations, close was being called more than once. var clientClose sync.Once //In certain situations, close was being called more than once.
defer clientClose.Do(client.Close) defer clientClose.Do(client.Close)
@ -354,7 +354,7 @@ func downloadErr() error {
if client.WaitAll() { if client.WaitAll() {
log.Print("downloaded ALL the torrents") log.Print("downloaded ALL the torrents")
} else { } else {
return xerrors.New("y u no complete torrents?!") return errors.New("y u no complete torrents?!")
} }
if flags.Seed { if flags.Seed {
if len(client.Torrents()) == 0 { if len(client.Torrents()) == 0 {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"log" "log"
"net" "net"
@ -8,8 +9,6 @@ import (
"sync" "sync"
"github.com/alexflint/go-arg" "github.com/alexflint/go-arg"
"golang.org/x/xerrors"
"github.com/anacrolix/torrent/mse" "github.com/anacrolix/torrent/mse"
) )
@ -41,25 +40,25 @@ func mainErr() error {
if args.Dial != nil { if args.Dial != nil {
cn, err := net.Dial(args.Dial.Network, args.Dial.Address) cn, err := net.Dial(args.Dial.Network, args.Dial.Address)
if err != nil { if err != nil {
return xerrors.Errorf("dialing: %w", err) return fmt.Errorf("dialing: %w", err)
} }
defer cn.Close() defer cn.Close()
rw, _, err := mse.InitiateHandshake(cn, []byte(args.Dial.SecretKey), args.Dial.InitialPayload, args.CryptoMethod) rw, _, err := mse.InitiateHandshake(cn, []byte(args.Dial.SecretKey), args.Dial.InitialPayload, args.CryptoMethod)
if err != nil { if err != nil {
return xerrors.Errorf("initiating handshake: %w", err) return fmt.Errorf("initiating handshake: %w", err)
} }
doStreaming(rw) doStreaming(rw)
} }
if args.Listen != nil { if args.Listen != nil {
l, err := net.Listen(args.Listen.Network, args.Listen.Address) l, err := net.Listen(args.Listen.Network, args.Listen.Address)
if err != nil { if err != nil {
return xerrors.Errorf("listening: %w", err) return fmt.Errorf("listening: %w", err)
} }
defer l.Close() defer l.Close()
cn, err := l.Accept() cn, err := l.Accept()
l.Close() l.Close()
if err != nil { if err != nil {
return xerrors.Errorf("accepting: %w", err) return fmt.Errorf("accepting: %w", err)
} }
defer cn.Close() defer cn.Close()
rw, _, err := mse.ReceiveHandshake(cn, func(f func([]byte) bool) { rw, _, err := mse.ReceiveHandshake(cn, func(f func([]byte) bool) {