rpi-vsts-ci-led/pkg/database/database.go

44 lines
560 B
Go
Raw Normal View History

2020-04-17 08:41:48 +00:00
package database
import (
"errors"
"os"
"path"
"git.trj.tw/golang/utils"
"go.etcd.io/bbolt"
)
var db *bbolt.DB
func New(dbPath ...string) error {
var err error
var fp string
if len(dbPath) > 0 && dbPath[0] != "" {
fp = dbPath[0]
} else {
wd, err := os.Getwd()
if err != nil {
return err
}
fp = path.Join(wd, "store.db")
}
fp = utils.ParsePath(fp)
db, err = bbolt.Open(fp, 0664, nil)
if err != nil {
return err
}
return nil
}
func Get() *bbolt.DB {
if db == nil {
panic(errors.New("database not init"))
}
return db
}