diff --git a/pkg/bdb/boltdb.go b/pkg/bdb/boltdb.go index 20840c5..171f917 100644 --- a/pkg/bdb/boltdb.go +++ b/pkg/bdb/boltdb.go @@ -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" @@ -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 } } @@ -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 } diff --git a/pkg/bdb/migrate/mig/mig.go b/pkg/bdb/migrate/mig/mig.go index 30fb188..2aa5e85 100644 --- a/pkg/bdb/migrate/mig/mig.go +++ b/pkg/bdb/migrate/mig/mig.go @@ -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" @@ -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, }) diff --git a/pkg/bdb/migrate/migrate.go b/pkg/bdb/migrate/migrate.go index 0a1ebda..06f106a 100644 --- a/pkg/bdb/migrate/migrate.go +++ b/pkg/bdb/migrate/migrate.go @@ -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" @@ -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) @@ -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) - } -} diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go new file mode 100644 index 0000000..a33eb23 --- /dev/null +++ b/pkg/fs/fs.go @@ -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 +} diff --git a/tests/runner_test.go b/tests/runner_test.go index 142215e..f4fe22b 100644 --- a/tests/runner_test.go +++ b/tests/runner_test.go @@ -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)