From d28c9cc7b44f457e5a490f7b698f2b8ebe5bc90e Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sun, 16 Nov 2014 03:51:36 -0800 Subject: [PATCH] refactor(init) extract initCheckDir function License: MIT Signed-off-by: Brian Tiger Chow # 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) --- cmd/ipfs2/init.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/cmd/ipfs2/init.go b/cmd/ipfs2/init.go index b0b945716..84b229da0 100644 --- a/cmd/ipfs2/init.go +++ b/cmd/ipfs2/init.go @@ -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 +}