go-bt/example_loader.go

66 lines
1.2 KiB
Go
Raw Normal View History

2014-08-14 18:48:33 +00:00
package main
import (
"flag"
"fmt"
2014-08-14 19:19:42 +00:00
"github.com/vbatts/go-bt/bencode"
"github.com/vbatts/go-bt/torrent"
"io/ioutil"
2014-08-14 18:48:33 +00:00
"os"
)
var (
flOutput = flag.String("o", "", "output the re-encoded torrent to file at this path")
)
func main() {
flag.Parse()
if len(*flOutput) > 0 && flag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "-o and multiple input files can not be used together")
os.Exit(1)
}
for _, arg := range flag.Args() {
fh, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
2014-08-14 19:55:39 +00:00
buf, err := ioutil.ReadAll(fh)
2014-08-14 18:48:33 +00:00
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
fh.Close()
2014-08-14 19:55:39 +00:00
tf := torrent.File{}
err = bencode.Unmarshal(buf, &tf)
2014-08-14 18:48:33 +00:00
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
2014-08-14 18:48:33 +00:00
fmt.Printf("Loaded: %s (%d files)\n", tf.Info.Name, len(tf.Info.Files))
if len(*flOutput) > 0 {
fhOutput, err := os.Create(*flOutput)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
buf, err = bencode.Marshal(tf)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
2014-08-14 19:55:39 +00:00
_, err = fhOutput.Write(buf)
2014-08-14 18:48:33 +00:00
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
fmt.Printf("wrote: %s\n", fhOutput.Name())
fhOutput.Close()
}
}
}