1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-11 07:03:32 +08:00

refactor(repo): privatize serialization methods

This commit is contained in:
Brian Tiger Chow
2015-01-12 17:50:28 -08:00
parent 86258face6
commit 47e4a35e6b
3 changed files with 15 additions and 15 deletions

View File

@ -58,7 +58,7 @@ func (r *FSRepo) Open() error {
if err != nil { if err != nil {
return err return err
} }
conf, err := Load(configFilename) conf, err := load(configFilename)
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,8 +17,8 @@ import (
var log = util.Logger("fsrepo") var log = util.Logger("fsrepo")
// ReadConfigFile reads the config from `filename` into `cfg`. // readConfigFile reads the config from `filename` into `cfg`.
func ReadConfigFile(filename string, cfg interface{}) error { func readConfigFile(filename string, cfg interface{}) error {
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return err return err
@ -30,7 +30,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
return nil return nil
} }
// WriteConfigFile writes the config from `cfg` into `filename`. // writeConfigFile writes the config from `cfg` into `filename`.
func writeConfigFile(filename string, cfg interface{}) error { func writeConfigFile(filename string, cfg interface{}) error {
err := os.MkdirAll(filepath.Dir(filename), 0775) err := os.MkdirAll(filepath.Dir(filename), 0775)
if err != nil { if err != nil {
@ -43,11 +43,11 @@ func writeConfigFile(filename string, cfg interface{}) error {
} }
defer f.Close() defer f.Close()
return Encode(f, cfg) return encode(f, cfg)
} }
// WriteFile writes the buffer at filename // writeFile writes the buffer at filename
func WriteFile(filename string, buf []byte) error { func writeFile(filename string, buf []byte) error {
err := os.MkdirAll(filepath.Dir(filename), 0775) err := os.MkdirAll(filepath.Dir(filename), 0775)
if err != nil { if err != nil {
return err return err
@ -63,8 +63,8 @@ func WriteFile(filename string, buf []byte) error {
return err return err
} }
// Encode configuration with JSON // encode configuration with JSON
func Encode(w io.Writer, value interface{}) error { func encode(w io.Writer, value interface{}) error {
// need to prettyprint, hence MarshalIndent, instead of Encoder // need to prettyprint, hence MarshalIndent, instead of Encoder
buf, err := config.Marshal(value) buf, err := config.Marshal(value)
if err != nil { if err != nil {
@ -81,7 +81,7 @@ func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
return nil, err return nil, err
} }
var cfg map[string]interface{} var cfg map[string]interface{}
if err := ReadConfigFile(filename, &cfg); err != nil { if err := readConfigFile(filename, &cfg); err != nil {
return nil, err return nil, err
} }
@ -95,7 +95,7 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
return err return err
} }
var mapconf map[string]interface{} var mapconf map[string]interface{}
if err := ReadConfigFile(filename, &mapconf); err != nil { if err := readConfigFile(filename, &mapconf); err != nil {
return err return err
} }
if err := common.MapSetKV(mapconf, key, value); err != nil { if err := common.MapSetKV(mapconf, key, value); err != nil {
@ -125,15 +125,15 @@ func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
return &conf, nil return &conf, nil
} }
// Load reads given file and returns the read config, or error. // load reads given file and returns the read config, or error.
func Load(filename string) (*config.Config, error) { func load(filename string) (*config.Config, error) {
// if nothing is there, fail. User must run 'ipfs init' // if nothing is there, fail. User must run 'ipfs init'
if !util.FileExists(filename) { if !util.FileExists(filename) {
return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'") return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
} }
var cfg config.Config var cfg config.Config
err := ReadConfigFile(filename, &cfg) err := readConfigFile(filename, &cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -15,7 +15,7 @@ func TestConfig(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
cfgRead, err := Load(filename) cfgRead, err := load(filename)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return