-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexceptions.go
More file actions
138 lines (117 loc) · 3.34 KB
/
Copy pathexceptions.go
File metadata and controls
138 lines (117 loc) · 3.34 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
)
// ExceptionsManager handles exceptions for usernames that should be ignored
// in similarity checks
type ExceptionsManager struct {
Exceptions map[int64]bool // Map of user IDs to ignore
filePath string
mutex sync.RWMutex // Mutex for thread safety
}
// NewExceptionsManager creates a new ExceptionsManager and loads exceptions from the specified file
func NewExceptionsManager(filePath string) *ExceptionsManager {
em := &ExceptionsManager{
Exceptions: make(map[int64]bool),
filePath: filePath,
}
em.LoadExceptions()
return em
}
// LoadExceptions loads exceptions from the file
func (em *ExceptionsManager) LoadExceptions() {
em.mutex.Lock()
defer em.mutex.Unlock()
// Clear existing exceptions
em.Exceptions = make(map[int64]bool)
// Check if file exists
if _, err := os.Stat(em.filePath); os.IsNotExist(err) {
log.Printf("Exceptions file not found: %s", em.filePath)
return
}
// Open file
file, err := os.Open(em.filePath)
if err != nil {
log.Printf("Error opening exceptions file: %v", err)
return
}
defer file.Close()
// Read line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip comments and empty lines
if strings.HasPrefix(line, "#") || line == "" {
continue
}
// Parse user ID
userID, err := strconv.ParseInt(line, 10, 64)
if err != nil {
log.Printf("Error parsing user ID from line '%s': %v", line, err)
continue
}
em.Exceptions[userID] = true
}
log.Printf("Loaded %d user ID exceptions from %s", len(em.Exceptions), em.filePath)
}
// SaveExceptions saves the current exceptions to the file
func (em *ExceptionsManager) SaveExceptions() error {
em.mutex.RLock()
defer em.mutex.RUnlock()
// Create or truncate file
file, err := os.Create(em.filePath)
if err != nil {
return fmt.Errorf("error creating exceptions file: %v", err)
}
defer file.Close()
// Write header comment
_, err = fmt.Fprintf(file, "# List of user IDs to ignore in similarity checks\n")
if err != nil {
return fmt.Errorf("error writing header: %v", err)
}
// Write each user ID
for userID := range em.Exceptions {
_, err = fmt.Fprintf(file, "%d\n", userID)
if err != nil {
return fmt.Errorf("error writing user ID: %v", err)
}
}
log.Printf("Saved %d user ID exceptions to %s", len(em.Exceptions), em.filePath)
return nil
}
// AddException adds a user ID to the exceptions list
func (em *ExceptionsManager) AddException(userID int64) error {
em.mutex.Lock()
em.Exceptions[userID] = true
em.mutex.Unlock()
return em.SaveExceptions()
}
// RemoveException removes a user ID from the exceptions list
func (em *ExceptionsManager) RemoveException(userID int64) error {
em.mutex.Lock()
delete(em.Exceptions, userID)
em.mutex.Unlock()
return em.SaveExceptions()
}
// IsExcepted checks if a user ID is in the exceptions list
func (em *ExceptionsManager) IsExcepted(userID int64) bool {
em.mutex.RLock()
defer em.mutex.RUnlock()
return em.Exceptions[userID]
}
// ListExceptions returns a slice of all excepted user IDs
func (em *ExceptionsManager) ListExceptions() []int64 {
em.mutex.RLock()
defer em.mutex.RUnlock()
exceptions := make([]int64, 0, len(em.Exceptions))
for userID := range em.Exceptions {
exceptions = append(exceptions, userID)
}
return exceptions
}