ironsmith/datastore/releases.go

143 lines
2.7 KiB
Go
Raw Permalink Normal View History

// 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 (
"bytes"
2016-03-31 22:32:56 -05:00
"encoding/json"
"io"
2016-03-31 22:32:56 -05:00
"time"
"github.com/boltdb/bolt"
)
// 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"`
}
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 {
fileKey := NewTimeKey()
r := &Release{
2016-04-07 09:49:54 -05:00
When: fileKey.Time(),
Version: version,
FileName: fileName,
FileKey: fileKey,
}
2016-03-31 22:32:56 -05:00
dsValue, err := json.Marshal(r)
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 22:32:56 -05:00
return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData)
})
}
2016-04-07 09:49:54 -05:00
// ReleaseFile returns a specific file from a release for the given file key
func (ds *Store) ReleaseFile(fileKey TimeKey) ([]byte, error) {
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
})
if err != nil {
return nil, err
}
return fileData.Bytes(), 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
2016-04-07 09:49:54 -05:00
}
// Releases lists all the releases in a given project
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 {
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() {
r := &Release{}
err := json.Unmarshal(v, r)
2016-04-07 09:49:54 -05:00
if err != nil {
return err
}
vers = append(vers, r)
2016-04-07 09:49:54 -05:00
}
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) {
r := &Release{}
err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketReleases)).Cursor()
_, v := c.Last()
if v == nil {
return ErrNotFound
}
err := json.Unmarshal(v, r)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return r, nil
}