-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.go
177 lines (135 loc) · 3.33 KB
/
storage.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
169
170
171
172
173
174
175
176
177
package goldga
//go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=storage_mock_test.go Storage
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"time"
"github.com/BurntSushi/toml"
"github.com/spf13/afero"
)
// nolint: gochecknoglobals
var defaultFs = afero.NewCacheOnReadFs(
afero.NewOsFs(),
afero.NewMemMapFs(),
time.Minute,
)
type Storage interface {
Read() ([]byte, error)
Write(data []byte) error
}
var _ Storage = (*SingleStorage)(nil)
type SingleStorage struct {
Path string
Fs afero.Fs
}
func (s *SingleStorage) Read() ([]byte, error) {
data, err := afero.ReadFile(s.Fs, s.Path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return data, nil
}
func (s *SingleStorage) Write(data []byte) error {
if err := s.Fs.MkdirAll(filepath.Dir(s.Path), os.ModePerm); err != nil {
return fmt.Errorf("failed to create directories: %w", err)
}
if err := afero.WriteFile(s.Fs, s.Path, data, os.ModePerm); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
type suiteData struct {
Snapshots map[string]string `toml:"snapshots"`
}
func newSuiteData() *suiteData {
return &suiteData{
Snapshots: map[string]string{},
}
}
func (s *suiteData) sortSnapshotKeys() []string {
keys := make([]string, 0, len(s.Snapshots))
for k := range s.Snapshots {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
var _ Storage = (*SuiteStorage)(nil)
type SuiteStorage struct {
Path string
Name string
Fs afero.Fs
}
func (s *SuiteStorage) getSuiteData() (*suiteData, error) {
exists, err := afero.Exists(s.Fs, s.Path)
if err != nil {
return nil, fmt.Errorf("failed to check file exist: %w", err)
}
if !exists {
return nil, afero.ErrFileNotFound
}
file, err := s.Fs.Open(s.Path)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
data := newSuiteData()
if _, err := toml.NewDecoder(file).Decode(&data); err != nil {
return nil, fmt.Errorf("toml decode error: %w", err)
}
return data, nil
}
func (s *SuiteStorage) Read() ([]byte, error) {
data, err := s.getSuiteData()
if err != nil {
return nil, err
}
if s, ok := data.Snapshots[s.Name]; ok {
return []byte(s), nil
}
return nil, afero.ErrFileNotFound
}
func (s *SuiteStorage) Write(input []byte) error {
data, err := s.getSuiteData()
if err != nil {
if !errors.Is(err, afero.ErrFileNotFound) {
return err
}
data = newSuiteData()
}
data.Snapshots[s.Name] = string(input)
if err := s.Fs.MkdirAll(filepath.Dir(s.Path), os.ModePerm); err != nil {
return fmt.Errorf("failed to create directories: %w", err)
}
file, err := s.Fs.Create(s.Path)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
w := bufio.NewWriter(file)
lines := []string{
"# Generated by goldga. DO NOT EDIT.",
"[snapshots]",
}
// Print header
for _, line := range lines {
if _, err := fmt.Fprintln(w, line); err != nil {
return fmt.Errorf("header write error: %w", err)
}
}
// Print snapshots
for _, k := range data.sortSnapshotKeys() {
v := data.Snapshots[k]
if _, err := fmt.Fprintf(w, "%q = '''\n%s'''\n", k, v); err != nil {
return fmt.Errorf("snapshot write error: %w", err)
}
}
if err := w.Flush(); err != nil {
return fmt.Errorf("flush error: %w", err)
}
return nil
}