You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
telegabber/yamldb/yamldb.go

48 lines
961 B

package yamldb
import (
"github.com/pkg/errors"
"io/ioutil"
"os"
"sync"
log "github.com/sirupsen/logrus"
)
// YamlDB represents a YAML file database instance
type YamlDB struct {
Path string
PathNew string
lock sync.Mutex
}
// Transaction executes the given callback and safely saves
// the data after they are modified within the callback
func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)) error {
var err error
log.Debug("Enter transaction")
db.lock.Lock()
defer func() {
db.lock.Unlock()
log.Debug("Exit transaction")
}()
callback()
yamlData, err := marshaller()
if err != nil {
return errors.Wrap(err, "Data marshalling error")
}
err = ioutil.WriteFile(db.PathNew, yamlData, 0644)
if err != nil {
return errors.Wrap(err, "YamlDB write failure")
}
err = os.Rename(db.PathNew, db.Path)
if err != nil {
return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
}
return nil
}