Finished log web endpoints
This commit is contained in:
@ -15,8 +15,10 @@ import (
|
||||
"github.com/boltdb/bolt"
|
||||
)
|
||||
|
||||
//TODO: Move this all over to GobStore if I ever get around to finishing it
|
||||
|
||||
// ErrNotFound is the error returned when a value cannot be found in the store for the given key
|
||||
var ErrNotFound = errors.New("Value not found")
|
||||
var ErrNotFound = errors.New("Value not found in datastore")
|
||||
|
||||
// Store is a datastore for getting and setting data for a given ironsmith project
|
||||
// run on top of a Bolt DB file
|
||||
|
@ -58,7 +58,7 @@ func (ds *Store) LastVersion(stage string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return ErrNotFound
|
||||
return nil // not found return blank
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -102,3 +102,79 @@ func (ds *Store) Versions() ([]*Log, error) {
|
||||
|
||||
return vers, nil
|
||||
}
|
||||
|
||||
// VersionLog returns all the log entries for a given version
|
||||
func (ds *Store) VersionLog(version string) ([]*Log, error) {
|
||||
var logs []*Log
|
||||
|
||||
if version == "" {
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
verFound := false
|
||||
|
||||
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
||||
c := tx.Bucket([]byte(bucketLog)).Cursor()
|
||||
|
||||
for k, v := c.Last(); k != nil; k, v = c.Prev() {
|
||||
l := &Log{}
|
||||
err := json.Unmarshal(v, l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if verFound && l.Version != version {
|
||||
return nil
|
||||
}
|
||||
|
||||
if l.Version == version {
|
||||
logs = append(logs, l)
|
||||
verFound = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
// StageLog returns the log entry for a given version + stage
|
||||
func (ds *Store) StageLog(version, stage string) (*Log, error) {
|
||||
var entry *Log
|
||||
|
||||
if version == "" || stage == "" {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
||||
c := tx.Bucket([]byte(bucketLog)).Cursor()
|
||||
|
||||
for k, v := c.Last(); k != nil; k, v = c.Prev() {
|
||||
l := &Log{}
|
||||
err := json.Unmarshal(v, l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Version == version && l.Stage == stage {
|
||||
entry = l
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ErrNotFound
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
@ -12,9 +12,10 @@ import (
|
||||
)
|
||||
|
||||
type release struct {
|
||||
When time.Time `json:"when"`
|
||||
Version string `json:"version"`
|
||||
FileKey TimeKey `json:"file"`
|
||||
When time.Time `json:"when"`
|
||||
Version string `json:"version"`
|
||||
FileName string `json:"fileName"`
|
||||
FileKey TimeKey `json:"fileKey"`
|
||||
}
|
||||
|
||||
const (
|
||||
@ -23,14 +24,14 @@ const (
|
||||
)
|
||||
|
||||
// AddRelease adds a new Release
|
||||
func (ds *Store) AddRelease(version string, fileData []byte) error {
|
||||
key := NewTimeKey()
|
||||
func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
|
||||
fileKey := NewTimeKey()
|
||||
|
||||
r := &release{
|
||||
When: key.Time(),
|
||||
Version: version,
|
||||
FileKey: fileKey,
|
||||
When: fileKey.Time(),
|
||||
Version: version,
|
||||
FileName: fileName,
|
||||
FileKey: fileKey,
|
||||
}
|
||||
|
||||
dsValue, err := json.Marshal(r)
|
||||
@ -39,7 +40,7 @@ func (ds *Store) AddRelease(version string, fileData []byte) error {
|
||||
}
|
||||
|
||||
return ds.bolt.Update(func(tx *bolt.Tx) error {
|
||||
err = tx.Bucket([]byte(bucketReleases)).Put(key.Bytes(), dsValue)
|
||||
err = tx.Bucket([]byte(bucketReleases)).Put([]byte(version), dsValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -47,3 +48,42 @@ func (ds *Store) AddRelease(version string, fileData []byte) error {
|
||||
return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData)
|
||||
})
|
||||
}
|
||||
|
||||
func (ds *Store) Release(version string) {
|
||||
|
||||
}
|
||||
|
||||
// Releases lists all the releases in a given project
|
||||
func (ds *Store) Releases() ([]*Log, error) {
|
||||
var vers []*Log
|
||||
|
||||
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
||||
c := tx.Bucket([]byte(bucketLog)).Cursor()
|
||||
|
||||
var current = ""
|
||||
|
||||
for k, v := c.Last(); k != nil; k, v = c.Prev() {
|
||||
l := &Log{}
|
||||
err := json.Unmarshal(v, l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// capture the newest entry for each version
|
||||
if l.Version != current {
|
||||
l.Log = "" // only care about date, ver and stage
|
||||
vers = append(vers, l)
|
||||
current = l.Version
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return vers, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user