aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjet2tlf <jet2tlf@gmail.com>2024-06-03 17:17:15 +0000
committerjet2tlf <jet2tlf@gmail.com>2024-06-03 17:17:15 +0000
commit3a6e39f8a82299f21b1efa8aaa9a1ea748d25628 (patch)
treebccb6ea9ad9bbcda4617f3e0c4dd33b99c605d22
parentafe50dd881ca58c6331cc84b151df9bafd9e82e2 (diff)
downloadbittorrent-go-3a6e39f8a82299f21b1efa8aaa9a1ea748d25628.tar.gz
bittorrent-go-3a6e39f8a82299f21b1efa8aaa9a1ea748d25628.zip
codecrafters submit [skip ci]
-rw-r--r--cmd/mybittorrent/main.go33
1 files changed, 12 insertions, 21 deletions
diff --git a/cmd/mybittorrent/main.go b/cmd/mybittorrent/main.go
index ba70193..5c2a12f 100644
--- a/cmd/mybittorrent/main.go
+++ b/cmd/mybittorrent/main.go
@@ -1,8 +1,7 @@
package main
import (
- // Uncomment this line to pass the first stage
- // "encoding/json"
+ "encoding/json"
"fmt"
"os"
"strconv"
@@ -10,9 +9,6 @@ import (
// bencode "github.com/jackpal/bencode-go" // Available if you need it!
)
-// Example:
-// - 5:hello -> hello
-// - 10:hello12345 -> hello12345
func decodeBencode(bencodedString string) (interface{}, error) {
if unicode.IsDigit(rune(bencodedString[0])) {
var firstColonIndex int
@@ -33,29 +29,24 @@ func decodeBencode(bencodedString string) (interface{}, error) {
return bencodedString[firstColonIndex+1 : firstColonIndex+1+length], nil
} else {
- return "", fmt.Errorf("Only strings are supported at the moment")
+ return "", fmt.Errorf("only strings are supported at the moment")
}
}
func main() {
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- fmt.Println("Logs from your program will appear here!")
-
command := os.Args[1]
if command == "decode" {
- // Uncomment this block to pass the first stage
- //
- // bencodedValue := os.Args[2]
- //
- // decoded, err := decodeBencode(bencodedValue)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- //
- // jsonOutput, _ := json.Marshal(decoded)
- // fmt.Println(string(jsonOutput))
+ bencodedValue := os.Args[2]
+
+ decoded, err := decodeBencode(bencodedValue)
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+
+ jsonOutput, _ := json.Marshal(decoded)
+ fmt.Println(string(jsonOutput))
} else {
fmt.Println("Unknown command: " + command)
os.Exit(1)