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

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Abd al-Rahman committed Oct 30, 2016
0 parents commit 704a013
Show file tree
Hide file tree
Showing 223 changed files with 119,304 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
odoobup
16 changes: 16 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Muhammad Abd al-Rahman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
168 changes: 168 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) MarshalBinary() (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) UnmarshalBinary(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.MarshalBinary()
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.UnmarshalBinary(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.UnmarshalBinary(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)
}
Loading

0 comments on commit 704a013

Please sign in to comment.