refactor(init) extract initCheckDir function

License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

# TYPES
# feat
# fix
# docs
# style (formatting, missing semi colons, etc; no code change):
# refactor
# test (adding missing tests, refactoring tests; no production code change)
# chore (updating grunt tasks etc; no production code change)
This commit is contained in:
Brian Tiger Chow 2014-11-16 03:51:36 -08:00
parent 2b72d9f14a
commit d28c9cc7b4

View File

@ -131,16 +131,9 @@ func datastoreConfig(dspath string) (config.Datastore, error) {
ds.Path = dspath
ds.Type = "leveldb"
// Construct the data store if missing
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
return ds, err
}
// Check the directory is writeable
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return ds, errors.New("Datastore '" + dspath + "' is not writeable")
err := initCheckDir(dspath)
if err != nil {
return ds, errors.Errorf("datastore: %s", err)
}
return ds, nil
@ -224,3 +217,19 @@ func identityConfig(nbits int) (config.Identity, error) {
return ident, nil
}
// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
// Construct the path if missing
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
// Check the directory is writeable
if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return errors.New("'" + path + "' is not writeable")
}
return nil
}