-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
161 lines (143 loc) · 3.35 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"os"
"github.com/mrobinsn/go-rtorrent/rtorrent"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var (
name = "rTorrent XMLRPC CLI"
version = "1.0.0"
app = initApp()
conn *rtorrent.RTorrent
endpoint string
view string
hash string
disableCertCheck bool
)
func initApp() *cli.App {
nApp := cli.NewApp()
nApp.Name = name
nApp.Version = version
nApp.Authors = []cli.Author{
{Name: "Michael Robinson", Email: "m@michaelrobinson.io"},
}
// Global flags
nApp.Flags = []cli.Flag{
cli.StringFlag{
Name: "endpoint",
Usage: "rTorrent endpoint",
Value: "http://myrtorrent/RPC2",
Destination: &endpoint,
},
cli.BoolFlag{
Name: "disable-cert-check",
Usage: "disable certificate checking on this endpoint, useful for testing",
Destination: &disableCertCheck,
},
}
nApp.Before = setupConnection
nApp.Commands = []cli.Command{{
Name: "get-ip",
Usage: "retrieves the IP for this rTorrent instance",
Action: getIP,
}, {
Name: "get-name",
Usage: "retrieves the name for this rTorrent instance",
Action: getName,
}, {
Name: "get-totals",
Usage: "retrieves the up/down totals for this rTorrent instance",
Action: getTotals,
}, {
Name: "get-torrents",
Usage: "retrieves the torrents from this rTorrent instance",
Action: getTorrents,
Flags: []cli.Flag{
cli.StringFlag{
Name: "view",
Usage: "view to use, known values: main, started, stopped, hashing, seeding",
Value: string(rtorrent.ViewMain),
Destination: &view,
},
},
}, {
Name: "get-files",
Usage: "retrieves the files for a specific torrent",
Action: getFiles,
Flags: []cli.Flag{
cli.StringFlag{
Name: "hash",
Usage: "hash of the torrent",
Value: "unknown",
Destination: &hash,
},
},
},
}
return nApp
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}
func setupConnection(c *cli.Context) error {
if endpoint == "" {
return errors.New("endpoint must be specified")
}
conn = rtorrent.New(endpoint, disableCertCheck)
return nil
}
func getIP(c *cli.Context) error {
ip, err := conn.IP()
if err != nil {
return errors.Wrap(err, "failed to get rTorrent IP")
}
fmt.Println(ip)
return nil
}
func getName(c *cli.Context) error {
name, err := conn.Name()
if err != nil {
return errors.Wrap(err, "failed to get rTorrent name")
}
fmt.Println(name)
return nil
}
func getTotals(c *cli.Context) error {
// Get Down Total
downTotal, err := conn.DownTotal()
if err != nil {
return errors.Wrap(err, "failed to get rTorrent down total")
}
fmt.Printf("%d\n", downTotal)
// Get Up Total
upTotal, err := conn.UpTotal()
if err != nil {
return errors.Wrap(err, "failed to get rTorrent up total")
}
fmt.Printf("%d\n", upTotal)
return nil
}
func getTorrents(c *cli.Context) error {
torrents, err := conn.GetTorrents(rtorrent.View(view))
if err != nil {
return errors.Wrap(err, "failed to get torrents")
}
for _, torrent := range torrents {
fmt.Println(torrent.Pretty())
}
return nil
}
func getFiles(c *cli.Context) error {
files, err := conn.GetFiles(rtorrent.Torrent{Hash: hash})
if err != nil {
return errors.Wrap(err, "failed to get files")
}
for _, file := range files {
fmt.Println(file.Pretty())
}
return nil
}