aboutsummaryrefslogtreecommitdiff
path: root/cmd/mybittorrent/main.go
blob: 7905e01fd12b3f1f87477db42862ce6ea10ab2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
	"encoding/json"
	"fmt"
	"os"
	// bencode "github.com/jackpal/bencode-go" // Available if you need it!
)

func main() {
	command := os.Args[1]

	switch command {
	case "decode":
		bencodedValue := os.Args[2]

		decoded, err := NewDecoder(bencodedValue).Decode()
		if err != nil {
			fmt.Println(err)
			return
		}

		jsonOutput, _ := json.Marshal(decoded)
		fmt.Println(string(jsonOutput))

	case "info":
		f, err := os.Open(os.Args[2])
		if err != nil {
			fmt.Println("Failed to open file: " + os.Args[2])
		}

		tf := NewFile()

		err = tf.ReadFrom(f)
		if err != nil {
			fmt.Println("Failed to parse file: " + os.Args[2])
			panic(err)
		}

		fmt.Printf("Tracker URL: %s\nLength: %d", tf.Announce, tf.Info.Length)

	default:
		fmt.Println("Unknown command: " + command)
		os.Exit(1)
	}
}