feat: delete cards #6

Closed
dananglin wants to merge 35 commits from delete-cards into main
3 changed files with 51 additions and 22 deletions
Showing only changes of commit e70d7c5611 - Show all commits

11
db.go
View file

@ -22,7 +22,13 @@ func mkDataDir(dir string) error {
return os.MkdirAll(dir, 0700) 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) { func dbPath(path string) (string, error) {
if len(path) > 0 { if len(path) > 0 {
filepath.Dir(path) 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. // 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) { func openDatabase(path string) (*bolt.DB, error) {
opts := bolt.Options{ opts := bolt.Options{
Timeout: 1 * time.Second, Timeout: 1 * time.Second,

View file

@ -34,10 +34,7 @@ func TestDbCustomPath(t *testing.T) {
} }
} }
// TODO: Finish testing
func TestDbDefaultPath(t *testing.T) { func TestDbDefaultPath(t *testing.T) {
var wantPath string
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
t.Fatalf("An error occurred whilst getting the current directory, %s", err) 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) t.Fatalf("An error occurred whilst setting the XDG_DATA_HOME environment variable, %s", err)
} }
var want string
goos := runtime.GOOS goos := runtime.GOOS
switch goos { switch goos {
case "linux": 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: 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" "github.com/magefile/mage/sh"
) )
const (
binary = "pelican"
)
var Default = Build 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 // Clean clean the workspace
func Clean() error { func Clean() error {
if err := os.Remove(binary); err != nil { if err := sh.Rm(binary); err != nil {
return err return err
} }
@ -33,19 +59,9 @@ func Clean() error {
for _, f := range files { for _, f := range files {
filename := f.Name() filename := f.Name()
if strings.HasSuffix(filename, ".db") { if strings.HasSuffix(filename, ".db") {
os.Remove(testDBDir + "/" + filename) sh.Rm(testDBDir + "/" + filename)
} }
} }
return nil 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")
}