feat: add database initialisation

This commit is contained in:
Dan Anglin 2021-09-15 00:25:05 +01:00
parent 279fadefdc
commit e70d7c5611
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 51 additions and 22 deletions

11
db.go
View file

@ -22,7 +22,13 @@ func mkDataDir(dir string) error {
return os.MkdirAll(dir, 0700)
}
// TODO: Needs unit testing and documentation
// dbPath returns the path to the database file. If a path is given then that is returned. Otherwise the default path is returned.
// For linux, the default location of the database file is $XDG_DATA_HOME/pelican/pelican.db. If the XDG_DATA_HOME environment
// variable is not set then it will default to $HOME/.local/share/pelican/pelican.db. For all other operating systems the default
// location is $HOME/.pelican/pelican.db.
//
// Before returning the path, dbPath attempts to create the directory of the database file. An error is returned if there
// are any issues creating this directory.
func dbPath(path string) (string, error) {
if len(path) > 0 {
filepath.Dir(path)
@ -58,9 +64,6 @@ func dbPath(path string) (string, error) {
}
// openDatabase opens the database, at a given path, for reading and writing. If the file does not exist it will be created.
// For linux, the default location of the database file is $XDG_DATA_HOME/pelican/pelican.db. If the XDG_DATA_HOME environment
// variable is not set then it will default to $HOME/.local/share/pelican/pelican.db.
// For all other operating systems the default location is $HOME/.pelican/pelican.db.
func openDatabase(path string) (*bolt.DB, error) {
opts := bolt.Options{
Timeout: 1 * time.Second,

View file

@ -34,10 +34,7 @@ func TestDbCustomPath(t *testing.T) {
}
}
// TODO: Finish testing
func TestDbDefaultPath(t *testing.T) {
var wantPath string
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("An error occurred whilst getting the current directory, %s", err)
@ -50,12 +47,25 @@ func TestDbDefaultPath(t *testing.T) {
t.Fatalf("An error occurred whilst setting the XDG_DATA_HOME environment variable, %s", err)
}
var want string
goos := runtime.GOOS
switch goos {
case "linux":
wantPath = filepath.Join(os.Getenv("XDG_DATA_HOME"), "pelican", "pelican.db")
want = filepath.Join(os.Getenv("XDG_DATA_HOME"), "pelican", "pelican.db")
default:
wantPath = filepath.Join(os.Getenv("HOME"), ".pelican", "pelican.db")
want = filepath.Join(os.Getenv("HOME"), ".pelican", "pelican.db")
}
got, err := dbPath("")
if err != nil {
t.Fatalf("An error occurred whilst executing `dbPath`, %s", err)
}
if got != want {
t.Errorf("Got unexpected database path: want %s, got %s", want, got)
} else {
t.Logf("Got expected database path: got %s", got)
}
}

View file

@ -9,13 +9,39 @@ import (
"github.com/magefile/mage/sh"
)
const (
binary = "pelican"
)
var Default = Build
var binary = "pelican"
// Test run the go tests
// To enable verbose mode set PELICAN_TEST_VERBOSE=1.
// To enable coverage mode set PELICAN_TEST_COVER=1.
func Test() error {
goTest := sh.RunCmd("go", "test")
args := []string{"."}
if os.Getenv("PELICAN_TEST_VERBOSE") == "1" {
args = append(args, "-v")
}
if os.Getenv("PELICAN_TEST_COVER") == "1" {
args = append(args, "-cover")
}
return goTest(args...)
}
// Build build the executable
func Build() error {
return sh.Run("go", "build", "-o", binary, "main.go")
}
// Clean clean the workspace
func Clean() error {
if err := os.Remove(binary); err != nil {
if err := sh.Rm(binary); err != nil {
return err
}
@ -33,19 +59,9 @@ func Clean() error {
for _, f := range files {
filename := f.Name()
if strings.HasSuffix(filename, ".db") {
os.Remove(testDBDir + "/" + filename)
sh.Rm(testDBDir + "/" + filename)
}
}
return nil
}
// Test run the go tests
func Test() error {
return sh.Run("go", "test", ".")
}
// Build build the executable
func Build() error {
return sh.Run("go", "build", "-o", binary, "main.go")
}