-
Notifications
You must be signed in to change notification settings - Fork 10
/
csvmappings.go
168 lines (147 loc) · 4.36 KB
/
csvmappings.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
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
/*
TapTo
Copyright (C) 2023, 2024 Callan Barrett
This file is part of TapTo.
TapTo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TapTo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TapTo. If not, see <http://www.gnu.org/licenses/>.
*/
package launcher
import (
_ "embed"
"errors"
"io"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/gocarina/gocsv"
"github.com/rs/zerolog/log"
"github.com/wizzomafizzo/tapto/pkg/platforms/mister"
)
type CsvMappingEntry struct {
MatchUID string `csv:"match_uid"`
MatchText string `csv:"match_text"`
Text string `csv:"text"`
}
func LoadCsvMappings() (map[string]string, map[string]string, error) {
uids := make(map[string]string)
texts := make(map[string]string)
if _, err := os.Stat(mister.MappingsFile); errors.Is(err, os.ErrNotExist) {
log.Info().Msg("no database file found, skipping")
return nil, nil, nil
}
f, err := os.Open(mister.MappingsFile)
if err != nil {
return nil, nil, err
}
defer func(c io.Closer) {
_ = c.Close()
}(f)
entries := make([]CsvMappingEntry, 0)
err = gocsv.Unmarshal(f, &entries)
if err != nil {
return nil, nil, err
}
count := 0
for i, entry := range entries {
if entry.MatchUID == "" && entry.MatchText == "" {
log.Warn().Msgf("entry %d has no UID or text, skipping", i+1)
continue
}
if entry.MatchUID != "" {
uid := strings.TrimSpace(entry.MatchUID)
uid = strings.ToLower(uid)
uid = strings.ReplaceAll(uid, ":", "")
uids[uid] = strings.TrimSpace(entry.Text)
}
if entry.MatchText != "" {
text := strings.TrimSpace(entry.MatchText)
texts[text] = strings.TrimSpace(entry.Text)
}
count++
}
log.Info().Msgf("loaded %d entries from database", count)
return uids, texts, nil
}
func StartCsvMappingsWatcher(
getLoadTime func() time.Time,
setMappings func(map[string]string, map[string]string),
) (func() error, error) {
var closeDbWatcher func() error
dbWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Error().Msgf("error creating mappings watcher: %s", err)
} else {
closeDbWatcher = dbWatcher.Close
}
go func() {
// this turned out to be not trivial to say the least, mostly due to
// the fact the fsnotify library does not implement the IN_CLOSE_WRITE
// inotify event, which signals the file has finished being written
// see: https://github.com/fsnotify/fsnotify/issues/372
//
// during a standard write operation, a file may emit multiple write
// events, including when the file could be half-written
//
// it's also the case that editors may delete the file and create a new
// one, which kills the active watcher
const delay = 1 * time.Second
for {
select {
case event, ok := <-dbWatcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
// usually receives multiple write events, just act on the first
if time.Since(getLoadTime()) < delay {
continue
}
time.Sleep(delay)
log.Info().Msg("database changed, reloading")
uids, texts, err := LoadCsvMappings()
if err != nil {
log.Error().Msgf("error loading database: %s", err)
} else {
setMappings(uids, texts)
}
} else if event.Has(fsnotify.Remove) {
// editors may also delete the file on write
time.Sleep(delay)
_, err := os.Stat(mister.MappingsFile)
if err == nil {
err = dbWatcher.Add(mister.MappingsFile)
if err != nil {
log.Error().Msgf("error watching database: %s", err)
}
log.Info().Msg("database changed, reloading")
uids, texts, err := LoadCsvMappings()
if err != nil {
log.Error().Msgf("error loading database: %s", err)
} else {
setMappings(uids, texts)
}
}
}
case err, ok := <-dbWatcher.Errors:
if !ok {
return
}
log.Error().Msgf("watcher error: %s", err)
}
}
}()
err = dbWatcher.Add(mister.MappingsFile)
if err != nil {
log.Error().Msgf("error watching database: %s", err)
}
return closeDbWatcher, nil
}