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-04-01 14:30:17 -05:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
2016-03-31 16:59:48 -05:00
|
|
|
|
|
|
|
type log struct {
|
|
|
|
When time.Time `json:"when"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Stage string `json:"stage"`
|
|
|
|
Log string `json:"log"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const bucketLog = "log"
|
|
|
|
|
|
|
|
// AddLog adds a new log entry
|
|
|
|
func (ds *Store) AddLog(version, stage, entry string) error {
|
|
|
|
key := NewTimeKey()
|
|
|
|
|
|
|
|
data := &log{
|
|
|
|
When: key.Time(),
|
|
|
|
Version: version,
|
|
|
|
Stage: stage,
|
|
|
|
Log: entry,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds.put(bucketLog, key, data)
|
|
|
|
}
|
2016-04-01 14:30:17 -05:00
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
// LatestVersion returns the latest version (successful or otherwise) for the current project
|
2016-04-01 14:30:17 -05:00
|
|
|
func (ds *Store) LatestVersion() (string, error) {
|
|
|
|
version := ""
|
|
|
|
|
|
|
|
err := ds.bolt.View(func(tx *bolt.Tx) error {
|
|
|
|
c := tx.Bucket([]byte(bucketLog)).Cursor()
|
|
|
|
|
2016-04-05 21:08:08 -05:00
|
|
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
2016-04-01 14:30:17 -05:00
|
|
|
l := &log{}
|
|
|
|
err := json.Unmarshal(v, l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Version != "" {
|
|
|
|
version = l.Version
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrNotFound
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-04-05 21:08:08 -05:00
|
|
|
|
2016-04-01 14:30:17 -05:00
|
|
|
return version, nil
|
|
|
|
}
|