serialize.go 2.82 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package config

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4
	"encoding/json"
5 6 7
	"errors"
	"fmt"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
8 9 10
	"io/ioutil"
	"os"
	"path"
11
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
14
// WriteFile writes the given buffer `buf` into file named `filename`.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
func WriteFile(filename string, buf []byte) error {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
16 17 18 19
	err := os.MkdirAll(path.Dir(filename), 0777)
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
21
	return ioutil.WriteFile(filename, buf, 0666)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
24
// ReadConfigFile reads the config from `filename` into `cfg`.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
func ReadConfigFile(filename string, cfg *Config) error {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
26
	buf, err := ioutil.ReadFile(filename)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27 28 29
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
31
	return json.Unmarshal(buf, cfg)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
34
// WriteConfigFile writes the config from `cfg` into `filename`.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
func WriteConfigFile(filename string, cfg *Config) error {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
36 37 38 39
	buf, err := json.MarshalIndent(cfg, "", "  ")
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
41
	return WriteFile(filename, buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
}
43 44 45 46 47 48

// WriteConfigFile writes the config from `cfg` into `filename`.
func GetValueInConfigFile(key string) (value string, err error) {
	// reading config file
	attrs := strings.Split(key, ".")

49
	filename, _ := u.TildeExpansion(defaultConfigFilePath)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return "", err
	}

	// deserializing json
	var cfg interface{}
	var exists bool

	err = json.Unmarshal(buf, &cfg)
	if err != nil {
		return "", 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
		}
	}
	return "", errors.New(fmt.Sprintf("%s is not a string", key))
}

// WriteConfigFile writes the config from `cfg` into `filename`.
func SetValueInConfigFile(key string, values []string) error {
	assignee := strings.Join(values, " ")
	attrs := strings.Split(key, ".")

86
	filename, _ := u.TildeExpansion(defaultConfigFilePath)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return err
	}

	// deserializing json
	var cfg, orig interface{}
	var exists, isMap bool
	cfgMap := make(map[string]interface{})

	err = json.Unmarshal(buf, &orig)
	cfg = orig
	if err != nil {
		return err
	}

	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], ".")))
		}
		cfg, exists = cfgMap[attrs[i]]
		if !exists {
			return errors.New(fmt.Sprintf("Configuration option key \"%s\" not recognized", strings.Join(attrs[:i+1], ".")))
		}
	}
	cfgMap[attrs[len(attrs)-1]] = assignee
	buf, err = json.MarshalIndent(orig, "", "  ")
	if err != nil {
		return err
	}
	WriteFile(filename, buf)
	return nil
}