forked from leeor/hammock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammock.go
52 lines (33 loc) · 1.02 KB
/
hammock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hammock
import (
"fmt"
"github.com/mikebell-org/go-couchdb"
)
type CouchDB struct {
couchdb.CouchDB
}
func Database(host, database, username, password string) (*CouchDB, error) {
db, err := couchdb.Database(host, database, username, password)
return &CouchDB{*db}, err
}
func Sync(db *CouchDB, path string) (changes []string, err error) {
// TODO: implement a document freezing option
disk_data := newDesignDocCollection()
if err = disk_data.loadFromDisk(path); err == nil {
for doc_name, document := range disk_data.Documents {
db_data := newDesignDocument(doc_name)
if err = db.GetDocument(&db_data, fmt.Sprintf("%v", doc_name)); err != nil {
changes = append(changes, fmt.Sprintf("Design document %v is missing", doc_name))
err = nil
}
if updated, doc_changes := db_data.update(document); updated {
changes = append(changes, doc_changes...)
if success, e := db.PutDocument(db_data, doc_name); e != nil || !success.OK {
err = e
return
}
}
}
}
return
}