-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_spec.go
More file actions
54 lines (45 loc) · 1.45 KB
/
config_spec.go
File metadata and controls
54 lines (45 loc) · 1.45 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
package backr
import (
"crypto/md5"
"errors"
"fmt"
"strconv"
)
// ProjectBackupSpec represents the content of a backup.yml file
type ProjectBackupSpec struct {
Name string `yaml:"name"`
Backups []BackupSpec
Archiver *Archiver `yaml:"archiver"`
}
// BackupSpec represents a backup specification
type BackupSpec struct {
MinAge int `yaml:"min_age"`
PeriodUnit int `yaml:"period_unit"` // unit for 'min_age', in hours (default to 24h)
IgnoreStartupTime bool `yaml:"ignore_startup_time"`
}
type Archiver struct {
Type string `yaml:"type"`
OutputFileExtension string `yaml:"ext"`
Command []string `yaml:"command"`
}
// GetChecksum returns a hash of the backup allowing to detect changes
func (b BackupSpec) GetChecksum() string {
data := []byte(strconv.Itoa(b.PeriodUnit) + strconv.Itoa(b.MinAge) + strconv.FormatBool(b.IgnoreStartupTime))
return fmt.Sprintf("%x", md5.Sum(data))
}
// IsValid returns a boolean indicating if the parsed backup.yml is valid
func (b ProjectBackupSpec) IsValid() error {
if b.Name == "" {
return errors.New("'name' is required")
}
if b.Archiver != nil {
archiver := *b.Archiver
if len(archiver.Command) == 0 || archiver.Type != "pliz" && archiver.Type != "stdout" {
return errors.New("'archiver' type must be 'pliz' or 'stdout', 'command' and 'ext' are required")
}
}
if len(b.Backups) == 0 {
return errors.New("'backups' cannot be empty")
}
return nil
}