Skip to content

Commit

Permalink
extract fs funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd committed Nov 16, 2023
1 parent 8e49c5d commit fedbb66
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
23 changes: 4 additions & 19 deletions pkg/bdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package bdb

import (
"net/http"
"os"
"path/filepath"
"time"

"github.com/aserto-dev/azm/cache"
"github.com/aserto-dev/azm/model"
cerr "github.com/aserto-dev/errors"
"github.com/aserto-dev/go-edge-ds/pkg/fs"

"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -59,13 +59,9 @@ func (s *BoltDB) Open() error {
}

dbDir := filepath.Dir(s.config.DBPath)
exists, err := filePathExists(dbDir)
if err != nil {
return errors.Wrap(err, "failed to determine if store path/file exists")
}
if !exists {
if err = os.MkdirAll(dbDir, 0700); err != nil {
return errors.Wrapf(err, "failed to create directory '%s'", dbDir)
if !fs.DirExists(dbDir) {
if err := fs.EnsureDirPath(dbDir); err != nil {
return err
}
}

Expand All @@ -91,17 +87,6 @@ func (s *BoltDB) Close() {
}
}

// filePathExists, internal helper function to detect if the file path exists.
func filePathExists(path string) (bool, error) {
if _, err := os.Stat(path); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, errors.Wrapf(err, "failed to stat file [%s]", path)
}
}

func (s *BoltDB) DB() *bolt.DB {
return s.db
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/bdb/migrate/mig/mig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/aserto-dev/go-edge-ds/pkg/bdb"
"github.com/aserto-dev/go-edge-ds/pkg/fs"
"github.com/rs/zerolog"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -197,6 +198,13 @@ func Backup(db *bolt.DB, version *semver.Version) error {
}

func OpenDB(cfg *bdb.Config) (*bolt.DB, error) {
dbDir := filepath.Dir(cfg.DBPath)
if !fs.DirExists(dbDir) {
if err := fs.EnsureDirPath(dbDir); err != nil {
return nil, err
}
}

db, err := bolt.Open(cfg.DBPath, 0644, &bolt.Options{
Timeout: cfg.RequestTimeout,
})
Expand Down
16 changes: 2 additions & 14 deletions pkg/bdb/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aserto-dev/go-edge-ds/pkg/bdb/migrate/mig001"
"github.com/aserto-dev/go-edge-ds/pkg/bdb/migrate/mig002"
"github.com/aserto-dev/go-edge-ds/pkg/bdb/migrate/mig003"
"github.com/pkg/errors"
"github.com/aserto-dev/go-edge-ds/pkg/fs"

"github.com/Masterminds/semver"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -39,13 +39,11 @@ var (
// higher returns false, error
// errors: returns false, error.
func CheckSchemaVersion(config *bdb.Config, logger *zerolog.Logger, reqVersion *semver.Version) (bool, error) {
if exist, err := fileExists(config.DBPath); err == nil && !exist {
if !fs.FileExists(config.DBPath) {
if err := create(config, logger, reqVersion); err != nil {
return false, err
}
return true, nil
} else if err != nil {
return false, err
}

boltdb, err := bdb.New(config, logger)
Expand Down Expand Up @@ -218,13 +216,3 @@ func execute(logger *zerolog.Logger, roDB, rwDB *bolt.DB, newVersion *semver.Ver
}
return os.ErrNotExist
}

func fileExists(path string) (bool, error) {
if _, err := os.Stat(path); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, errors.Wrapf(err, "failed to stat file '%s'", path)
}
}
28 changes: 28 additions & 0 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fs

import (
"os"
)

func FileExists(path string) bool {
fsInfo, err := os.Stat(path)
if err == nil && !fsInfo.IsDir() {
return true
}
return false
}

func DirExists(path string) bool {
fsInfo, err := os.Stat(path)
if err == nil && fsInfo.IsDir() {
return true
}
return false
}

func EnsureDirPath(path string) error {
if !DirExists(path) {
return os.MkdirAll(path, 0700)
}
return nil
}
6 changes: 4 additions & 2 deletions tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ func TestMain(m *testing.M) {
logger := zerolog.New(io.Discard)

dirPath := os.TempDir()
os.MkdirAll(dirPath, 0700)
if err := os.MkdirAll(dirPath, 0700); err != nil {
panic(err)
}

dbPath := path.Join(os.TempDir(), "edge-ds", "test-eds.db")
dbPath := path.Join(dirPath, "edge-ds", "test-eds.db")
os.Remove(dbPath)
fmt.Println(dbPath)

Expand Down

0 comments on commit fedbb66

Please sign in to comment.