Commit 80323eba authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

config: redo to use other serialization functions

parent bf7cb198
......@@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"github.com/gonuts/flag"
"github.com/jbenet/commander"
config "github.com/jbenet/go-ipfs/config"
......@@ -67,19 +68,28 @@ func configCmd(c *commander.Command, inp []string) error {
// Getter (1 param)
if len(inp) == 1 {
value, err := config.GetValueInConfigFile(inp[0])
value, err := config.ReadConfigKey(filename, inp[0])
if err != nil {
return errors.New("Failed to get config value: " + err.Error())
return fmt.Errorf("Failed to get config value: %s", err)
}
u.POut(value + "\n")
strval, ok := value.(string)
if ok {
u.POut("%s\n", strval)
return nil
}
if err := config.Encode(os.Stdout, value); err != nil {
return fmt.Errorf("Failed to encode config value: %s", err)
}
u.POut("\n")
return nil
}
// Setter (>1 params)
err = config.SetValueInConfigFile(inp[0], inp[1:])
err = config.WriteConfigKey(filename, inp[0], inp[1])
if err != nil {
return errors.New("Failed to set config value: " + err.Error())
return fmt.Errorf("Failed to set config value: %s", err)
}
return nil
......
......@@ -67,7 +67,7 @@ func main() {
func localNode() (*core.IpfsNode, error) {
//todo implement config file flag
cfg, err := config.LoadConfig("")
cfg, err := config.ConfigLoad("")
if err != nil {
return nil, err
}
......
......@@ -32,6 +32,7 @@ var defaultConfigFile = `{
}
`
// ConfigFilename returns the proper tilde expanded config filename.
func ConfigFilename(filename string) (string, error) {
if len(filename) == 0 {
filename = defaultConfigFilePath
......@@ -41,8 +42,8 @@ func ConfigFilename(filename string) (string, error) {
return u.TildeExpansion(filename)
}
// LoadConfig reads given file and returns the read config, or error.
func LoadConfig(filename string) (*Config, error) {
// ConfigLoad reads given file and returns the read config, or error.
func ConfigLoad(filename string) (*Config, error) {
filename, err := ConfigFilename(filename)
if err != nil {
return nil, err
......@@ -67,3 +68,8 @@ func LoadConfig(filename string) (*Config, error) {
return &cfg, err
}
// Set sets the value of a particular config key
func Set(filename, key, value string) error {
return nil
}
......@@ -7,7 +7,7 @@ import (
func TestConfig(t *testing.T) {
cfg, err := LoadConfig("")
cfg, err := ConfigLoad("")
if err != nil {
t.Error(err)
return
......
......@@ -2,120 +2,114 @@ package config
import (
"encoding/json"
"errors"
"fmt"
u "github.com/jbenet/go-ipfs/util"
"io/ioutil"
"io"
"os"
"path"
"strings"
)
// WriteFile writes the given buffer `buf` into file named `filename`.
func WriteFile(filename string, buf []byte) error {
err := os.MkdirAll(path.Dir(filename), 0777)
// ReadConfigFile reads the config from `filename` into `cfg`.
func ReadConfigFile(filename string, cfg interface{}) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
return ioutil.WriteFile(filename, buf, 0666)
return Decode(f, cfg)
}
// ReadConfigFile reads the config from `filename` into `cfg`.
func ReadConfigFile(filename string, cfg *Config) error {
buf, err := ioutil.ReadFile(filename)
// WriteConfigFile writes the config from `cfg` into `filename`.
func WriteConfigFile(filename string, cfg interface{}) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return json.Unmarshal(buf, cfg)
return Encode(f, cfg)
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func WriteConfigFile(filename string, cfg *Config) error {
buf, err := json.MarshalIndent(cfg, "", " ")
// WriteFile writes the buffer at filename
func WriteFile(filename string, buf []byte) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return WriteFile(filename, buf)
_, err = f.Write(buf)
return err
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func GetValueInConfigFile(key string) (value string, err error) {
// reading config file
attrs := strings.Split(key, ".")
filename, _ := u.TildeExpansion(defaultConfigFilePath)
buf, err := ioutil.ReadFile(filename)
// Encode configuration with JSON
func Encode(w io.Writer, value interface{}) error {
// need to prettyprint, hence MarshalIndent, instead of Encoder
buf, err := json.MarshalIndent(value, "", " ")
if err != nil {
return "", err
return err
}
// deserializing json
var cfg interface{}
var exists bool
_, err = w.Write(buf)
return err
}
err = json.Unmarshal(buf, &cfg)
if err != nil {
return "", err
// Decode configuration with JSON
func Decode(r io.Reader, value interface{}) error {
return json.NewDecoder(r).Decode(value)
}
// ReadConfigKey retrieves only the value of a particular key
func ReadConfigKey(filename, key string) (interface{}, error) {
var cfg interface{}
if err := ReadConfigFile(filename, &cfg); err != nil {
return nil, err
}
for i := range attrs {
cfgMap, isMap := cfg.(map[string]interface{})
if !isMap {
return "", errors.New(fmt.Sprintf("%s has no attributes", strings.Join(attrs[:i], ".")))
}
cfg, exists = cfgMap[attrs[i]]
if !exists {
return "", errors.New(fmt.Sprintf("Configuration option key \"%s\" not recognized", strings.Join(attrs[:i+1], ".")))
}
val, is_string := cfg.(string)
if is_string {
return val, nil
var ok bool
cursor := cfg
parts := strings.Split(key, ".")
for i, part := range parts {
cursor, ok = cursor.(map[string]interface{})[part]
if !ok {
sofar := strings.Join(parts[:i], ".")
return nil, fmt.Errorf("%s key has no attributes", sofar)
}
}
return "", errors.New(fmt.Sprintf("%s is not a string", key))
return cursor, nil
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func SetValueInConfigFile(key string, values []string) error {
assignee := strings.Join(values, " ")
attrs := strings.Split(key, ".")
filename, _ := u.TildeExpansion(defaultConfigFilePath)
buf, err := ioutil.ReadFile(filename)
if err != nil {
// WriteConfigKey writes the value of a particular key
func WriteConfigKey(filename, key string, value interface{}) error {
var cfg interface{}
if err := ReadConfigFile(filename, &cfg); err != nil {
return err
}
// deserializing json
var cfg, orig interface{}
var exists, isMap bool
cfgMap := make(map[string]interface{})
var ok bool
var mcursor map[string]interface{}
cursor := cfg
err = json.Unmarshal(buf, &orig)
cfg = orig
if err != nil {
return err
}
parts := strings.Split(key, ".")
for i, part := range parts {
mcursor, ok = cursor.(map[string]interface{})
if !ok {
sofar := strings.Join(parts[:i], ".")
return fmt.Errorf("%s key is not a map", sofar)
}
for i := 0; i < len(attrs); i++ {
cfgMap, isMap = cfg.(map[string]interface{})
// curs = append(curs, cfgMap)
if !isMap {
return errors.New(fmt.Sprintf("%s has no attributes", strings.Join(attrs[:i], ".")))
// last part? set here
if i == (len(parts) - 1) {
mcursor[part] = value
break
}
cfg, exists = cfgMap[attrs[i]]
if !exists {
return errors.New(fmt.Sprintf("Configuration option key \"%s\" not recognized", strings.Join(attrs[:i+1], ".")))
cursor, ok = mcursor[part]
if !ok { // create map if this is empty
mcursor[part] = map[string]interface{}{}
cursor = mcursor[part]
}
}
cfgMap[attrs[len(attrs)-1]] = assignee
buf, err = json.MarshalIndent(orig, "", " ")
if err != nil {
return err
}
WriteFile(filename, buf)
return nil
return WriteConfigFile(filename, cfg)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment