-
Notifications
You must be signed in to change notification settings - Fork 18
/
setsavepath.go
78 lines (70 loc) · 2.34 KB
/
setsavepath.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
package setsavepath
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: "setsavepath {client} {savePath} [--category category] [--tag tag] [--filter filter] [infoHash]...",
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "setsavepath"},
Short: "Set the save path of torrents in client.",
Long: `Set the save path of torrents in 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(2), cobra.OnlyValidArgs),
RunE: setsavepath,
}
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 setsavepath(cmd *cobra.Command, args []string) error {
clientName := args[0]
savePath := args[1]
infoHashes := args[2:]
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.SetAllTorrentsSavePath(savePath)
if err != nil {
return err
}
} else if len(infoHashes) > 0 {
err = clientInstance.SetTorrentsSavePath(infoHashes, savePath)
if err != nil {
return err
}
}
return nil
}