提交 3986af76 作者: Brian Tiger Chow

refactor(repo): move public methods

上级 47e4a35e
...@@ -6,10 +6,11 @@ import ( ...@@ -6,10 +6,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/jbenet/go-ipfs/repo" repo "github.com/jbenet/go-ipfs/repo"
common "github.com/jbenet/go-ipfs/repo/common"
config "github.com/jbenet/go-ipfs/repo/config" config "github.com/jbenet/go-ipfs/repo/config"
util "github.com/jbenet/go-ipfs/util" util "github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror" debugerror "github.com/jbenet/go-ipfs/util/debugerror"
) )
type FSRepo struct { type FSRepo struct {
...@@ -107,6 +108,42 @@ func (r *FSRepo) SetConfig(conf *config.Config) error { ...@@ -107,6 +108,42 @@ func (r *FSRepo) SetConfig(conf *config.Config) error {
return nil return nil
} }
// GetConfigKey retrieves only the value of a particular key
func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
filename, err := config.Filename(r.path)
if err != nil {
return nil, err
}
var cfg map[string]interface{}
if err := readConfigFile(filename, &cfg); err != nil {
return nil, err
}
return common.MapGetKV(cfg, key)
}
// SetConfigKey writes the value of a particular key
func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
filename, err := config.Filename(r.path)
if err != nil {
return err
}
var mapconf map[string]interface{}
if err := readConfigFile(filename, &mapconf); err != nil {
return err
}
if err := common.MapSetKV(mapconf, key, value); err != nil {
return err
}
if err := writeConfigFile(filename, mapconf); err != nil {
return err
}
conf, err := convertMapToConfig(mapconf)
if err != nil {
return err
}
return r.SetConfig(conf)
}
func (r *FSRepo) Close() error { func (r *FSRepo) Close() error {
if r.state != opened { if r.state != opened {
return debugerror.Errorf("repo is %s", r.state) return debugerror.Errorf("repo is %s", r.state)
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"path/filepath" "path/filepath"
"time" "time"
common "github.com/jbenet/go-ipfs/repo/common"
"github.com/jbenet/go-ipfs/repo/config" "github.com/jbenet/go-ipfs/repo/config"
"github.com/jbenet/go-ipfs/util" "github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror" "github.com/jbenet/go-ipfs/util/debugerror"
...@@ -74,45 +73,6 @@ func encode(w io.Writer, value interface{}) error { ...@@ -74,45 +73,6 @@ func encode(w io.Writer, value interface{}) error {
return err return err
} }
// GetConfigKey retrieves only the value of a particular key
func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
filename, err := config.Filename(r.path)
if err != nil {
return nil, err
}
var cfg map[string]interface{}
if err := readConfigFile(filename, &cfg); err != nil {
return nil, err
}
return common.MapGetKV(cfg, key)
}
// SetConfigKey writes the value of a particular key
func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
filename, err := config.Filename(r.path)
if err != nil {
return err
}
var mapconf map[string]interface{}
if err := readConfigFile(filename, &mapconf); err != nil {
return err
}
if err := common.MapSetKV(mapconf, key, value); err != nil {
return err
}
// must use raw method because there may exist keys not present in the *config.Config struct
if err := writeConfigFile(filename, mapconf); err != nil {
return err
}
conf, err := convertMapToConfig(mapconf)
if err != nil {
return err
}
*r.config = *conf // copy so caller cannot modify the private config
return nil
}
func convertMapToConfig(v map[string]interface{}) (*config.Config, error) { func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
var buf bytes.Buffer var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil { if err := json.NewEncoder(&buf).Encode(v); err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论