-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunmapped.go
More file actions
84 lines (70 loc) · 2.16 KB
/
unmapped.go
File metadata and controls
84 lines (70 loc) · 2.16 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
)
// Sync direction constants (string values for JSON serialization).
const (
DirectionForwardStr = "forward" // AniList → MAL
DirectionReverseStr = "reverse" // MAL → AniList
)
// UnmappedEntry represents an entry that could not be mapped during sync.
type UnmappedEntry struct {
AniListID int `json:"anilist_id"`
MALID int `json:"mal_id,omitempty"`
Title string `json:"title"`
MediaType string `json:"media_type"` // "anime" or "manga"
Direction string `json:"direction"` // "forward" or "reverse"
Reason string `json:"reason,omitempty"` // why it was unmapped
}
// UnmappedState holds the list of unmapped entries from the last sync.
type UnmappedState struct {
Entries []UnmappedEntry `json:"entries"`
UpdatedAt time.Time `json:"updated_at"`
}
// LoadUnmappedState loads unmapped state from a JSON file.
func LoadUnmappedState(path string) (*UnmappedState, error) {
if path == "" {
path = getDefaultUnmappedPath()
}
data, err := os.ReadFile(path) // #nosec G304 - Path from user config
if err != nil {
if os.IsNotExist(err) {
return &UnmappedState{}, nil
}
return nil, fmt.Errorf("read unmapped state: %w", err)
}
var state UnmappedState
if err := json.Unmarshal(data, &state); err != nil {
return nil, fmt.Errorf("parse unmapped state: %w", err)
}
return &state, nil
}
// Save writes the unmapped state to a JSON file.
func (s *UnmappedState) Save(path string) error {
if path == "" {
path = getDefaultUnmappedPath()
}
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o750); err != nil { // #nosec G301 - State directory
return fmt.Errorf("create unmapped state directory: %w", err)
}
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("marshal unmapped state: %w", err)
}
if err := os.WriteFile(path, data, 0o600); err != nil {
return fmt.Errorf("write unmapped state: %w", err)
}
return nil
}
func getDefaultUnmappedPath() string {
configDir, err := os.UserConfigDir()
if err != nil {
return ""
}
return filepath.Join(configDir, "anilist-mal-sync", "state", "unmapped.json")
}