forked from gdm85/go-libdeluge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
364 lines (320 loc) · 10.4 KB
/
cli.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// go-libdeluge v0.5.6 - a native deluge RPC client library
// Copyright (C) 2015~2020 gdm85 - https://github.com/gdm85/go-libdeluge/
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// Command line util to issue requests against a headless deluge server.
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
delugeclient "github.com/Haibread/go-libdeluge"
)
var (
host string
port uint
username, password string
logLevel string
addURI string
listTorrents bool
listAvailablePlugins bool
listEnabledPlugins bool
enablePlugin string
disablePlugin string
listAccounts bool
torrentHash string
setLabel string
addLabel string
removeLabel string
getLabels bool
listLabels bool
v2daemon bool
free bool
testListenPort bool
sessionStatus bool
fs = flag.NewFlagSet("default", flag.ContinueOnError)
)
func init() {
fs.StringVar(&host, "host", "localhost", "Deluge server host")
fs.StringVar(&host, "h", "localhost", "Deluge server host (shorthand)")
fs.UintVar(&port, "port", 58846, "Deluge server port")
fs.UintVar(&port, "p", 58846, "Deluge server port (shorthand)")
fs.StringVar(&username, "username", "localclient", "Deluge user name")
fs.StringVar(&username, "u", "localclient", "Deluge user name (shorthand)")
fs.StringVar(&password, "password", "", "Deluge password; use environment DELUGE_PASSWORD instead")
fs.StringVar(&logLevel, "log-level", "", "Log level, one of 'DEBUG' or 'NONE'")
fs.StringVar(&logLevel, "l", "", "Log level, one of 'DEBUG' or 'NONE' (shorthand)")
fs.StringVar(&addURI, "a", "", "Add a torrent via magnet URI")
fs.StringVar(&addURI, "add", "", "Add a torrent via magnet URI")
fs.BoolVar(&v2daemon, "v2", false, "Use protocol compatible with a v2 daemon")
fs.BoolVar(&listTorrents, "e", false, "List all torrents")
fs.BoolVar(&listTorrents, "list", false, "List all torrents")
fs.BoolVar(&listEnabledPlugins, "list-enabled-plugins", false, "List enabled plugins")
fs.BoolVar(&listEnabledPlugins, "P", false, "List enabled plugins")
fs.BoolVar(&listAvailablePlugins, "list-available-plugins", false, "List available plugins")
fs.BoolVar(&listAvailablePlugins, "A", false, "List available plugins")
fs.StringVar(&enablePlugin, "enable-plugin", "", "Enable a plugin")
fs.StringVar(&disablePlugin, "disable-plugin", "", "Disable a plugin")
fs.StringVar(&torrentHash, "torrent", "", "Operate on specified torrent hash")
fs.StringVar(&torrentHash, "t", "", "Operate on specified torrent hash")
fs.StringVar(&setLabel, "set-label", "", "Set label on torrent")
fs.StringVar(&setLabel, "b", "", "Set label on torrent")
fs.StringVar(&addLabel, "add-label", "", "Add label on torrent")
fs.StringVar(&addLabel, "c", "", "Add label on torrent")
fs.StringVar(&removeLabel, "remove-label", "", "Remove label on torrent")
fs.StringVar(&removeLabel, "r", "", "Remove label on torrent")
fs.BoolVar(&getLabels, "get-labels", false, "List all labels")
fs.BoolVar(&listLabels, "list-labels", false, "List all torrents' labels")
fs.BoolVar(&listLabels, "g", false, "List all torrents' labels")
fs.BoolVar(&free, "f", false, "Display free space")
fs.BoolVar(&free, "free", false, "Display free space")
fs.BoolVar(&testListenPort, "o", false, "Test listen port")
fs.BoolVar(&testListenPort, "test-listen-port", false, "Test listen port")
fs.BoolVar(&listAccounts, "list-accounts", false, "List all known user accounts")
fs.BoolVar(&sessionStatus, "s", false, "Show session status")
fs.BoolVar(&sessionStatus, "session-status", false, "Show session status")
}
func main() {
err := fs.Parse(os.Args[1:])
if err != nil {
os.Exit(1)
}
// parse password via environment variable
for _, decl := range os.Environ() {
parts := strings.SplitN(decl, "=", 2)
if len(parts) != 2 {
continue
}
if parts[0] == "DELUGE_PASSWORD" {
password = parts[1]
break
}
}
// validate log level
var logger *log.Logger
var debugIncoming bool
switch logLevel {
case "", "NONE":
// nothing to do
case "DEBUG":
logger = log.New(os.Stderr, "DELUGE: ", log.Lshortfile)
debugIncoming = true
default:
fmt.Fprintf(os.Stderr, "ERROR: invalid log level %q specified\n", logLevel)
os.Exit(2)
}
settings := delugeclient.Settings{
Hostname: host,
Port: port,
Login: username,
Password: password,
Logger: logger,
DebugServerResponses: debugIncoming}
deluge := delugeclient.NewV2(settings)
// perform connection to Deluge server
err = deluge.Connect()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: connection failed: %v\n", err)
os.Exit(3)
}
defer deluge.Close()
// print daemon version
ver, err := deluge.DaemonVersion()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: daemon version retrieval: %v\n", err)
os.Exit(4)
}
fmt.Printf("Deluge daemon version: %v\n", ver)
// print available methods
methods, err := deluge.MethodsList()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: methods list retrieval: %v\n", err)
os.Exit(5)
}
fmt.Println("available methods:", methods)
// add each of the torrents
if addURI != `` {
torrentHash, err := deluge.AddTorrentMagnet(addURI, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not add magnet URI '%s': %v\n", addURI, err)
os.Exit(5)
}
if torrentHash == "" {
fmt.Println("torrent was not added")
} else {
fmt.Println("added torrent with hash:", torrentHash)
}
}
if free {
n, err := deluge.GetFreeSpace("")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not read free space: %v\n", err)
os.Exit(6)
}
fmt.Printf("free space: %d bytes\n", n)
}
if listAvailablePlugins {
plugins, err := deluge.GetAvailablePlugins()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: available plugins list retrieval: %v\n", err)
os.Exit(5)
}
fmt.Println("available plugins:", plugins)
}
if listEnabledPlugins {
plugins, err := deluge.GetEnabledPlugins()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enabled plugins list retrieval: %v\n", err)
os.Exit(5)
}
fmt.Println("enabled plugins:", plugins)
}
if enablePlugin != "" {
err := deluge.EnablePlugin(enablePlugin)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enable plugin %s: %v\n", enablePlugin, err)
os.Exit(5)
}
}
if disablePlugin != "" {
err := deluge.DisablePlugin(disablePlugin)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: disable plugin %s: %v\n", disablePlugin, err)
os.Exit(5)
}
}
if setLabel != "" {
if torrentHash == "" {
fmt.Fprintf(os.Stderr, "ERROR: no torrent hash specified\n")
os.Exit(5)
}
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enabled plugins list retrieval: %v\n", err)
os.Exit(5)
}
err = p.SetTorrentLabel(torrentHash, setLabel)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: setting label %q on torrent %q: %v\n", setLabel, torrentHash, err)
os.Exit(5)
}
}
if addLabel != "" {
if torrentHash != "" {
fmt.Fprintf(os.Stderr, "ERROR: no torrent hash should be specified\n")
os.Exit(5)
}
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: label plugin: %v\n", err)
os.Exit(5)
}
err = p.AddLabel(addLabel)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: adding label %q: %v\n", addLabel, err)
os.Exit(5)
}
}
if removeLabel != "" {
if torrentHash != "" {
fmt.Fprintf(os.Stderr, "ERROR: no torrent hash should be specified\n")
os.Exit(5)
}
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: label plugin: %v\n", err)
os.Exit(5)
}
err = p.RemoveLabel(removeLabel)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: removing label %q: %v\n", addLabel, err)
os.Exit(5)
}
}
if getLabels {
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: label plugin: %v\n", err)
os.Exit(5)
}
labels, err := p.GetLabels()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: retrieving labels: %v\n", err)
os.Exit(5)
}
je := json.NewEncoder(os.Stdout)
je.SetIndent("", "\t")
if err := je.Encode(labels); err != nil {
os.Exit(7)
}
}
if listLabels {
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enabled plugins list retrieval: %v\n", err)
os.Exit(5)
}
labelsByTorrent, err := p.GetTorrentsLabels(delugeclient.StateUnspecified, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enabled plugins list retrieval: %v\n", err)
os.Exit(5)
}
je := json.NewEncoder(os.Stdout)
je.SetIndent("", "\t")
if err := je.Encode(labelsByTorrent); err != nil {
os.Exit(7)
}
}
if listTorrents {
torrents, err := deluge.TorrentsStatus(delugeclient.StateUnspecified, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not list all torrents: %v\n", err)
os.Exit(6)
}
je := json.NewEncoder(os.Stdout)
je.SetIndent("", "\t")
if err := je.Encode(torrents); err != nil {
os.Exit(7)
}
}
if listAccounts {
accounts, err := deluge.KnownAccounts()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not list all accounts: %v\n", err)
os.Exit(6)
}
je := json.NewEncoder(os.Stdout)
je.SetIndent("", "\t")
if err := je.Encode(accounts); err != nil {
os.Exit(7)
}
}
if testListenPort {
success, err := deluge.TestListenPort()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not test listen port: %v\n", err)
os.Exit(6)
}
fmt.Printf("test listen port: %v\n", success)
}
if sessionStatus {
status, err := deluge.GetSessionStatus()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not get session status: %v\n", err)
os.Exit(6)
}
fmt.Printf("session status: %+v\n", status)
}
}