github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/repo/config/config.go (about) 1 // package config implements the ipfs config file datastructures and utilities. 2 package config 3 4 import ( 5 "bytes" 6 "encoding/json" 7 "fmt" 8 "os" 9 "path/filepath" 10 "strings" 11 12 u "github.com/ipfs/go-ipfs/util" 13 ) 14 15 var log = u.Logger("config") 16 17 // Config is used to load IPFS config files. 18 type Config struct { 19 Identity Identity // local node's peer identity 20 Datastore Datastore // local node's storage 21 Addresses Addresses // local node's addresses 22 Mounts Mounts // local node's mount points 23 Version Version // local node's version management 24 Discovery Discovery // local node's discovery mechanisms 25 Bootstrap []string // local nodes's bootstrap peer addresses 26 Tour Tour // local node's tour position 27 Gateway Gateway // local node's gateway server options 28 SupernodeRouting SupernodeClientConfig // local node's routing servers (if SupernodeRouting enabled) 29 API API // local node's API settings 30 Swarm SwarmConfig 31 Log Log 32 } 33 34 const ( 35 // DefaultPathName is the default config dir name 36 DefaultPathName = ".ipfs" 37 // DefaultPathRoot is the path to the default config dir location. 38 DefaultPathRoot = "~/" + DefaultPathName 39 // DefaultConfigFile is the filename of the configuration file 40 DefaultConfigFile = "config" 41 // EnvDir is the environment variable used to change the path root. 42 EnvDir = "IPFS_PATH" 43 ) 44 45 // PathRoot returns the default configuration root directory 46 func PathRoot() (string, error) { 47 dir := os.Getenv(EnvDir) 48 var err error 49 if len(dir) == 0 { 50 dir, err = u.TildeExpansion(DefaultPathRoot) 51 } 52 return dir, err 53 } 54 55 // Path returns the path `extension` relative to the configuration root. If an 56 // empty string is provided for `configroot`, the default root is used. 57 func Path(configroot, extension string) (string, error) { 58 if len(configroot) == 0 { 59 dir, err := PathRoot() 60 if err != nil { 61 return "", err 62 } 63 return filepath.Join(dir, extension), nil 64 65 } 66 return filepath.Join(configroot, extension), nil 67 } 68 69 // Filename returns the configuration file path given a configuration root 70 // directory. If the configuration root directory is empty, use the default one 71 func Filename(configroot string) (string, error) { 72 return Path(configroot, DefaultConfigFile) 73 } 74 75 // HumanOutput gets a config value ready for printing 76 func HumanOutput(value interface{}) ([]byte, error) { 77 s, ok := value.(string) 78 if ok { 79 return []byte(strings.Trim(s, "\n")), nil 80 } 81 return Marshal(value) 82 } 83 84 // Marshal configuration with JSON 85 func Marshal(value interface{}) ([]byte, error) { 86 // need to prettyprint, hence MarshalIndent, instead of Encoder 87 return json.MarshalIndent(value, "", " ") 88 } 89 90 func FromMap(v map[string]interface{}) (*Config, error) { 91 buf := new(bytes.Buffer) 92 if err := json.NewEncoder(buf).Encode(v); err != nil { 93 return nil, err 94 } 95 var conf Config 96 if err := json.NewDecoder(buf).Decode(&conf); err != nil { 97 return nil, fmt.Errorf("Failure to decode config: %s", err) 98 } 99 return &conf, nil 100 } 101 102 func ToMap(conf *Config) (map[string]interface{}, error) { 103 buf := new(bytes.Buffer) 104 if err := json.NewEncoder(buf).Encode(conf); err != nil { 105 return nil, err 106 } 107 var m map[string]interface{} 108 if err := json.NewDecoder(buf).Decode(&m); err != nil { 109 return nil, fmt.Errorf("Failure to decode config: %s", err) 110 } 111 return m, nil 112 }