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 (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
2016-03-31 16:59:48 -05:00
|
|
|
|
|
|
|
type release struct {
|
|
|
|
When time.Time `json:"when"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
FileKey TimeKey `json:"file"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketReleases = "releases"
|
|
|
|
bucketFiles = "files"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddRelease adds a new Release
|
|
|
|
func (ds *Store) AddRelease(version string, fileData []byte) error {
|
|
|
|
key := NewTimeKey()
|
|
|
|
fileKey := NewTimeKey()
|
|
|
|
|
|
|
|
r := &release{
|
|
|
|
When: key.Time(),
|
|
|
|
Version: version,
|
|
|
|
FileKey: fileKey,
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
err = tx.Bucket([]byte(bucketReleases)).Put(key.Bytes(), dsValue)
|
|
|
|
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
|
|
|
}
|