github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/directory/backend.go (about) 1 package directory 2 3 import ( 4 "io" 5 "os" 6 ) 7 8 // Backend is the interface for any directory service. It is effectively 9 // the protocol right now. In the future we may abstract this further to 10 // an official protocol if it proves to be necessary. Until then, it is 11 // a value add on top of the Appfile (but not part of that format) that Otto 12 // uses for global state. 13 type Backend interface { 14 // PutBlob writes binary data for a given project/infra/app. 15 // 16 // GetBlob reads that data back out. 17 // 18 // ListBlob lists the binary data stored. 19 PutBlob(string, *BlobData) error 20 GetBlob(string) (*BlobData, error) 21 22 // PutInfra and GetInfra are the functions used to store and retrieve 23 // data about infrastructures. 24 PutInfra(*Infra) error 25 GetInfra(*Infra) (*Infra, error) 26 27 // PutDev stores the result of a dev. 28 // 29 // GetDev queries a dev. The result is returned. The parameter 30 // must fill in the App, Infra, and InfraFlavor fields. 31 PutDev(*Dev) error 32 GetDev(*Dev) (*Dev, error) 33 DeleteDev(*Dev) error 34 35 // PutBuild stores the result of a build. 36 // 37 // GetBuild queries a build. The result is returned. The parameter 38 // must fill in the App, Infra, and InfraFlavor fields. 39 PutBuild(*Build) error 40 GetBuild(*Build) (*Build, error) 41 42 // PutDeploy stores the result of a build. 43 // 44 // GetDeploy queries a deploy. The result is returned. The parameter 45 // must fill in the App, Infra, and InfraFlavor fields. 46 PutDeploy(*Deploy) error 47 GetDeploy(*Deploy) (*Deploy, error) 48 } 49 50 // Build represents a build of an App. 51 type Build struct { 52 // Lookup information for the Build. AppID, Infra, and InfraFlavor 53 // are required. 54 Lookup 55 56 // Resulting artifact from the build 57 Artifact map[string]string 58 } 59 60 // BlobData is the metadata and data associated with stored binary 61 // data. The fields and their usage varies depending on the operations, 62 // so please read the documentation for each field carefully. 63 type BlobData struct { 64 // Key is the key for the blob data. This is populated on read and ignored 65 // on any other operation. 66 Key string 67 68 // Data is the data for the blob data. When writing, this should be 69 // the data to write. When reading, this is the data that is read. 70 Data io.Reader 71 72 closer io.Closer 73 } 74 75 func (d *BlobData) Close() error { 76 if d.closer != nil { 77 return d.closer.Close() 78 } 79 80 return nil 81 } 82 83 // WriteToFile is a helper to write BlobData to a file. While this is 84 // a very easy thing to do, it is so common that we provide a function 85 // for doing so. 86 func (d *BlobData) WriteToFile(path string) error { 87 f, err := os.Create(path) 88 if err != nil { 89 return err 90 } 91 defer f.Close() 92 93 _, err = io.Copy(f, d.Data) 94 return err 95 }