@cryptotaxi247 / kubo / commits / 47e4a35e6

refactor(repo): privatize serialization methods

Brian Tiger Chow committed Jan 12, 2015 at 17:50 UTC 47e4a35e6b841fc23a6b9c1992d00d9d1fa1bdea
3 files changed +15 -15
repo/fsrepo/fsrepo.go
+1 -1
@@ -58,7 +58,7 @@ func (r *FSRepo) Open() error {
58 if err != nil {
59 return err
60 }
61 - conf, err := Load(configFilename)
61 + conf, err := load(configFilename)
62 if err != nil {
63 return err
64 }
repo/fsrepo/serialize.go
+13 -13
@@ -17,8 +17,8 @@ import (
17
18 var log = util.Logger("fsrepo")
19
20 -// ReadConfigFile reads the config from `filename` into `cfg`.
21 -func ReadConfigFile(filename string, cfg interface{}) error {
20 +// readConfigFile reads the config from `filename` into `cfg`.
21 +func readConfigFile(filename string, cfg interface{}) error {
22 f, err := os.Open(filename)
23 if err != nil {
24 return err
@@ -30,7 +30,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
30 return nil
31 }
32
33 -// WriteConfigFile writes the config from `cfg` into `filename`.
33 +// writeConfigFile writes the config from `cfg` into `filename`.
34 func writeConfigFile(filename string, cfg interface{}) error {
35 err := os.MkdirAll(filepath.Dir(filename), 0775)
36 if err != nil {
@@ -43,11 +43,11 @@ func writeConfigFile(filename string, cfg interface{}) error {
43 }
44 defer f.Close()
45
46 - return Encode(f, cfg)
46 + return encode(f, cfg)
47 }
48
49 -// WriteFile writes the buffer at filename
50 -func WriteFile(filename string, buf []byte) error {
49 +// writeFile writes the buffer at filename
50 +func writeFile(filename string, buf []byte) error {
51 err := os.MkdirAll(filepath.Dir(filename), 0775)
52 if err != nil {
53 return err
@@ -63,8 +63,8 @@ func WriteFile(filename string, buf []byte) error {
63 return err
64 }
65
66 -// Encode configuration with JSON
67 -func Encode(w io.Writer, value interface{}) error {
66 +// encode configuration with JSON
67 +func encode(w io.Writer, value interface{}) error {
68 // need to prettyprint, hence MarshalIndent, instead of Encoder
69 buf, err := config.Marshal(value)
70 if err != nil {
@@ -81,7 +81,7 @@ func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
81 return nil, err
82 }
83 var cfg map[string]interface{}
84 - if err := ReadConfigFile(filename, &cfg); err != nil {
84 + if err := readConfigFile(filename, &cfg); err != nil {
85 return nil, err
86 }
87
@@ -95,7 +95,7 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
95 return err
96 }
97 var mapconf map[string]interface{}
98 - if err := ReadConfigFile(filename, &mapconf); err != nil {
98 + if err := readConfigFile(filename, &mapconf); err != nil {
99 return err
100 }
101 if err := common.MapSetKV(mapconf, key, value); err != nil {
@@ -125,15 +125,15 @@ func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
125 return &conf, nil
126 }
127
128 -// Load reads given file and returns the read config, or error.
129 -func Load(filename string) (*config.Config, error) {
128 +// load reads given file and returns the read config, or error.
129 +func load(filename string) (*config.Config, error) {
130 // if nothing is there, fail. User must run 'ipfs init'
131 if !util.FileExists(filename) {
132 return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
133 }
134
135 var cfg config.Config
136 - err := ReadConfigFile(filename, &cfg)
136 + err := readConfigFile(filename, &cfg)
137 if err != nil {
138 return nil, err
139 }
repo/fsrepo/serialize_test.go
+1 -1
@@ -15,7 +15,7 @@ func TestConfig(t *testing.T) {
15 if err != nil {
16 t.Error(err)
17 }
18 - cfgRead, err := Load(filename)
18 + cfgRead, err := load(filename)
19 if err != nil {
20 t.Error(err)
21 return