2016-03-31 16:59:48 -05:00
|
|
|
// Copyright 2016 Tim Shannon. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package datastore
|
|
|
|
|
2016-03-31 22:32:56 -05:00
|
|
|
import (
|
2016-04-18 15:31:22 -05:00
|
|
|
"bytes"
|
2016-03-31 22:32:56 -05:00
|
|
|
"encoding/json"
|
2016-04-18 15:31:22 -05:00
|
|
|
"io"
|
2016-03-31 22:32:56 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
2016-03-31 16:59:48 -05:00
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
// Release is a record of the fully built and ready to deploy release file
|
|
|
|
type Release struct {
|
2016-04-07 09:49:54 -05:00
|
|
|
When time.Time `json:"when"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
FileName string `json:"fileName"`
|
|
|
|
FileKey TimeKey `json:"fileKey"`
|
2016-03-31 16:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketReleases = "releases"
|
|
|
|
bucketFiles = "files"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddRelease adds a new Release
|
2016-04-07 09:49:54 -05:00
|
|
|
func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
|
2016-03-31 16:59:48 -05:00
|
|
|
fileKey := NewTimeKey()
|
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
r := &Release{
|
2016-04-07 09:49:54 -05:00
|
|
|
When: fileKey.Time(),
|
|
|
|
Version: version,
|
|
|
|
FileName: fileName,
|
|
|
|
FileKey: fileKey,
|
2016-03-31 16:59:48 -05:00
|
|
|
}
|
|
|
|
|
2016-03-31 22:32:56 -05:00
|
|
|
dsValue, err := json.Marshal(r)
|
2016-03-31 16:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:32:56 -05:00
|
|
|
return ds.bolt.Update(func(tx *bolt.Tx) error {
|
2016-04-07 09:49:54 -05:00
|
|
|
err = tx.Bucket([]byte(bucketReleases)).Put([]byte(version), dsValue)
|
2016-03-31 22:32:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-31 16:59:48 -05:00
|
|
|
|
2016-03-31 22:32:56 -05:00
|
|
|
return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData)
|
|
|
|
})
|
2016-03-31 16:59:48 -05:00
|
|
|
}
|
2016-04-07 09:49:54 -05:00
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
// ReleaseFile returns a specific file from a release for the given file key
|
|
|
|
func (ds *Store) ReleaseFile(fileKey TimeKey) ([]byte, error) {
|
2016-04-18 15:31:22 -05:00
|
|
|
var fileData bytes.Buffer
|
|
|
|
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
|
|
|
dsValue := tx.Bucket([]byte(bucketFiles)).Get(fileKey.Bytes())
|
|
|
|
|
|
|
|
if dsValue == nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := io.Copy(&fileData, bytes.NewReader(dsValue))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-13 15:53:49 -05:00
|
|
|
|
2016-04-18 15:31:22 -05:00
|
|
|
return fileData.Bytes(), nil
|
2016-04-13 11:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release gets the release record for a specific version
|
|
|
|
func (ds *Store) Release(version string) (*Release, error) {
|
|
|
|
r := &Release{}
|
|
|
|
err := ds.get(bucketReleases, []byte(version), r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
2016-04-07 09:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Releases lists all the releases in a given project
|
2016-04-13 11:29:17 -05:00
|
|
|
func (ds *Store) Releases() ([]*Release, error) {
|
|
|
|
var vers []*Release
|
2016-04-07 09:49:54 -05:00
|
|
|
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
2016-04-13 11:29:17 -05:00
|
|
|
c := tx.Bucket([]byte(bucketReleases)).Cursor()
|
2016-04-07 09:49:54 -05:00
|
|
|
|
|
|
|
for k, v := c.Last(); k != nil; k, v = c.Prev() {
|
2016-04-13 11:29:17 -05:00
|
|
|
r := &Release{}
|
|
|
|
err := json.Unmarshal(v, r)
|
2016-04-07 09:49:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
vers = append(vers, r)
|
2016-04-07 09:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return vers, nil
|
|
|
|
}
|
2016-04-13 11:29:17 -05:00
|
|
|
|
|
|
|
// LastRelease lists the last release for a project
|
|
|
|
func (ds *Store) LastRelease() (*Release, error) {
|
2016-04-13 15:53:49 -05:00
|
|
|
r := &Release{}
|
2016-04-13 11:29:17 -05:00
|
|
|
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
|
|
|
c := tx.Bucket([]byte(bucketReleases)).Cursor()
|
|
|
|
|
2016-04-25 16:39:11 -05:00
|
|
|
_, v := c.First() // this is confusing
|
2016-04-13 15:53:49 -05:00
|
|
|
if v == nil {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2016-04-13 11:29:17 -05:00
|
|
|
|
|
|
|
err := json.Unmarshal(v, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|