aboutsummaryrefslogtreecommitdiff
path: root/cmd/mybittorrent/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/mybittorrent/main.go')
-rw-r--r--cmd/mybittorrent/main.go51
1 files changed, 20 insertions, 31 deletions
diff --git a/cmd/mybittorrent/main.go b/cmd/mybittorrent/main.go
index e3e902a..7905e01 100644
--- a/cmd/mybittorrent/main.go
+++ b/cmd/mybittorrent/main.go
@@ -4,42 +4,14 @@ import (
"encoding/json"
"fmt"
"os"
- "strconv"
- "strings"
- "unicode"
// bencode "github.com/jackpal/bencode-go" // Available if you need it!
)
-func decodeBencode(bencodedString string) (interface{}, error) {
- if unicode.IsDigit(rune(bencodedString[0])) {
- var firstColonIndex int
-
- for i := 0; i < len(bencodedString); i++ {
- if bencodedString[i] == ':' {
- firstColonIndex = i
- break
- }
- }
-
- lengthStr := bencodedString[:firstColonIndex]
-
- length, err := strconv.Atoi(lengthStr)
- if err != nil {
- return "", err
- }
-
- return bencodedString[firstColonIndex+1 : firstColonIndex+1+length], nil
- } else if rune(bencodedString[0]) == 'i' {
- return strconv.Atoi(bencodedString[1:strings.IndexByte(bencodedString, 'e')])
- } else {
- return "", fmt.Errorf("only strings are supported at the moment")
- }
-}
-
func main() {
command := os.Args[1]
- if command == "decode" {
+ switch command {
+ case "decode":
bencodedValue := os.Args[2]
decoded, err := NewDecoder(bencodedValue).Decode()
@@ -50,7 +22,24 @@ func main() {
jsonOutput, _ := json.Marshal(decoded)
fmt.Println(string(jsonOutput))
- } else {
+
+ 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)
}