提交 7ad559b8 作者: Brian Tiger Chow

refactor(fsrepo, component): expose SetPath to ensure that components handle paths

上级 b6603051
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
type Component interface { type Component interface {
Open() error Open() error
io.Closer io.Closer
SetPath(string)
} }
type Initializer func(path string, conf *config.Config) error type Initializer func(path string, conf *config.Config) error
type InitializationChecker func(path string) bool type InitializationChecker func(path string) bool
...@@ -15,7 +15,7 @@ var _ InitializationChecker = ConfigComponentIsInitialized ...@@ -15,7 +15,7 @@ var _ InitializationChecker = ConfigComponentIsInitialized
// NB: create with makeConfigComponent function. // NB: create with makeConfigComponent function.
// NOT THREAD-SAFE // NOT THREAD-SAFE
type ConfigComponent struct { type ConfigComponent struct {
Path string // required at instantiation path string // required at instantiation
config *config.Config // assigned on Open() config *config.Config // assigned on Open()
} }
...@@ -39,7 +39,7 @@ func InitConfigComponent(path string, conf *config.Config) error { ...@@ -39,7 +39,7 @@ func InitConfigComponent(path string, conf *config.Config) error {
// Open returns an error if the config file is not present. // Open returns an error if the config file is not present.
func (c *ConfigComponent) Open() error { func (c *ConfigComponent) Open() error {
configFilename, err := config.Filename(c.Path) configFilename, err := config.Filename(c.path)
if err != nil { if err != nil {
return err return err
} }
...@@ -67,7 +67,7 @@ func (c *ConfigComponent) SetConfig(updated *config.Config) error { ...@@ -67,7 +67,7 @@ func (c *ConfigComponent) SetConfig(updated *config.Config) error {
// GetConfigKey retrieves only the value of a particular key. // GetConfigKey retrieves only the value of a particular key.
func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) { func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) {
filename, err := config.Filename(c.Path) filename, err := config.Filename(c.path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -80,7 +80,7 @@ func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) { ...@@ -80,7 +80,7 @@ func (c *ConfigComponent) GetConfigKey(key string) (interface{}, error) {
// SetConfigKey writes the value of a particular key. // SetConfigKey writes the value of a particular key.
func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error { func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error {
filename, err := config.Filename(c.Path) filename, err := config.Filename(c.path)
if err != nil { if err != nil {
return err return err
} }
...@@ -103,6 +103,10 @@ func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error { ...@@ -103,6 +103,10 @@ func (c *ConfigComponent) SetConfigKey(key string, value interface{}) error {
return c.setConfigUnsynced(conf) // TODO roll this into this method return c.setConfigUnsynced(conf) // TODO roll this into this method
} }
func (c *ConfigComponent) SetPath(p string) {
c.path = p
}
// ConfigComponentIsInitialized returns true if the repo is initialized at // ConfigComponentIsInitialized returns true if the repo is initialized at
// provided |path|. // provided |path|.
func ConfigComponentIsInitialized(path string) bool { func ConfigComponentIsInitialized(path string) bool {
...@@ -118,7 +122,7 @@ func ConfigComponentIsInitialized(path string) bool { ...@@ -118,7 +122,7 @@ func ConfigComponentIsInitialized(path string) bool {
// setConfigUnsynced is for private use. // setConfigUnsynced is for private use.
func (r *ConfigComponent) setConfigUnsynced(updated *config.Config) error { func (r *ConfigComponent) setConfigUnsynced(updated *config.Config) error {
configFilename, err := config.Filename(r.Path) configFilename, err := config.Filename(r.path)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -314,7 +314,8 @@ func componentBuilders() []componentBuilder { ...@@ -314,7 +314,8 @@ func componentBuilders() []componentBuilder {
Init: component.InitConfigComponent, Init: component.InitConfigComponent,
IsInitialized: component.ConfigComponentIsInitialized, IsInitialized: component.ConfigComponentIsInitialized,
OpenHandler: func(r *FSRepo) error { OpenHandler: func(r *FSRepo) error {
cc := component.ConfigComponent{Path: r.path} cc := component.ConfigComponent{}
cc.SetPath(r.path)
if err := cc.Open(); err != nil { if err := cc.Open(); err != nil {
return err return err
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论