fix(ipfs2/init) datastore
Brian Tiger Chow committed
Nov 4, 2014 at 19:09 UTC
62fd9166ceb7a960f19a00dc290926c3764dd4db
1 file changed
+30
-20
cmd/ipfs2/init.go
+30
-20
@@ -86,28 +86,11 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
86
}
87
cfg := new(config.Config)
88
89
- if len(dspath) == 0 {
90
- dspath, err = config.DataStorePath("")
91
- if err != nil {
92
- return err
93
- }
94
- }
95
- cfg.Datastore = config.Datastore{
96
- Path: dspath,
97
- Type: "leveldb",
98
- }
99
-
100
- // Construct the data store if missing
101
- if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
89
+ ds, err := datastoreConfig(dspath)
90
+ if err != nil {
91
return err
92
}
104
-
105
- // Check the directory is writeable
106
- if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
107
- os.Remove(f.Name())
108
- } else {
109
- return errors.New("Datastore '" + dspath + "' is not writeable")
110
- }
93
+ cfg.Datastore = ds
94
95
cfg.Identity = config.Identity{}
96
@@ -162,3 +145,30 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
145
}
146
return nil
147
}
148
+
149
+func datastoreConfig(dspath string) (config.Datastore, error) {
150
+ ds := config.Datastore{}
151
+ if len(dspath) == 0 {
152
+ var err error
153
+ dspath, err = config.DataStorePath("")
154
+ if err != nil {
155
+ return ds, err
156
+ }
157
+ }
158
+ ds.Path = dspath
159
+ ds.Type = "leveldb"
160
+
161
+ // Construct the data store if missing
162
+ if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
163
+ return ds, err
164
+ }
165
+
166
+ // Check the directory is writeable
167
+ if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
168
+ os.Remove(f.Name())
169
+ } else {
170
+ return ds, errors.New("Datastore '" + dspath + "' is not writeable")
171
+ }
172
+
173
+ return ds, nil
174
+}