/
list.go
78 lines (66 loc) · 1.58 KB
/
list.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 cmd
import (
"fmt"
"log"
"github.com/LucaCtt/thelist/common"
"github.com/LucaCtt/thelist/common/client"
"github.com/LucaCtt/thelist/common/store"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func list(p Prompt, c client.Client, s store.Store) error {
items, err := s.All()
if err != nil {
return fmt.Errorf("get items from store failed: %w", err)
}
if len(items) == 0 {
return fmt.Errorf("The list is empty")
}
shows := make([]*common.Show, len(items))
for i, item := range items {
show, err := common.GetShow(c, item.ShowID, item.Type)
if err != nil {
return fmt.Errorf("get show from client failed: %w", err)
}
shows[i] = show
}
options := make([]string, len(shows))
for i, s := range shows {
if s.Year == 0 {
options[i] = fmt.Sprintf("%s", s.Name)
continue
}
options[i] = fmt.Sprintf("%s (%d)", s.Name, s.Year)
}
watched, err := p.MultiSelect("Shows", options)
if err != nil {
return fmt.Errorf("prompt shows list failed: %w", err)
}
for _, w := range watched {
err = s.Delete(items[w].ID)
if err != nil {
return fmt.Errorf("delete watched shows failed: %w", err)
}
}
return nil
}
func init() {
rootCmd.AddCommand(listCmd)
}
var listCmd = &cobra.Command{
Use: listCmdUse,
Short: listCmdShort,
Long: listCmdLong,
Run: func(cmd *cobra.Command, args []string) {
dbStore, err := store.New(viper.GetString(dbPathOpt))
if err != nil {
log.Fatal(err)
}
defer dbStore.Close()
client := client.New(viper.GetString(apiKeyOpt))
err = list(&CliPrompt{}, client, dbStore)
if err != nil {
log.Fatal(err)
}
},
}