-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (68 loc) · 2.01 KB
/
main.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/Harry-kp/nebula/logger"
"github.com/Harry-kp/nebula/torrentfile"
"github.com/Harry-kp/nebula/utils"
)
// resolveFilePath resolves the absolute path and checks if the file exists
func resolveFilePath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
return absPath, nil
}
func main() {
// Define flags for input and output file paths
inputFile := flag.String("input", "", "Path to the input torrent file (required)")
outputFile := flag.String("output", ".", "Path to the output file or directory (default: current directory)")
logEnabled := flag.Bool("log", false, "Enable logging")
// Parse the flags
flag.Parse()
// Validate required flags
if *inputFile == "" {
fmt.Println("Torrent file path is required")
flag.Usage()
os.Exit(1)
}
// Initialize the global logger
config := logger.Config{
LogEnabled: *logEnabled,
}
logger.Init(config)
// Display banner
utils.Banner()
// Resolve input file path
inPath, err := resolveFilePath(*inputFile)
if err != nil {
logger.Fatal(fmt.Sprintf("Error resolving input file path: %v", err))
}
// Resolve output file path
outPath, err := resolveFilePath(*outputFile)
if err != nil {
logger.Fatal(fmt.Sprintf("Error resolving output file path: %v", err))
}
// Open the torrent file
tf, err := torrentfile.Open(inPath)
if err != nil {
logger.Fatal(fmt.Sprintf("Error opening torrent file: %v", err))
}
// If output path is a directory, append the torrent file name
if stat, err := os.Stat(outPath); err == nil && stat.IsDir() {
outPath = filepath.Join(outPath, tf.Name)
}
// Validate the output path
if _, err := os.Stat(outPath); err == nil {
logger.Fatal("Error: output file already exists")
}
// Download the torrent file to the specified output path
err = tf.DownloadToFile(outPath)
if err != nil {
logger.Fatal(fmt.Sprintf("Error downloading torrent file: %v", err))
}
fmt.Println("Download completed successfully")
}