forked from hashicorp/otto
/
backend.go
95 lines (80 loc) · 2.59 KB
/
backend.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
package directory
import (
"io"
"os"
)
// Backend is the interface for any directory service. It is effectively
// the protocol right now. In the future we may abstract this further to
// an official protocol if it proves to be necessary. Until then, it is
// a value add on top of the Appfile (but not part of that format) that Otto
// uses for global state.
type Backend interface {
// PutBlob writes binary data for a given project/infra/app.
//
// GetBlob reads that data back out.
//
// ListBlob lists the binary data stored.
PutBlob(string, *BlobData) error
GetBlob(string) (*BlobData, error)
// PutInfra and GetInfra are the functions used to store and retrieve
// data about infrastructures.
PutInfra(*Infra) error
GetInfra(*Infra) (*Infra, error)
// PutDev stores the result of a dev.
//
// GetDev queries a dev. The result is returned. The parameter
// must fill in the App, Infra, and InfraFlavor fields.
PutDev(*Dev) error
GetDev(*Dev) (*Dev, error)
DeleteDev(*Dev) error
// PutBuild stores the result of a build.
//
// GetBuild queries a build. The result is returned. The parameter
// must fill in the App, Infra, and InfraFlavor fields.
PutBuild(*Build) error
GetBuild(*Build) (*Build, error)
// PutDeploy stores the result of a build.
//
// GetDeploy queries a deploy. The result is returned. The parameter
// must fill in the App, Infra, and InfraFlavor fields.
PutDeploy(*Deploy) error
GetDeploy(*Deploy) (*Deploy, error)
}
// Build represents a build of an App.
type Build struct {
// Lookup information for the Build. AppID, Infra, and InfraFlavor
// are required.
Lookup
// Resulting artifact from the build
Artifact map[string]string
}
// BlobData is the metadata and data associated with stored binary
// data. The fields and their usage varies depending on the operations,
// so please read the documentation for each field carefully.
type BlobData struct {
// Key is the key for the blob data. This is populated on read and ignored
// on any other operation.
Key string
// Data is the data for the blob data. When writing, this should be
// the data to write. When reading, this is the data that is read.
Data io.Reader
closer io.Closer
}
func (d *BlobData) Close() error {
if d.closer != nil {
return d.closer.Close()
}
return nil
}
// WriteToFile is a helper to write BlobData to a file. While this is
// a very easy thing to do, it is so common that we provide a function
// for doing so.
func (d *BlobData) WriteToFile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, d.Data)
return err
}