mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
basic root dir pre check on fs storage.New (#199)
* basic root dir pre check on fs storage.New * update uses of fs.storage.New to check for error * use testify to check for error
This commit is contained in:
committed by
Aaron Schlesinger
parent
9e76722acc
commit
5101e1ea6d
@@ -23,7 +23,10 @@ func GetStorage() (storage.BackendConnector, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("missing disk storage root (%s)", err)
|
||||
}
|
||||
s := fs.NewStorage(rootLocation, afero.NewOsFs())
|
||||
s, err := fs.NewStorage(rootLocation, afero.NewOsFs())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create new storage from os fs (%s)", err)
|
||||
}
|
||||
return storage.NoOpBackendConnector(s), nil
|
||||
case "mongo":
|
||||
mongoURI, err := envy.MustGet("ATHENS_MONGO_STORAGE_URL")
|
||||
|
||||
@@ -35,7 +35,10 @@ func GetStorage() (storage.BackendConnector, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("missing disk storage root (%s)", err)
|
||||
}
|
||||
s := fs.NewStorage(storageRoot, afero.NewOsFs())
|
||||
s, err := fs.NewStorage(storageRoot, afero.NewOsFs())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create new storage from os fs (%s)", err)
|
||||
}
|
||||
return storage.NoOpBackendConnector(s), nil
|
||||
case "postgres", "sqlite", "cockroach", "mysql":
|
||||
storageRoot, err = envy.MustGet("ATHENS_RDBMS_STORAGE_NAME")
|
||||
|
||||
@@ -37,7 +37,8 @@ func (d *FsTests) SetupTest() {
|
||||
memFs := afero.NewOsFs()
|
||||
r, err := afero.TempDir(memFs, "", "athens-fs-tests")
|
||||
d.Require().NoError(err)
|
||||
d.storage = NewStorage(r, memFs)
|
||||
d.storage, err = NewStorage(r, memFs)
|
||||
d.Require().NoError(err)
|
||||
d.rootDir = r
|
||||
d.fs = memFs
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,6 +1,7 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gomods/athens/pkg/storage"
|
||||
@@ -23,7 +24,15 @@ func (s *storageImpl) versionLocation(module, version string) string {
|
||||
|
||||
// NewStorage returns a new ListerSaver implementation that stores
|
||||
// everything under rootDir
|
||||
func NewStorage(rootDir string, filesystem afero.Fs) storage.Backend {
|
||||
return &storageImpl{rootDir: rootDir, filesystem: filesystem}
|
||||
// If the root directory does not exist an error is returned
|
||||
func NewStorage(rootDir string, filesystem afero.Fs) (storage.Backend, error) {
|
||||
exists, err := afero.Exists(filesystem, rootDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not check if root directory `%s` exists: %s", rootDir, err)
|
||||
}
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("root directory `%s` does not exist", rootDir)
|
||||
}
|
||||
return &storageImpl{rootDir: rootDir, filesystem: filesystem}, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -29,7 +29,10 @@ func NewStorage() (storage.BackendConnector, error) {
|
||||
return nil, fmt.Errorf("could not create temp dir for 'In Memory' storage (%s)", err)
|
||||
}
|
||||
|
||||
s := fs.NewStorage(tmpDir, memFs)
|
||||
s, err := fs.NewStorage(tmpDir, memFs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create storage from memory fs (%s)", err)
|
||||
}
|
||||
memStorage = storage.NoOpBackendConnector(s)
|
||||
return memStorage, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user