-
Notifications
You must be signed in to change notification settings - Fork 18
/
reannounce.go
77 lines (69 loc) · 2.27 KB
/
reannounce.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
package reannounce
import (
"fmt"
"github.com/spf13/cobra"
"github.com/sagan/ptool/client"
"github.com/sagan/ptool/cmd"
"github.com/sagan/ptool/util/helper"
)
var command = &cobra.Command{
Use: "reannounce {client} [--category category] [--tag tag] [--filter filter] [infoHash]...",
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "reannounce"},
Short: "Reannounce torrents of client.",
Long: `Reannounce torrents of client.
[infoHash]...: infoHash list of torrents. It's possible to use state filter to target multiple torrents:
_all, _active, _done, _undone, _downloading, _seeding, _paused, _completed, _error.
Specially, use a single "-" as args to read infoHash list from stdin, delimited by blanks.`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
RunE: reannounce,
}
var (
category = ""
tag = ""
filter = ""
)
func init() {
command.Flags().StringVarP(&filter, "filter", "", "", "Filter torrents by name")
command.Flags().StringVarP(&category, "category", "", "", "Filter torrents by category")
command.Flags().StringVarP(&tag, "tag", "", "",
"Filter torrents by tag. Comma-separated list. Torrent which tags contain any one in the list matches")
cmd.RootCmd.AddCommand(command)
}
func reannounce(cmd *cobra.Command, args []string) error {
clientName := args[0]
infoHashes := args[1:]
if category == "" && tag == "" && filter == "" {
if len(infoHashes) == 0 {
return fmt.Errorf("you must provide at least a condition flag or hashFilter")
}
if len(infoHashes) == 1 && infoHashes[0] == "-" {
if data, err := helper.ReadArgsFromStdin(); err != nil {
return fmt.Errorf("failed to parse stdin to info hashes: %v", err)
} else if len(data) == 0 {
return nil
} else {
infoHashes = data
}
}
}
clientInstance, err := client.CreateClient(clientName)
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
infoHashes, err = client.SelectTorrents(clientInstance, category, tag, filter, infoHashes...)
if err != nil {
return err
}
if infoHashes == nil {
err = clientInstance.ReannounceAllTorrents()
if err != nil {
return err
}
} else if len(infoHashes) > 0 {
err = clientInstance.ReannounceTorrents(infoHashes)
if err != nil {
return err
}
}
return nil
}