refactor(config, repo): all writes go through FSRepo. next: privatize these
Brian Tiger Chow committed
Jan 12, 2015 at 15:00 UTC
405afd2a02d74ed078dd78f64c3905a642f80dfd
11 files changed
+81
-75
cmd/ipfs/main.go
+2
-1
@@ -23,6 +23,7 @@ import (
23
daemon "github.com/jbenet/go-ipfs/core/daemon"
24
repo "github.com/jbenet/go-ipfs/repo"
25
config "github.com/jbenet/go-ipfs/repo/config"
26
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
27
updates "github.com/jbenet/go-ipfs/updates"
28
u "github.com/jbenet/go-ipfs/util"
29
"github.com/jbenet/go-ipfs/util/debugerror"
@@ -449,7 +450,7 @@ func loadConfig(path string) (*config.Config, error) {
450
return nil, err
451
}
452
452
- return config.Load(configFile)
453
+ return fsrepo.Load(configFile)
454
}
455
456
// startProfiling begins CPU profiling and returns a `stop` function to be
cmd/ipfs/tour.go
+2
-1
@@ -10,6 +10,7 @@ import (
10
cmds "github.com/jbenet/go-ipfs/commands"
11
config "github.com/jbenet/go-ipfs/repo/config"
12
tour "github.com/jbenet/go-ipfs/tour"
13
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
14
)
15
16
var tourCmd = &cmds.Command{
@@ -191,5 +192,5 @@ func writeConfig(path string, cfg *config.Config) error {
192
if err != nil {
193
return err
194
}
194
- return config.WriteConfigFile(filename, cfg)
195
+ return fsrepo.WriteConfigFile(filename, cfg)
196
}
core/commands/bootstrap.go
+4
-3
@@ -6,6 +6,7 @@ import (
6
7
cmds "github.com/jbenet/go-ipfs/commands"
8
config "github.com/jbenet/go-ipfs/repo/config"
9
+ "github.com/jbenet/go-ipfs/repo/fsrepo"
10
u "github.com/jbenet/go-ipfs/util"
11
errors "github.com/jbenet/go-ipfs/util/debugerror"
12
)
@@ -250,7 +251,7 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []config.BootstrapP
251
}
252
}
253
253
- err := config.WriteConfigFile(filename, cfg)
254
+ err := fsrepo.WriteConfigFile(filename, cfg)
255
if err != nil {
256
return nil, err
257
}
@@ -278,7 +279,7 @@ func bootstrapRemove(filename string, cfg *config.Config, toRemove []config.Boot
279
}
280
cfg.Bootstrap = keep
281
281
- err := config.WriteConfigFile(filename, cfg)
282
+ err := fsrepo.WriteConfigFile(filename, cfg)
283
if err != nil {
284
return nil, err
285
}
@@ -291,7 +292,7 @@ func bootstrapRemoveAll(filename string, cfg *config.Config) ([]config.Bootstrap
292
copy(removed, cfg.Bootstrap)
293
294
cfg.Bootstrap = nil
294
- err := config.WriteConfigFile(filename, cfg)
295
+ err := fsrepo.WriteConfigFile(filename, cfg)
296
if err != nil {
297
return nil, err
298
}
core/commands/config.go
+3
-2
@@ -11,6 +11,7 @@ import (
11
12
cmds "github.com/jbenet/go-ipfs/commands"
13
config "github.com/jbenet/go-ipfs/repo/config"
14
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
15
u "github.com/jbenet/go-ipfs/util"
16
)
17
@@ -141,7 +142,7 @@ variable set to your preferred text editor.
142
}
143
144
func getConfig(filename string, key string) (*ConfigField, error) {
144
- value, err := config.ReadConfigKey(filename, key)
145
+ value, err := fsrepo.ReadConfigKey(filename, key)
146
if err != nil {
147
return nil, fmt.Errorf("Failed to get config value: %s", err)
148
}
@@ -153,7 +154,7 @@ func getConfig(filename string, key string) (*ConfigField, error) {
154
}
155
156
func setConfig(filename string, key, value string) (*ConfigField, error) {
156
- err := config.WriteConfigKey(filename, key, value)
157
+ err := fsrepo.WriteConfigKey(filename, key, value)
158
if err != nil {
159
return nil, fmt.Errorf("Failed to set config value: %s", err)
160
}
repo/config/config.go
+11
-23
@@ -3,6 +3,7 @@ package config
3
4
import (
5
"encoding/base64"
6
+ "encoding/json"
7
"errors"
8
"os"
9
"path/filepath"
@@ -13,7 +14,6 @@ import (
14
15
ic "github.com/jbenet/go-ipfs/p2p/crypto"
16
u "github.com/jbenet/go-ipfs/util"
16
- "github.com/jbenet/go-ipfs/util/debugerror"
17
)
18
19
var log = u.Logger("config")
@@ -191,29 +191,17 @@ func (i *Identity) DecodePrivateKey(passphrase string) (ic.PrivKey, error) {
191
return ic.UnmarshalPrivateKey(pkb)
192
}
193
194
-// Load reads given file and returns the read config, or error.
195
-func Load(filename string) (*Config, error) {
196
- // if nothing is there, fail. User must run 'ipfs init'
197
- if !u.FileExists(filename) {
198
- return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
194
+// HumanOutput gets a config value ready for printing
195
+func HumanOutput(value interface{}) ([]byte, error) {
196
+ s, ok := value.(string)
197
+ if ok {
198
+ return []byte(strings.Trim(s, "\n")), nil
199
}
200
-
201
- var cfg Config
202
- err := ReadConfigFile(filename, &cfg)
203
- if err != nil {
204
- return nil, err
205
- }
206
-
207
- // tilde expansion on datastore path
208
- cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
209
- if err != nil {
210
- return nil, err
211
- }
212
-
213
- return &cfg, err
200
+ return Marshal(value)
201
}
202
216
-// Set sets the value of a particular config key
217
-func Set(filename, key, value string) error {
218
- return WriteConfigKey(filename, key, value)
203
+// Marshal configuration with JSON
204
+func Marshal(value interface{}) ([]byte, error) {
205
+ // need to prettyprint, hence MarshalIndent, instead of Encoder
206
+ return json.MarshalIndent(value, "", " ")
207
}
repo/config/version.go
-13
@@ -120,19 +120,6 @@ func (v *Version) ShouldCheckForUpdate() bool {
120
return true
121
}
122
123
-// RecordUpdateCheck is called to record that an update check was performed,
124
-// showing that the running version is the most recent one.
125
-func RecordUpdateCheck(cfg *Config, filename string) {
126
- cfg.Version.CheckDate = time.Now()
127
-
128
- if cfg.Version.CheckPeriod == "" {
129
- // CheckPeriod was not initialized for some reason (e.g. config file broken)
130
- log.Error("config.Version.CheckPeriod not set. config broken?")
131
- }
132
-
133
- WriteConfigFile(filename, cfg)
134
-}
135
-
123
// VersionDefaultValue returns the default version config value (for init).
124
func VersionDefaultValue() Version {
125
return Version{
repo/config/version_test.go
+2
-3
@@ -1,6 +1,7 @@
1
package config
2
3
import (
4
+ "encoding/json"
5
"strings"
6
"testing"
7
)
@@ -23,8 +24,7 @@ func TestAutoUpdateValues(t *testing.T) {
24
}
25
26
for i, tc := range tests {
26
- err := Decode(strings.NewReader(tc.input), &tval)
27
- if err != tc.err {
27
+ if err := json.NewDecoder(strings.NewReader(tc.input)).Decode(&tval); err != tc.err {
28
t.Fatalf("%d failed - got err %q wanted %v", i, err, tc.err)
29
}
30
@@ -32,5 +32,4 @@ func TestAutoUpdateValues(t *testing.T) {
32
t.Fatalf("%d failed - got val %q where we wanted %q", i, tval.AutoUpdate, tc.val)
33
}
34
}
35
-
35
}
repo/fsrepo/fsrepo.go
+1
-1
@@ -54,7 +54,7 @@ func (r *FSRepo) SetConfig(conf *config.Config) error {
54
if err != nil {
55
return err
56
}
57
- if err := config.WriteConfigFile(configFilename, conf); err != nil {
57
+ if err := WriteConfigFile(configFilename, conf); err != nil {
58
return err
59
}
60
r.config = *conf // copy so caller cannot modify the private config
repo/fsrepo/serialize.go
renamed
+50
-25
@@ -1,4 +1,4 @@
1
-package config
1
+package fsrepo
2
3
import (
4
"encoding/json"
@@ -7,8 +7,15 @@ import (
7
"os"
8
"path/filepath"
9
"strings"
10
+ "time"
11
+
12
+ "github.com/jbenet/go-ipfs/repo/config"
13
+ "github.com/jbenet/go-ipfs/util"
14
+ "github.com/jbenet/go-ipfs/util/debugerror"
15
)
16
17
+var log = util.Logger("fsrepo")
18
+
19
// ReadConfigFile reads the config from `filename` into `cfg`.
20
func ReadConfigFile(filename string, cfg interface{}) error {
21
f, err := os.Open(filename)
@@ -16,8 +23,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
23
return err
24
}
25
defer f.Close()
19
-
20
- if err := Decode(f, cfg); err != nil {
26
+ if err := json.NewDecoder(f).Decode(cfg); err != nil {
27
return fmt.Errorf("Failure to decode config: %s", err)
28
}
29
return nil
@@ -56,38 +62,17 @@ func WriteFile(filename string, buf []byte) error {
62
return err
63
}
64
59
-// HumanOutput gets a config value ready for printing
60
-func HumanOutput(value interface{}) ([]byte, error) {
61
- s, ok := value.(string)
62
- if ok {
63
- return []byte(strings.Trim(s, "\n")), nil
64
- }
65
- return Marshal(value)
66
-}
67
-
68
-// Marshal configuration with JSON
69
-func Marshal(value interface{}) ([]byte, error) {
70
- // need to prettyprint, hence MarshalIndent, instead of Encoder
71
- return json.MarshalIndent(value, "", " ")
72
-}
73
-
65
// Encode configuration with JSON
66
func Encode(w io.Writer, value interface{}) error {
67
// need to prettyprint, hence MarshalIndent, instead of Encoder
77
- buf, err := Marshal(value)
68
+ buf, err := config.Marshal(value)
69
if err != nil {
70
return err
71
}
81
-
72
_, err = w.Write(buf)
73
return err
74
}
75
86
-// Decode configuration with JSON
87
-func Decode(r io.Reader, value interface{}) error {
88
- return json.NewDecoder(r).Decode(value)
89
-}
90
-
76
// ReadConfigKey retrieves only the value of a particular key
77
func ReadConfigKey(filename, key string) (interface{}, error) {
78
var cfg interface{}
@@ -142,3 +127,43 @@ func WriteConfigKey(filename, key string, value interface{}) error {
127
128
return WriteConfigFile(filename, cfg)
129
}
130
+
131
+// Load reads given file and returns the read config, or error.
132
+func Load(filename string) (*config.Config, error) {
133
+ // if nothing is there, fail. User must run 'ipfs init'
134
+ if !util.FileExists(filename) {
135
+ return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
136
+ }
137
+
138
+ var cfg config.Config
139
+ err := ReadConfigFile(filename, &cfg)
140
+ if err != nil {
141
+ return nil, err
142
+ }
143
+
144
+ // tilde expansion on datastore path
145
+ cfg.Datastore.Path, err = util.TildeExpansion(cfg.Datastore.Path)
146
+ if err != nil {
147
+ return nil, err
148
+ }
149
+
150
+ return &cfg, err
151
+}
152
+
153
+// Set sets the value of a particular config key
154
+func Set(filename, key, value string) error {
155
+ return WriteConfigKey(filename, key, value)
156
+}
157
+
158
+// RecordUpdateCheck is called to record that an update check was performed,
159
+// showing that the running version is the most recent one.
160
+func RecordUpdateCheck(cfg *config.Config, filename string) {
161
+ cfg.Version.CheckDate = time.Now()
162
+
163
+ if cfg.Version.CheckPeriod == "" {
164
+ // CheckPeriod was not initialized for some reason (e.g. config file broken)
165
+ log.Error("config.Version.CheckPeriod not set. config broken?")
166
+ }
167
+
168
+ WriteConfigFile(filename, cfg)
169
+}
repo/fsrepo/serialize_test.go
renamed
+4
-2
@@ -1,13 +1,15 @@
1
-package config
1
+package fsrepo
2
3
import (
4
"testing"
5
+
6
+ config "github.com/jbenet/go-ipfs/repo/config"
7
)
8
9
func TestConfig(t *testing.T) {
10
const filename = ".ipfsconfig"
11
const dsPath = "/path/to/datastore"
10
- cfgWritten := new(Config)
12
+ cfgWritten := new(config.Config)
13
cfgWritten.Datastore.Path = dsPath
14
err := WriteConfigFile(filename, cfgWritten)
15
if err != nil {
updates/updates.go
+2
-1
@@ -6,6 +6,7 @@ import (
6
"time"
7
8
config "github.com/jbenet/go-ipfs/repo/config"
9
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
10
u "github.com/jbenet/go-ipfs/util"
11
12
semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
@@ -209,7 +210,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
210
// if there is no update available, record it, and exit.
211
if err == ErrNoUpdateAvailable {
212
log.Noticef("No update available, checked on %s", time.Now())
212
- config.RecordUpdateCheck(cfg, confFile) // only record if we checked successfully.
213
+ fsrepo.RecordUpdateCheck(cfg, confFile) // only record if we checked successfully.
214
return nil
215
}
216