1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 05:52:20 +08:00

move utility method

This commit is contained in:
Brian Tiger Chow
2015-01-12 18:23:29 -08:00
parent 306c7e2e7b
commit bbaf70974d
3 changed files with 15 additions and 14 deletions

View File

@ -2,9 +2,11 @@
package config
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
@ -205,3 +207,15 @@ func Marshal(value interface{}) ([]byte, error) {
// need to prettyprint, hence MarshalIndent, instead of Encoder
return json.MarshalIndent(value, "", " ")
}
func FromMap(v map[string]interface{}) (*Config, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
var conf Config
if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
return nil, fmt.Errorf("Failure to decode config: %s", err)
}
return &conf, nil
}

View File

@ -149,7 +149,7 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
if err := writeConfigFile(filename, mapconf); err != nil {
return err
}
conf, err := convertMapToConfig(mapconf)
conf, err := config.FromMap(mapconf)
if err != nil {
return err
}

View File

@ -1,7 +1,6 @@
package fsrepo
import (
"bytes"
"encoding/json"
"fmt"
"io"
@ -73,18 +72,6 @@ func encode(w io.Writer, value interface{}) error {
return err
}
func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
var conf config.Config
if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
return nil, fmt.Errorf("Failure to decode config: %s", err)
}
return &conf, nil
}
// load reads given file and returns the read config, or error.
func load(filename string) (*config.Config, error) {
// if nothing is there, fail. User must run 'ipfs init'