Refactor config file location, add IPFS_CONFIG_DIR

Removed config.Filename and replace it with config.GetConfigFilePath that
takes a configuration directory as argument instead. Makes code simpler.
ipfs.getConfigDir now also return the default configuration dir instead of
an empty string in some cases.
parent 73077d1f
......@@ -44,8 +44,12 @@ func init() {
func configCmd(c *commander.Command, inp []string) error {
// todo: implement --config filename flag.
filename, err := config.Filename("")
confdir, err := getConfigDir(c.Parent)
if err != nil {
return err
}
filename, err := config.GetConfigFilePath(confdir)
if err != nil {
return err
}
......
......@@ -37,15 +37,9 @@ func initCmd(c *commander.Command, inp []string) error {
if err != nil {
return err
}
if configpath == "" {
configpath, err = u.TildeExpansion("~/.go-ipfs")
if err != nil {
return err
}
}
u.POut("initializing ipfs node at %s\n", configpath)
filename, err := config.Filename(configpath + "/config")
filename, err := config.GetConfigFilePath(configpath)
if err != nil {
return errors.New("Couldn't get home directory path")
}
......@@ -69,10 +63,11 @@ func initCmd(c *commander.Command, inp []string) error {
cfg.Datastore = config.Datastore{}
if len(dspath) == 0 {
dspath, err = u.TildeExpansion("~/.go-ipfs/datastore")
dspath, err = config.GetDefaultPathRoot()
if err != nil {
return err
}
dspath = dspath + "/datastore"
}
cfg.Datastore.Path = dspath
cfg.Datastore.Type = "leveldb"
......@@ -122,12 +117,7 @@ func initCmd(c *commander.Command, inp []string) error {
},
}
path, err := u.TildeExpansion(config.DefaultConfigFilePath)
if err != nil {
return err
}
err = config.WriteConfigFile(path, cfg)
err = config.WriteConfigFile(filename, cfg)
if err != nil {
return err
}
......
......@@ -53,7 +53,11 @@ Use "ipfs help <command>" for more information about a command.
}
func init() {
CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
config, err := config.GetDefaultPathRoot()
if err != nil {
config = ""
}
CmdIpfs.Flag.String("c", config, "specify config directory")
}
func ipfsCmd(c *commander.Command, args []string) error {
......@@ -74,7 +78,12 @@ func main() {
}
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
cfg, err := config.Load(confdir + "/config")
filename, err := config.GetConfigFilePath(confdir)
if err != nil {
return nil, err
}
cfg, err := config.Load(filename)
if err != nil {
return nil, err
}
......@@ -83,16 +92,19 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
}
// Gets the config "-c" flag from the command, or returns
// the empty string
// the default configuration root directory
func getConfigDir(c *commander.Command) (string, error) {
conf := c.Flag.Lookup("c").Value.Get()
if conf == nil {
return "", nil
return config.GetDefaultPathRoot()
}
confStr, ok := conf.(string)
if !ok {
return "", errors.New("failed to retrieve config flag value.")
}
if len(confStr) == 0 {
return config.GetDefaultPathRoot()
}
return u.TildeExpansion(confStr)
}
......@@ -42,11 +42,26 @@ type Config struct {
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
}
// DefaultPathRoot is the default parth for the IPFS node's root dir.
const DefaultPathRoot = "~/.go-ipfs"
// Return the default configuration root directory
func GetDefaultPathRoot() (string, error) {
dir := os.Getenv("IPFS_CONFIG_DIR")
var err error
if len(dir) == 0 {
dir, err = u.TildeExpansion("~/.go-ipfs")
}
return dir, err
}
// DefaultConfigFilePath points to the ipfs node config file.
const DefaultConfigFilePath = DefaultPathRoot + "/config"
// Return the configuration file path given a configuration root directory
// If the configuration root directory is empty, use the default one
func GetConfigFilePath(configroot string) (string, error) {
if len(configroot) == 0 {
dir, err := GetDefaultPathRoot()
return dir+"/config", err
} else {
return configroot+"/config", nil
}
}
// DecodePrivateKey is a helper to decode the users PrivateKey
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
......@@ -60,30 +75,15 @@ func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error
return x509.ParsePKCS1PrivateKey(pkb)
}
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
if len(filename) == 0 {
filename = DefaultConfigFilePath
}
// tilde expansion on config file
return u.TildeExpansion(filename)
}
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
filename, err := Filename(filename)
if err != nil {
return nil, err
}
// if nothing is there, fail. User must run 'ipfs init'
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
}
var cfg Config
err = ReadConfigFile(filename, &cfg)
err := ReadConfigFile(filename, &cfg)
if err != nil {
return nil, err
}
......
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