From 4ffcd9f1ce4051d7687ae54d892f25b29693947c Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 27 Aug 2017 12:25:51 +1000 Subject: [PATCH] cmd/torrentfs: Switch to tagflag and rename -torrentPath->-metainfoDir --- README.md | 4 +-- cmd/torrentfs/main.go | 76 ++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 25ff7b18..2f8636d9 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,10 @@ Downloads torrents from the command-line. This first example does not use `godo` ### torrentfs -torrentfs mounts a FUSE filesystem at `-mountDir`. The contents are the torrents described by the torrent files and magnet links at `-torrentPath`. Data for read requests is fetched only as required from the torrent network, and stored at `-downloadDir`. +torrentfs mounts a FUSE filesystem at `-mountDir`. The contents are the torrents described by the torrent files and magnet links at `-metainfoDir`. Data for read requests is fetched only as required from the torrent network, and stored at `-downloadDir`. $ mkdir mnt torrents - $ godo github.com/anacrolix/torrent/cmd/torrentfs -mountDir mnt -torrentPath torrents & + $ godo github.com/anacrolix/torrent/cmd/torrentfs -mountDir=mnt -metainfoDir=torrents & $ cd torrents $ wget http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.iso.torrent $ cd .. diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index f9ee139e..1d12d6a7 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -2,7 +2,6 @@ package main import ( - "flag" "log" "net" "net/http" @@ -18,6 +17,7 @@ import ( fusefs "bazil.org/fuse/fs" "github.com/anacrolix/dht" _ "github.com/anacrolix/envpprof" + "github.com/anacrolix/tagflag" "github.com/anacrolix/torrent" "github.com/anacrolix/torrent/fs" @@ -25,42 +25,35 @@ import ( ) var ( - torrentPath = flag.String("torrentPath", func() string { - _user, err := user.Current() - if err != nil { - log.Fatal(err) - } - return filepath.Join(_user.HomeDir, ".config/transmission/torrents") - }(), "torrent files in this location describe the contents of the mounted filesystem") - downloadDir = flag.String("downloadDir", "", "location to save torrent data") - mountDir = flag.String("mountDir", "", "location the torrent contents are made available") + args = struct { + MetainfoDir string `help:"torrent files in this location describe the contents of the mounted filesystem"` + DownloadDir string `help:"location to save torrent data"` + MountDir string `help:"location the torrent contents are made available"` - disableTrackers = flag.Bool("disableTrackers", false, "disables trackers") - testPeer = flag.String("testPeer", "", "the address for a test peer") - readaheadBytes = flag.Int64("readaheadBytes", 10*1024*1024, "bytes to readahead in each torrent from the last read piece") - listenAddr = flag.String("listenAddr", ":6882", "incoming connection address") - - testPeerAddr *net.TCPAddr + DisableTrackers bool + TestPeer *net.TCPAddr + ReadaheadBytes tagflag.Bytes + ListenAddr *net.TCPAddr + }{ + MetainfoDir: func() string { + _user, err := user.Current() + if err != nil { + log.Fatal(err) + } + return filepath.Join(_user.HomeDir, ".config/transmission/torrents") + }(), + ReadaheadBytes: 10 << 20, + ListenAddr: &net.TCPAddr{}, + } ) -func resolveTestPeerAddr() { - if *testPeer == "" { - return - } - var err error - testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer) - if err != nil { - log.Fatal(err) - } -} - func exitSignalHandlers(fs *torrentfs.TorrentFS) { c := make(chan os.Signal) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) for { <-c fs.Destroy() - err := fuse.Unmount(*mountDir) + err := fuse.Unmount(args.MountDir) if err != nil { log.Print(err) } @@ -70,8 +63,8 @@ func exitSignalHandlers(fs *torrentfs.TorrentFS) { func addTestPeer(client *torrent.Client) { for _, t := range client.Torrents() { t.AddPeers([]torrent.Peer{{ - IP: testPeerAddr.IP, - Port: testPeerAddr.Port, + IP: args.TestPeer.IP, + Port: args.TestPeer.Port, }}) } } @@ -81,27 +74,23 @@ func main() { } func mainExitCode() int { - flag.Parse() - if flag.NArg() != 0 { - os.Stderr.WriteString("one does not simply pass positional args\n") - return 2 - } - if *mountDir == "" { + tagflag.Parse(&args) + if args.MountDir == "" { os.Stderr.WriteString("y u no specify mountpoint?\n") return 2 } log.SetFlags(log.LstdFlags | log.Lshortfile) - conn, err := fuse.Mount(*mountDir) + conn, err := fuse.Mount(args.MountDir) if err != nil { log.Fatal(err) } - defer fuse.Unmount(*mountDir) + defer fuse.Unmount(args.MountDir) // TODO: Think about the ramifications of exiting not due to a signal. defer conn.Close() client, err := torrent.NewClient(&torrent.Config{ - DataDir: *downloadDir, - DisableTrackers: *disableTrackers, - ListenAddr: *listenAddr, + DataDir: args.DownloadDir, + DisableTrackers: args.DisableTrackers, + ListenAddr: args.ListenAddr.String(), NoUpload: true, // Ensure that downloads are responsive. DHTConfig: dht.ServerConfig{ StartingNodes: dht.GlobalBootstrapAddrs, @@ -115,7 +104,7 @@ func mainExitCode() int { http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { client.WriteStatus(w) }) - dw, err := dirwatch.New(*torrentPath) + dw, err := dirwatch.New(args.MetainfoDir) if err != nil { log.Printf("error watching torrent dir: %s", err) return 1 @@ -144,11 +133,10 @@ func mainExitCode() int { } } }() - resolveTestPeerAddr() fs := torrentfs.New(client) go exitSignalHandlers(fs) - if testPeerAddr != nil { + if args.TestPeer != nil { go func() { for { addTestPeer(client)