@cryptotaxi247 / kubo / commits / 3986af769

refactor(repo): move public methods

Brian Tiger Chow committed Jan 12, 2015 at 17:53 UTC 3986af769194d83638e101584bc80d898c9a289a
2 files changed +39 -42
repo/fsrepo/fsrepo.go
+39 -2
@@ -6,10 +6,11 @@ import (
6 "os"
7 "path/filepath"
8
9 - "github.com/jbenet/go-ipfs/repo"
9 + repo "github.com/jbenet/go-ipfs/repo"
10 + common "github.com/jbenet/go-ipfs/repo/common"
11 config "github.com/jbenet/go-ipfs/repo/config"
12 util "github.com/jbenet/go-ipfs/util"
12 - "github.com/jbenet/go-ipfs/util/debugerror"
13 + debugerror "github.com/jbenet/go-ipfs/util/debugerror"
14 )
15
16 type FSRepo struct {
@@ -107,6 +108,42 @@ func (r *FSRepo) SetConfig(conf *config.Config) error {
108 return nil
109 }
110
111 +// GetConfigKey retrieves only the value of a particular key
112 +func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
113 + filename, err := config.Filename(r.path)
114 + if err != nil {
115 + return nil, err
116 + }
117 + var cfg map[string]interface{}
118 + if err := readConfigFile(filename, &cfg); err != nil {
119 + return nil, err
120 + }
121 + return common.MapGetKV(cfg, key)
122 +}
123 +
124 +// SetConfigKey writes the value of a particular key
125 +func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
126 + filename, err := config.Filename(r.path)
127 + if err != nil {
128 + return err
129 + }
130 + var mapconf map[string]interface{}
131 + if err := readConfigFile(filename, &mapconf); err != nil {
132 + return err
133 + }
134 + if err := common.MapSetKV(mapconf, key, value); err != nil {
135 + return err
136 + }
137 + if err := writeConfigFile(filename, mapconf); err != nil {
138 + return err
139 + }
140 + conf, err := convertMapToConfig(mapconf)
141 + if err != nil {
142 + return err
143 + }
144 + return r.SetConfig(conf)
145 +}
146 +
147 func (r *FSRepo) Close() error {
148 if r.state != opened {
149 return debugerror.Errorf("repo is %s", r.state)
repo/fsrepo/serialize.go
-40
@@ -9,7 +9,6 @@ import (
9 "path/filepath"
10 "time"
11
12 - common "github.com/jbenet/go-ipfs/repo/common"
12 "github.com/jbenet/go-ipfs/repo/config"
13 "github.com/jbenet/go-ipfs/util"
14 "github.com/jbenet/go-ipfs/util/debugerror"
@@ -74,45 +73,6 @@ func encode(w io.Writer, value interface{}) error {
73 return err
74 }
75
77 -// GetConfigKey retrieves only the value of a particular key
78 -func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
79 - filename, err := config.Filename(r.path)
80 - if err != nil {
81 - return nil, err
82 - }
83 - var cfg map[string]interface{}
84 - if err := readConfigFile(filename, &cfg); err != nil {
85 - return nil, err
86 - }
87 -
88 - return common.MapGetKV(cfg, key)
89 -}
90 -
91 -// SetConfigKey writes the value of a particular key
92 -func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
93 - filename, err := config.Filename(r.path)
94 - if err != nil {
95 - return err
96 - }
97 - var mapconf map[string]interface{}
98 - if err := readConfigFile(filename, &mapconf); err != nil {
99 - return err
100 - }
101 - if err := common.MapSetKV(mapconf, key, value); err != nil {
102 - return err
103 - }
104 - // must use raw method because there may exist keys not present in the *config.Config struct
105 - if err := writeConfigFile(filename, mapconf); err != nil {
106 - return err
107 - }
108 - conf, err := convertMapToConfig(mapconf)
109 - if err != nil {
110 - return err
111 - }
112 - *r.config = *conf // copy so caller cannot modify the private config
113 - return nil
114 -}
115 -
76 func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
77 var buf bytes.Buffer
78 if err := json.NewEncoder(&buf).Encode(v); err != nil {