refactor(init, fsrepo): go through repo to write to config
Brian Tiger Chow committed
Jan 12, 2015 at 11:33 UTC
fa4cfe8da28776dd2d5c89cc0d8d070cea3a78f4
2 files changed
+45
-6
cmd/ipfs/init.go
+8
-6
@@ -95,11 +95,6 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
95
96
u.POut("initializing ipfs node at %s\n", configRoot)
97
98
- configFilename, err := config.Filename(configRoot)
99
- if err != nil {
100
- return nil, debugerror.New("Couldn't get home directory path")
101
- }
102
-
98
if fsrepo.ConfigIsInitialized(configRoot) && !force {
99
return nil, errCannotInitConfigExists
100
}
@@ -109,7 +104,14 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
104
return nil, err
105
}
106
112
- if err := config.WriteConfigFile(configFilename, conf); err != nil {
107
+ repo := fsrepo.At(configRoot)
108
+ if err := repo.Open(); err != nil {
109
+ return nil, err
110
+ }
111
+ if err := repo.SetConfig(conf); err != nil {
112
+ return nil, err
113
+ }
114
+ if err := repo.Close(); err != nil {
115
return nil, err
116
}
117
repo/fsrepo/fsrepo.go
+37
@@ -1,10 +1,47 @@
1
package fsrepo
2
3
import (
4
+ "io"
5
+
6
config "github.com/jbenet/go-ipfs/repo/config"
7
util "github.com/jbenet/go-ipfs/util"
8
)
9
10
+type FSRepo struct {
11
+ path string
12
+ config config.Config
13
+}
14
+
15
+func At(path string) *FSRepo {
16
+ return &FSRepo{
17
+ path: path,
18
+ }
19
+}
20
+
21
+func (r *FSRepo) Open() error {
22
+ // TODO may need to check that directory is writeable
23
+ // TODO acquire repo lock
24
+ return nil
25
+}
26
+
27
+func (r *FSRepo) SetConfig(conf *config.Config) error {
28
+ configFilename, err := config.Filename(r.path)
29
+ if err != nil {
30
+ return err
31
+ }
32
+ if err := config.WriteConfigFile(configFilename, conf); err != nil {
33
+ return err
34
+ }
35
+ r.config = *conf // copy so caller cannot modify the private config
36
+ return nil
37
+}
38
+
39
+func (r *FSRepo) Close() error {
40
+ return nil // TODO release repo lock
41
+}
42
+
43
+var _ io.Closer = &FSRepo{}
44
+
45
// ConfigIsInitialized returns true if the config exists in provided |path|.
46
func ConfigIsInitialized(path string) bool {
47
configFilename, err := config.Filename(path)