Skip to content
This repository was archived by the owner on Jun 14, 2018. It is now read-only.

Commit 704a013

Browse files
author
Muhammad Abd al-Rahman
committed
first commit
0 parents  commit 704a013

File tree

223 files changed

+119304
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+119304
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
odoobup

Godeps/Godeps.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/Readme

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Muhammad Abd al-Rahman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

db.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/gob"
6+
"errors"
7+
"fmt"
8+
"log"
9+
"os"
10+
"strconv"
11+
12+
"github.com/boltdb/bolt"
13+
)
14+
15+
var db = dbSetup()
16+
17+
type (
18+
Config struct {
19+
ID int
20+
Info ConfigInfo
21+
}
22+
23+
ConfigInfo struct {
24+
URL string
25+
DBName string
26+
OdooPass string // Odoo Master Password
27+
BackupDir string
28+
Version float64
29+
}
30+
)
31+
32+
func dbSetup() *bolt.DB {
33+
dbDir := os.Getenv("HOME") + "/.odoobup"
34+
dbPath := dbDir + "/config.db"
35+
36+
//check if .odoobup directory not found
37+
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
38+
err := os.Mkdir(dbDir, 0777)
39+
if err != nil {
40+
log.Fatalln(err)
41+
}
42+
}
43+
44+
//create and open a database
45+
db, err := bolt.Open(dbPath, 0777, nil)
46+
if err != nil {
47+
log.Fatalln(err)
48+
}
49+
50+
//create config bucket
51+
db.Update(func(tx *bolt.Tx) error {
52+
_, err := tx.CreateBucketIfNotExists([]byte("config"))
53+
if err != nil {
54+
log.Fatalln(err)
55+
}
56+
return nil
57+
})
58+
59+
return db
60+
}
61+
62+
func (c *Config) MarshalBinary() (data []byte, err error) {
63+
w := new(bytes.Buffer)
64+
encoder := gob.NewEncoder(w)
65+
err = encoder.Encode(c.Info)
66+
if err != nil {
67+
return nil, err
68+
}
69+
return w.Bytes(), nil
70+
}
71+
72+
func (c *Config) UnmarshalBinary(data []byte) error {
73+
r := bytes.NewBuffer(data)
74+
decoder := gob.NewDecoder(r)
75+
return decoder.Decode(&c.Info)
76+
}
77+
78+
func NewConfig(ci *ConfigInfo) (*Config, error) {
79+
var c Config
80+
81+
err := db.Update(func(tx *bolt.Tx) error {
82+
bkt := tx.Bucket([]byte("config"))
83+
84+
seq, _ := bkt.NextSequence()
85+
c.ID = int(seq)
86+
c.Info = *ci
87+
88+
encoding, err := c.MarshalBinary()
89+
if err != nil {
90+
return err
91+
}
92+
93+
if err := bkt.Put(itob(c.ID), encoding); err != nil {
94+
return err
95+
}
96+
97+
return nil
98+
})
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return &c, nil
104+
}
105+
106+
func ConfigByID(id int) (*Config, error) {
107+
var config Config
108+
109+
err := db.View(func(tx *bolt.Tx) error {
110+
v := tx.Bucket([]byte("config")).Get(itob(id))
111+
if v == nil {
112+
idStr := fmt.Sprint(id)
113+
return errors.New("The ID " + idStr + " was not Found")
114+
}
115+
116+
if err := config.UnmarshalBinary(v); err != nil {
117+
return err
118+
}
119+
config.ID = id
120+
121+
return nil
122+
})
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
return &config, nil
128+
}
129+
130+
func AllConfig() ([]Config, error) {
131+
var config Config
132+
var allConfig []Config
133+
134+
err := db.View(func(tx *bolt.Tx) error {
135+
if err := tx.Bucket([]byte("config")).ForEach(func(k, v []byte) error {
136+
config.ID, _ = strconv.Atoi(string(k))
137+
if err := config.UnmarshalBinary(v); err != nil {
138+
return err
139+
}
140+
141+
allConfig = append(allConfig, config)
142+
143+
return nil
144+
145+
}); err != nil {
146+
return err
147+
}
148+
149+
return nil
150+
151+
})
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
return allConfig, nil
157+
}
158+
159+
func DeleteConfig(id int) error {
160+
return db.Update(func(tx *bolt.Tx) error {
161+
return tx.Bucket([]byte("config")).Delete(itob(id))
162+
})
163+
}
164+
165+
func itob(v int) []byte {
166+
id := fmt.Sprintf("%08d", v)
167+
return []byte(id)
168+
}

0 commit comments

Comments
 (0)