diff options
Diffstat (limited to 'yamldb')
-rw-r--r-- | yamldb/yamldb.go | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/yamldb/yamldb.go b/yamldb/yamldb.go index eec23be..1478cc7 100644 --- a/yamldb/yamldb.go +++ b/yamldb/yamldb.go @@ -18,9 +18,7 @@ type YamlDB struct { // 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 - +func (db *YamlDB) Transaction(callback func() bool, marshaller func() ([]byte, error)) error { log.Debug("Enter transaction") db.lock.Lock() defer func() { @@ -28,19 +26,21 @@ func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error) 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") + isDataChanged := callback() + + if isDataChanged { + 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 |