-
Notifications
You must be signed in to change notification settings - Fork 14
/
artifact.go
61 lines (49 loc) · 1.36 KB
/
artifact.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
package versioned
import (
"encoding/json"
"fmt"
"io/ioutil"
)
//go:generate counterfeiter -o fakes/fake_artifact.go . Artifact
type Artifact interface {
Save(backup map[string]BucketSnapshot) error
Load() (map[string]BucketSnapshot, error)
}
type BucketSnapshot struct {
BucketName string `json:"bucket_name"`
RegionName string `json:"region_name"`
Versions []BlobVersion `json:"versions"`
}
type BlobVersion struct {
BlobKey string `json:"blob_key"`
Id string `json:"version_id"`
}
type FileArtifact struct {
filePath string
}
func NewFileArtifact(filePath string) FileArtifact {
return FileArtifact{filePath: filePath}
}
func (a FileArtifact) Save(backup map[string]BucketSnapshot) error {
marshalledBackup, err := json.MarshalIndent(backup, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(a.filePath, marshalledBackup, 0666)
if err != nil {
return fmt.Errorf("could not write backup file: %s", err.Error())
}
return nil
}
func (a FileArtifact) Load() (map[string]BucketSnapshot, error) {
bytes, err := ioutil.ReadFile(a.filePath)
if err != nil {
return nil, fmt.Errorf("could not read backup file: %s", err.Error())
}
var backup map[string]BucketSnapshot
err = json.Unmarshal(bytes, &backup)
if err != nil {
return nil, fmt.Errorf("backup file has an invalid format: %s", err.Error())
}
return backup, nil
}