-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
275 lines (232 loc) · 6.83 KB
/
config.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package restic
import (
"errors"
"fmt"
"os"
"strings"
"github.com/liuminhaw/wrestic-bkp/restic/tests"
"gopkg.in/yaml.v3"
)
var (
ErrConfigNotFound = errors.New("config file not found")
ErrConfigBackupNameNotFound = errors.New("backup name in config not found")
)
type Config struct {
Repository ConfigRepository `yaml:"repository"`
Backups []Backup `yaml:"backups"`
}
type BackupTypeConfig interface {
Validate() error
String() string
}
type ConfigRepository struct {
Password string `yaml:"password"`
}
type Backup struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Config BackupTypeConfig `yaml:"config"`
}
type LocalBackupConfig struct {
Sources []string `yaml:"sources"`
Destination string `yaml:"destination"`
Excludes []string `yaml:"excludes"`
}
func (c LocalBackupConfig) Validate() error {
return nil
}
func (c LocalBackupConfig) String() string {
var builder strings.Builder
builder.WriteString(srcDestString(c.Sources, c.Destination))
return builder.String()
}
type SftpBackupConfig struct {
Host string `yaml:"host"`
Sources []string `yaml:"sources"`
Destination string `yaml:"destination"`
Excludes []string `yaml:"excludes"`
}
func (c SftpBackupConfig) Validate() error {
return nil
}
func (c SftpBackupConfig) String() string {
var builder strings.Builder
builder.WriteString(srcDestString(c.Sources, c.Destination))
builder.WriteString(fmt.Sprintf("Host: %s\n", c.Host))
return builder.String()
}
type S3BackupConfig struct {
AccessKeyId string `yaml:"accessKeyId"`
SecretAccessKey string `yaml:"secretAccessKey"`
Region string `yaml:"region"`
Sources []string `yaml:"sources"`
Destination string `yaml:"destination"`
Excludes []string `yaml:"excludes"`
}
func (c S3BackupConfig) Validate() error {
return nil
}
func (c S3BackupConfig) String() string {
var builder strings.Builder
builder.WriteString(srcDestString(c.Sources, c.Destination))
builder.WriteString(fmt.Sprintf("Access Key ID: %s\n", c.AccessKeyId))
builder.WriteString(fmt.Sprintf("Secret Access Key: %s\n", c.SecretAccessKey))
builder.WriteString(fmt.Sprintf("Region: %s\n", c.Region))
return builder.String()
}
func srcDestString(sources []string, destination string) string {
var builder strings.Builder
builder.WriteString("Sources:\n")
for _, source := range sources {
builder.WriteString(fmt.Sprintf("- %s\n", source))
}
builder.WriteString(fmt.Sprintf("Destination: %s\n", destination))
return builder.String()
}
// NewConfig read in file and return restic Config type struct
func NewConfig(filepath string) (*Config, error) {
data, err := os.ReadFile(filepath)
if err != nil {
return nil, ErrConfigNotFound
}
var rawConfig struct {
Repository struct {
Password string `yaml:"password"`
} `yaml:"repository"`
Backups []struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Config map[string]interface{} `yaml:"config"`
} `yaml:"backups"`
}
err = yaml.Unmarshal(data, &rawConfig)
if err != nil {
return nil, fmt.Errorf("new config: %w", err)
}
// Process raw backup configuration
config := Config{Repository: rawConfig.Repository}
for _, rawBackup := range rawConfig.Backups {
var typedConfig BackupTypeConfig
switch rawBackup.Type {
case "local":
typedConfig = &LocalBackupConfig{}
case "sftp":
typedConfig = &SftpBackupConfig{}
case "s3":
typedConfig = &S3BackupConfig{}
default:
return nil, fmt.Errorf("new config: unsupportedt type %s", rawBackup.Type)
}
configBytes, err := yaml.Marshal(rawBackup.Config)
if err != nil {
return nil, fmt.Errorf("new config: remarshal: %w", err)
}
// Re-marshal config
err = yaml.Unmarshal(configBytes, typedConfig)
if err != nil {
return nil, fmt.Errorf("new config: remarshal: %w", err)
}
config.Backups = append(config.Backups, Backup{
Name: rawBackup.Name,
Type: rawBackup.Type,
Config: typedConfig,
})
}
return &config, nil
}
// ReadBackup find Backup struct with given name and returns it.
// Return ErrConfigBackupNameNotFound error if no matching name found in config
func (c *Config) ReadBackup(name string) (Backup, error) {
for _, backup := range c.Backups {
if backup.Name == name {
return backup, nil
}
}
return Backup{}, ErrConfigBackupNameNotFound
}
func (c *Config) BackupNames() []string {
names := []string{}
for _, backup := range c.Backups {
names = append(names, backup.Name)
}
return names
}
func (c *Config) IsValidName(name string) bool {
for _, backup := range c.Backups {
if backup.Name == name {
return true
}
}
return false
}
func (c *Config) CreateRepositoryStruct(bConf BackupTypeConfig) (ResticRepository, error) {
switch v := bConf.(type) {
case *LocalBackupConfig:
return LocalBackupRepository{
Password: c.Repository.Password,
Destination: v.Destination,
Sources: v.Sources,
Excludes: v.Excludes,
}, nil
case *S3BackupConfig:
return S3BackupRepository{
Password: c.Repository.Password,
Destination: v.Destination,
Sources: v.Sources,
Excludes: v.Excludes,
AccessKeyId: v.AccessKeyId,
SecretAccessKey: v.SecretAccessKey,
}, nil
case *SftpBackupConfig:
return SftpBackupRepository{
Password: c.Repository.Password,
Destination: v.Destination,
Sources: v.Sources,
Excludes: v.Excludes,
ConfigHost: v.Host,
}, nil
default:
fmt.Printf("type of bConf: %T\n", v)
return nil, errors.New("no matched concrete type")
}
}
func (c *Config) CreateTestStruct(bConf BackupTypeConfig) (tests.ResticTest, error) {
switch v := bConf.(type) {
case *LocalBackupConfig:
return tests.LocalRepositoryTest{
Password: c.Repository.Password,
Destination: v.Destination,
Sources: v.Sources,
}, nil
default:
fmt.Printf("type of bConf: %T\n", v)
return nil, errors.New("no matched concrete type")
}
}
// type Config struct {
// Repository struct {
// Password string `yaml:"password"`
// } `yaml:"repository"`
// Backups []struct {
// Name string `yaml:"name"`
// Type string `yaml:"type"`
// Host string `yaml:"host,omitempty"`
// Sources []string `yaml:"sources"`
// Destination string `yaml:"destination"`
// AccessKeyId string `yaml:"accessKeyId,omitempty"`
// SecretKey string `yaml:"secretAccessKey,omitempty"`
// Region string `yaml:"region,omitempty"`
// } `yaml:"backups"`
// }
// // NewConfig load data into struct from given config file
// func NewConfig(filepath string) (*Config, error) {
// data, err := os.ReadFile(filepath)
// if err != nil {
// return nil, ErrConfigNotFound
// }
// var config Config
// if err := yaml.Unmarshal(data, &config); err != nil {
// return nil, fmt.Errorf("new config: %w", err)
// }
// return &config, nil
// }