This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
168 lines (134 loc) · 2.74 KB
/
db.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"log"
"os"
"strconv"
"github.com/boltdb/bolt"
)
var db = dbSetup()
type (
Config struct {
ID int
Info ConfigInfo
}
ConfigInfo struct {
URL string
DBName string
OdooPass string // Odoo Master Password
BackupDir string
Version float64
}
)
func dbSetup() *bolt.DB {
dbDir := os.Getenv("HOME") + "/.odoobup"
dbPath := dbDir + "/config.db"
//check if .odoobup directory not found
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
err := os.Mkdir(dbDir, 0777)
if err != nil {
log.Fatalln(err)
}
}
//create and open a database
db, err := bolt.Open(dbPath, 0777, nil)
if err != nil {
log.Fatalln(err)
}
//create config bucket
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("config"))
if err != nil {
log.Fatalln(err)
}
return nil
})
return db
}
func (c *Config) Encode() (data []byte, err error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
err = encoder.Encode(c.Info)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (c *Config) Decode(data []byte) error {
r := bytes.NewBuffer(data)
decoder := gob.NewDecoder(r)
return decoder.Decode(&c.Info)
}
func NewConfig(ci *ConfigInfo) (*Config, error) {
var c Config
err := db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte("config"))
seq, _ := bkt.NextSequence()
c.ID = int(seq)
c.Info = *ci
encoding, err := c.Encode()
if err != nil {
return err
}
if err := bkt.Put(itob(c.ID), encoding); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return &c, nil
}
func ConfigByID(id int) (*Config, error) {
var config Config
err := db.View(func(tx *bolt.Tx) error {
v := tx.Bucket([]byte("config")).Get(itob(id))
if v == nil {
idStr := fmt.Sprint(id)
return errors.New("The ID " + idStr + " was not Found")
}
if err := config.Decode(v); err != nil {
return err
}
config.ID = id
return nil
})
if err != nil {
return nil, err
}
return &config, nil
}
func AllConfig() ([]Config, error) {
var config Config
var allConfig []Config
err := db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket([]byte("config")).ForEach(func(k, v []byte) error {
config.ID, _ = strconv.Atoi(string(k))
if err := config.Decode(v); err != nil {
return err
}
allConfig = append(allConfig, config)
return nil
}); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return allConfig, nil
}
func DeleteConfig(id int) error {
return db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte("config")).Delete(itob(id))
})
}
func itob(v int) []byte {
id := fmt.Sprintf("%08d", v)
return []byte(id)
}