-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstore.go
More file actions
228 lines (198 loc) · 5.2 KB
/
Copy pathstore.go
File metadata and controls
228 lines (198 loc) · 5.2 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
package macro
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
)
var (
// ErrNotFound is returned when a macro with the given name does not exist.
ErrNotFound = errors.New("macro: not found")
// ErrEmptyName is returned when a macro name is empty.
ErrEmptyName = errors.New("macro: name must not be empty")
// ErrNoSteps is returned when a macro has no steps.
ErrNoSteps = errors.New("macro: must have at least one step")
)
// Store manages CRUD operations and file persistence for macros.
// It mirrors the preset.Store pattern: file-based JSON with
// sync.RWMutex and atomic temp-file + rename writes.
type Store struct {
mu sync.RWMutex
macros []Macro
filePath string
}
// NewStore creates a Store that persists to the given file path.
// If the file exists, macros are loaded from it. If it does not exist,
// the store starts empty and the file is created on first mutation.
func NewStore(filePath string) (*Store, error) {
s := &Store{
filePath: filePath,
macros: []Macro{},
}
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return s, nil
}
return nil, fmt.Errorf("read macros file: %w", err)
}
if err := json.Unmarshal(data, &s.macros); err != nil {
return nil, fmt.Errorf("parse macros file: %w", err)
}
if s.macros == nil {
s.macros = []Macro{}
}
return s, nil
}
// List returns all macros.
func (s *Store) List() []Macro {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]Macro, len(s.macros))
copy(result, s.macros)
return result
}
// Get returns a macro by name.
func (s *Store) Get(name string) (Macro, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, m := range s.macros {
if m.Name == name {
return m, nil
}
}
return Macro{}, ErrNotFound
}
// Save creates or updates a macro. If a macro with the same name exists,
// it is replaced.
func (s *Store) Save(m Macro) error {
if m.Name == "" {
return ErrEmptyName
}
if len(m.Steps) == 0 {
return ErrNoSteps
}
result := ValidateSteps(m.Steps)
if result.HasErrors() {
return &result.Errors[0]
}
s.mu.Lock()
// Replace existing macro with same name, or append.
var previous Macro
replaceIdx := -1
for i := range s.macros {
if s.macros[i].Name == m.Name {
previous = s.macros[i]
replaceIdx = i
s.macros[i] = m
break
}
}
if replaceIdx == -1 {
s.macros = append(s.macros, m)
}
data, err := s.marshalLocked()
if err != nil {
// Roll back: restore previous state.
if replaceIdx >= 0 {
s.macros[replaceIdx] = previous
} else {
s.macros = s.macros[:len(s.macros)-1]
}
s.mu.Unlock()
return fmt.Errorf("save macros: %w", err)
}
if err := s.writeFile(data); err != nil {
// Roll back: restore previous state before releasing the lock.
if replaceIdx >= 0 {
s.macros[replaceIdx] = previous
} else {
s.macros = s.macros[:len(s.macros)-1]
}
s.mu.Unlock()
return fmt.Errorf("save macros: %w", err)
}
s.mu.Unlock()
return nil
}
// Delete removes a macro by name.
func (s *Store) Delete(name string) error {
s.mu.Lock()
idx := -1
for i, m := range s.macros {
if m.Name == name {
idx = i
break
}
}
if idx == -1 {
s.mu.Unlock()
return ErrNotFound
}
removed := s.macros[idx]
s.macros = append(s.macros[:idx], s.macros[idx+1:]...)
data, err := s.marshalLocked()
if err != nil {
// Roll back: re-insert at original position.
rear := make([]Macro, len(s.macros[idx:]))
copy(rear, s.macros[idx:])
s.macros = append(s.macros[:idx], removed)
s.macros = append(s.macros, rear...)
s.mu.Unlock()
return fmt.Errorf("save macros: %w", err)
}
if err := s.writeFile(data); err != nil {
// Roll back: re-insert at original position before releasing the lock.
rear := make([]Macro, len(s.macros[idx:]))
copy(rear, s.macros[idx:])
s.macros = append(s.macros[:idx], removed)
s.macros = append(s.macros, rear...)
s.mu.Unlock()
return fmt.Errorf("save macros: %w", err)
}
s.mu.Unlock()
return nil
}
// marshalLocked serializes the current macros to JSON.
// Must be called with s.mu held.
func (s *Store) marshalLocked() ([]byte, error) {
data, err := json.MarshalIndent(s.macros, "", " ")
if err != nil {
return nil, fmt.Errorf("marshal macros: %w", err)
}
return data, nil
}
// writeFile writes pre-serialized data to disk atomically (temp file + rename).
// Called with the lock held to prevent transient state visibility.
func (s *Store) writeFile(data []byte) error {
dir := filepath.Dir(s.filePath)
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("create directory %s: %w", dir, err)
}
tmpFile, err := os.CreateTemp(dir, "macros-*.tmp")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
tmpPath := tmpFile.Name()
if _, err := tmpFile.Write(data); err != nil {
_ = tmpFile.Close()
_ = os.Remove(tmpPath)
return fmt.Errorf("write temp file: %w", err)
}
if err := tmpFile.Sync(); err != nil {
_ = tmpFile.Close()
_ = os.Remove(tmpPath)
return fmt.Errorf("fsync temp file: %w", err)
}
if err := tmpFile.Close(); err != nil {
_ = os.Remove(tmpPath)
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Rename(tmpPath, s.filePath); err != nil {
_ = os.Remove(tmpPath)
return fmt.Errorf("rename temp file: %w", err)
}
return nil
}