-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
414 additions
and
694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
package config | ||
|
||
const defaultDBFilename = "ma.db" | ||
import "github.com/spf13/viper" | ||
|
||
var DefaultDbFile = NormalisePath(dataHome + defaultDBFilename) | ||
const defaultDBDirname = ".madb" | ||
|
||
var DefaultDbPath = NormalisePath(dataHome + defaultDBDirname) | ||
|
||
func DBPath() string { | ||
return viper.GetString("db.path") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/bahner/go-ma-actor/config" | ||
badger "github.com/dgraph-io/badger/v3" | ||
"github.com/mitchellh/go-homedir" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func init() { | ||
|
||
pflag.String("db", config.DefaultDbPath, "File for sqlite database.") | ||
|
||
viper.BindPFlag("db.path", pflag.Lookup("db")) | ||
|
||
} | ||
|
||
var ( | ||
_db *badger.DB | ||
once sync.Once | ||
) | ||
|
||
// InitDB initializes the Badger database and sets the global `db` variable. | ||
// It uses `sync.Once` to ensure that the database is opened only once. | ||
func Init() (db *badger.DB, err error) { | ||
|
||
once.Do(func() { | ||
|
||
var dbPath string | ||
|
||
dbPath, err = dbDir() | ||
if err != nil { | ||
return | ||
} | ||
|
||
db, err = badger.Open(badger.DefaultOptions(dbPath)) | ||
if err != nil { | ||
return | ||
} | ||
}) | ||
|
||
return db, nil | ||
} | ||
|
||
// CloseDB closes the Badger database. This should be called when your application exits. | ||
func Close() (err error) { | ||
if _db != nil { | ||
err = _db.Close() | ||
if err != nil { | ||
return fmt.Errorf("failed to close BadgerDB: %v", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func DB() *badger.DB { | ||
return _db | ||
} | ||
|
||
// Returns expanded path to the dbDir file | ||
// If the expansion fails it returns an empty string | ||
func dbDir() (string, error) { | ||
|
||
p := viper.GetString("db.path") | ||
p, err := homedir.Expand(p) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return config.NormalisePath(p), nil | ||
|
||
} |
Oops, something went wrong.