refactor(fsrepo, component): expose SetPath to ensure that components handle paths
Brian Tiger Chow committed
Jan 14, 2015 at 08:26 UTC
7ad559b8c72ceb560ae26e0835343be9f1c84df4
3 files changed
+12
-6
repo/fsrepo/component/component.go
+1
@@ -9,6 +9,7 @@ import (
9
type Component interface {
10
Open() error
11
io.Closer
12
+ SetPath(string)
13
}
14
type Initializer func(path string, conf *config.Config) error
15
type InitializationChecker func(path string) bool
repo/fsrepo/component/config.go
+9
-5
@@ -15,7 +15,7 @@ var _ InitializationChecker = ConfigComponentIsInitialized
15
// NB: create with makeConfigComponent function.
16
// NOT THREAD-SAFE
17
type ConfigComponent struct {
18
- Path string // required at instantiation
18
+ path string // required at instantiation
19
config *config.Config // assigned on Open()
20
}
21
@@ -39,7 +39,7 @@ func InitConfigComponent(path string, conf *config.Config) error {
39
40
// Open returns an error if the config file is not present.
41
func (c *ConfigComponent) Open() error {
42
- configFilename, err := config.Filename(c.Path)
42
+ configFilename, err := config.Filename(c.path)
43
if err != nil {
44
return err
45
}
@@ -67,7 +67,7 @@ func (c *ConfigComponent) SetConfig(updated *config.Config) error {
67
68
// GetConfigKey retrieves only the value of a particular key.
69
func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) {
70
- filename, err := config.Filename(c.Path)
70
+ filename, err := config.Filename(c.path)
71
if err != nil {
72
return nil, err
73
}
@@ -80,7 +80,7 @@ func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) {
80
81
// SetConfigKey writes the value of a particular key.
82
func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error {
83
- filename, err := config.Filename(c.Path)
83
+ filename, err := config.Filename(c.path)
84
if err != nil {
85
return err
86
}
@@ -103,6 +103,10 @@ func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error {
103
return c.setConfigUnsynced(conf) // TODO roll this into this method
104
}
105
106
+func (c *ConfigComponent) SetPath(p string) {
107
+ c.path = p
108
+}
109
+
110
// ConfigComponentIsInitialized returns true if the repo is initialized at
111
// provided |path|.
112
func ConfigComponentIsInitialized(path string) bool {
@@ -118,7 +122,7 @@ func ConfigComponentIsInitialized(path string) bool {
122
123
// setConfigUnsynced is for private use.
124
func (r *ConfigComponent) setConfigUnsynced(updated *config.Config) error {
121
- configFilename, err := config.Filename(r.Path)
125
+ configFilename, err := config.Filename(r.path)
126
if err != nil {
127
return err
128
}
repo/fsrepo/fsrepo.go
+2
-1
@@ -314,7 +314,8 @@ func componentBuilders() []componentBuilder {
314
Init: component.InitConfigComponent,
315
IsInitialized: component.ConfigComponentIsInitialized,
316
OpenHandler: func(r *FSRepo) error {
317
- cc := component.ConfigComponent{Path: r.path}
317
+ cc := component.ConfigComponent{}
318
+ cc.SetPath(r.path)
319
if err := cc.Open(); err != nil {
320
return err
321
}