128 lines
2.5 KiB
Go
128 lines
2.5 KiB
Go
// 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
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// Release is a record of the fully built and ready to deploy release file
|
|
type Release struct {
|
|
When time.Time `json:"when"`
|
|
Version string `json:"version"`
|
|
FileName string `json:"fileName"`
|
|
FileKey TimeKey `json:"fileKey"`
|
|
}
|
|
|
|
const (
|
|
bucketReleases = "releases"
|
|
bucketFiles = "files"
|
|
)
|
|
|
|
// AddRelease adds a new Release
|
|
func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
|
|
fileKey := NewTimeKey()
|
|
|
|
r := &Release{
|
|
When: fileKey.Time(),
|
|
Version: version,
|
|
FileName: fileName,
|
|
FileKey: fileKey,
|
|
}
|
|
|
|
dsValue, err := json.Marshal(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return ds.bolt.Update(func(tx *bolt.Tx) error {
|
|
err = tx.Bucket([]byte(bucketReleases)).Put([]byte(version), dsValue)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData)
|
|
})
|
|
}
|
|
|
|
// ReleaseFile returns a specific file from a release for the given file key
|
|
func (ds *Store) ReleaseFile(fileKey TimeKey) ([]byte, error) {
|
|
var fileData []byte
|
|
|
|
err := ds.get(bucketFiles, fileKey.Bytes(), &fileData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fileData, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// Releases lists all the releases in a given project
|
|
func (ds *Store) Releases() ([]*Release, error) {
|
|
var vers []*Release
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket([]byte(bucketReleases)).Cursor()
|
|
|
|
for k, v := c.Last(); k != nil; k, v = c.Prev() {
|
|
r := &Release{}
|
|
err := json.Unmarshal(v, r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
vers = append(vers, r)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return vers, nil
|
|
}
|
|
|
|
// LastRelease lists the last release for a project
|
|
func (ds *Store) LastRelease() (*Release, error) {
|
|
var r *Release
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket([]byte(bucketReleases)).Cursor()
|
|
|
|
_, v := c.Last()
|
|
|
|
err := json.Unmarshal(v, r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if r == nil {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
return r, nil
|
|
}
|