-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_unmapped.go
More file actions
268 lines (235 loc) · 6.63 KB
/
cmd_unmapped.go
File metadata and controls
268 lines (235 loc) · 6.63 KB
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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/urfave/cli/v3"
)
func newUnmappedCommand() *cli.Command {
return &cli.Command{
Name: "unmapped",
Usage: "Show and manage unmapped entries from last sync",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "fix",
Usage: "interactively fix unmapped entries",
},
&cli.BoolFlag{
Name: "ignore-all",
Usage: "add all unmapped entries to ignore list",
},
},
Action: runUnmapped,
}
}
func runUnmapped(_ context.Context, cmd *cli.Command) error {
configPath := cmd.String("config")
mappingsPath := resolveMappingsPath(configPath)
state, err := LoadUnmappedState("")
if err != nil {
return fmt.Errorf("load unmapped state: %w", err)
}
if len(state.Entries) == 0 {
log.Println("No unmapped entries found. Run 'sync' first.")
return nil
}
if cmd.Bool("ignore-all") {
return runUnmappedIgnoreAll(state, mappingsPath)
}
if cmd.Bool("fix") {
return runUnmappedFix(state, mappingsPath)
}
return runUnmappedList(state)
}
func resolveMappingsPath(configPath string) string {
if configPath == "" {
return ""
}
config, err := loadConfigFromFile(configPath)
if err != nil {
return ""
}
return config.MappingsFilePath
}
func runUnmappedList(state *UnmappedState) error {
log.Printf("Unmapped entries (%d) from last sync (%s):\n",
len(state.Entries), state.UpdatedAt.Format("2006-01-02 15:04:05"))
for i, entry := range state.Entries {
printUnmappedEntry(i+1, entry)
}
log.Println("\nUse --fix to interactively manage these entries")
log.Println("Use --ignore-all to add all to ignore list")
return nil
}
func printUnmappedEntry(num int, entry UnmappedEntry) {
mediaLabel := capitalizeFirst(entry.MediaType)
log.Println(formatUnmappedLine(num, entry, mediaLabel))
}
func isReverseEntry(entry UnmappedEntry) bool {
return entry.Direction == DirectionReverseStr
}
func runUnmappedIgnoreAll(state *UnmappedState, mappingsPath string) error {
mappings, err := LoadMappings(mappingsPath)
if err != nil {
return fmt.Errorf("load mappings: %w", err)
}
added := 0
for _, entry := range state.Entries {
if addIgnoreEntry(entry, mappings) {
logIgnoredEntry(entry)
added++
}
}
if added == 0 {
log.Println("All entries are already in the ignore list.")
return nil
}
if err := mappings.Save(mappingsPath); err != nil {
return fmt.Errorf("save mappings: %w", err)
}
savePath := mappingsPath
if savePath == "" {
savePath = getDefaultMappingsPath()
}
log.Printf("Added %d entries to ignore list in %s", added, savePath)
return nil
}
func addIgnoreEntry(entry UnmappedEntry, mappings *MappingsConfig) bool {
if isReverseEntry(entry) {
if entry.MALID > 0 && !mappings.IsIgnoredByMALID(entry.MALID) {
mappings.AddIgnoreByMALID(entry.MALID, entry.Title, entry.Reason)
return true
}
return false
}
if entry.AniListID > 0 && !mappings.IsIgnored(entry.AniListID, entry.Title) {
mappings.AddIgnoreByID(entry.AniListID, entry.Title, entry.Reason)
return true
}
return false
}
func logIgnoredEntry(entry UnmappedEntry) {
if isReverseEntry(entry) {
log.Printf(" + %q (MAL: %d)", entry.Title, entry.MALID)
} else {
log.Printf(" + %q (AniList: %d)", entry.Title, entry.AniListID)
}
}
func runUnmappedFix(state *UnmappedState, mappingsPath string) error {
mappings, err := LoadMappings(mappingsPath)
if err != nil {
return fmt.Errorf("load mappings: %w", err)
}
reader := bufio.NewReader(os.Stdin)
changed := false
for i, entry := range state.Entries {
printFixHeader(i+1, len(state.Entries), entry)
action := promptFixAction(reader, entry)
changed = applyFixAction(action, entry, mappings, reader) || changed
if action == "q" {
break
}
}
if changed {
err := mappings.Save(mappingsPath)
if err != nil {
return fmt.Errorf("save mappings: %w", err)
}
savePath := mappingsPath
if savePath == "" {
savePath = getDefaultMappingsPath()
}
log.Printf("Saved changes to %s", savePath)
}
return nil
}
func printFixHeader(num, total int, entry UnmappedEntry) {
mediaLabel := capitalizeFirst(entry.MediaType)
switch {
case entry.AniListID > 0:
log.Printf("\n[%d/%d] %q (AniList: %d, %s)", num, total, entry.Title, entry.AniListID, mediaLabel)
case entry.MALID > 0:
log.Printf("\n[%d/%d] %q (MAL: %d, %s)", num, total, entry.Title, entry.MALID, mediaLabel)
default:
log.Printf("\n[%d/%d] %q (%s)", num, total, entry.Title, mediaLabel)
}
}
func promptFixAction(reader *bufio.Reader, entry UnmappedEntry) string {
mapLabel := "MAL ID"
if isReverseEntry(entry) {
mapLabel = "AniList ID"
}
log.Printf("\nAction: [i]gnore [m]ap to %s [s]kip [q]uit\n> ", mapLabel)
input, err := reader.ReadString('\n')
if err != nil {
return "s"
}
return strings.TrimSpace(strings.ToLower(input))
}
func applyFixAction(action string, entry UnmappedEntry, mappings *MappingsConfig, reader *bufio.Reader) bool {
switch action {
case "i":
return applyIgnoreAction(entry, mappings)
case "m":
return applyMapAction(entry, mappings, reader)
case "q":
log.Println("Quitting...")
default:
log.Println(" -> Skipped")
}
return false
}
func applyIgnoreAction(entry UnmappedEntry, mappings *MappingsConfig) bool {
if isReverseEntry(entry) {
if entry.MALID > 0 {
mappings.AddIgnoreByMALID(entry.MALID, entry.Title, entry.Reason)
log.Printf(" -> Added MAL ID %d to ignore list", entry.MALID)
return true
}
log.Println(" -> Cannot ignore: no MAL ID available")
return false
}
if entry.AniListID > 0 {
mappings.AddIgnoreByID(entry.AniListID, entry.Title, entry.Reason)
log.Printf(" -> Added AniList ID %d to ignore list", entry.AniListID)
return true
}
log.Println(" -> Cannot ignore: no AniList ID available")
return false
}
func applyMapAction(entry UnmappedEntry, mappings *MappingsConfig, reader *bufio.Reader) bool {
if isReverseEntry(entry) {
anilistID, ok := promptID(reader, "AniList")
if ok && entry.MALID > 0 {
mappings.AddManualMapping(anilistID, entry.MALID, entry.Title)
log.Printf(" -> Mapped AniList %d -> MAL %d", anilistID, entry.MALID)
return true
}
return false
}
malID, ok := promptID(reader, "MAL")
if ok && entry.AniListID > 0 {
mappings.AddManualMapping(entry.AniListID, malID, entry.Title)
log.Printf(" -> Mapped AniList %d -> MAL %d", entry.AniListID, malID)
return true
}
return false
}
func promptID(reader *bufio.Reader, label string) (int, bool) {
log.Printf(" Enter %s ID: ", label)
input, err := reader.ReadString('\n')
if err != nil {
return 0, false
}
input = strings.TrimSpace(input)
id, err := strconv.Atoi(input)
if err != nil || id <= 0 {
log.Printf(" Invalid %s ID", label)
return 0, false
}
return id, true
}