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)
Brian Tiger Chow committed
Nov 16, 2014 at 03:51 UTC
d28c9cc7b44f457e5a490f7b698f2b8ebe5bc90e
1 file changed
+19
-10
cmd/ipfs2/init.go
+19
-10
@@ -131,16 +131,9 @@ func datastoreConfig(dspath string) (config.Datastore, error) {
131
ds.Path = dspath
132
ds.Type = "leveldb"
133
134
- // Construct the data store if missing
135
- if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
136
- return ds, err
137
- }
138
-
139
- // Check the directory is writeable
140
- if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
141
- os.Remove(f.Name())
142
- } else {
143
- return ds, errors.New("Datastore '" + dspath + "' is not writeable")
134
+ err := initCheckDir(dspath)
135
+ if err != nil {
136
+ return ds, errors.Errorf("datastore: %s", err)
137
}
138
139
return ds, nil
@@ -224,3 +217,19 @@ func identityConfig(nbits int) (config.Identity, error) {
217
218
return ident, nil
219
}
220
+
221
+// initCheckDir ensures the directory exists and is writable
222
+func initCheckDir(path string) error {
223
+ // Construct the path if missing
224
+ if err := os.MkdirAll(path, os.ModePerm); err != nil {
225
+ return err
226
+ }
227
+
228
+ // Check the directory is writeable
229
+ if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
230
+ os.Remove(f.Name())
231
+ } else {
232
+ return errors.New("'" + path + "' is not writeable")
233
+ }
234
+ return nil
235
+}