-
Notifications
You must be signed in to change notification settings - Fork 18
/
getcategories.go
54 lines (46 loc) · 1.32 KB
/
getcategories.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
package getcategories
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/sagan/ptool/client"
"github.com/sagan/ptool/cmd"
"github.com/sagan/ptool/util"
)
var command = &cobra.Command{
Use: "getcategories {client}",
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "getcategories"},
Short: "Get all categories of client.",
Long: `Get all categories of client.`,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: getcategories,
}
var (
showNamesOnly = false
)
func init() {
command.Flags().BoolVarP(&showNamesOnly, "show-names-only", "", false, "Show category names only")
cmd.RootCmd.AddCommand(command)
}
func getcategories(cmd *cobra.Command, args []string) error {
clientName := args[0]
clientInstance, err := client.CreateClient(clientName)
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
cats, err := clientInstance.GetCategories()
if err != nil {
return fmt.Errorf("failed to get categories: %v", err)
}
if showNamesOnly {
fmt.Printf("%s\n", strings.Join(util.Map(cats, func(cat client.TorrentCategory) string {
return cat.Name
}), ", "))
} else {
fmt.Printf("%-20s %s\n", "Name", "SavePath")
for _, cat := range cats {
fmt.Printf("%-20s %s\n", cat.Name, cat.SavePath)
}
}
return nil
}