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.
上级 73077d1f
...@@ -44,8 +44,12 @@ func init() { ...@@ -44,8 +44,12 @@ func init() {
func configCmd(c *commander.Command, inp []string) error { func configCmd(c *commander.Command, inp []string) error {
// todo: implement --config filename flag. confdir, err := getConfigDir(c.Parent)
filename, err := config.Filename("") if err != nil {
return err
}
filename, err := config.GetConfigFilePath(confdir)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -37,15 +37,9 @@ func initCmd(c *commander.Command, inp []string) error { ...@@ -37,15 +37,9 @@ func initCmd(c *commander.Command, inp []string) error {
if err != nil { if err != nil {
return err return err
} }
if configpath == "" {
configpath, err = u.TildeExpansion("~/.go-ipfs")
if err != nil {
return err
}
}
u.POut("initializing ipfs node at %s\n", configpath) u.POut("initializing ipfs node at %s\n", configpath)
filename, err := config.Filename(configpath + "/config") filename, err := config.GetConfigFilePath(configpath)
if err != nil { if err != nil {
return errors.New("Couldn't get home directory path") return errors.New("Couldn't get home directory path")
} }
...@@ -69,10 +63,11 @@ func initCmd(c *commander.Command, inp []string) error { ...@@ -69,10 +63,11 @@ func initCmd(c *commander.Command, inp []string) error {
cfg.Datastore = config.Datastore{} cfg.Datastore = config.Datastore{}
if len(dspath) == 0 { if len(dspath) == 0 {
dspath, err = u.TildeExpansion("~/.go-ipfs/datastore") dspath, err = config.GetDefaultPathRoot()
if err != nil { if err != nil {
return err return err
} }
dspath = dspath + "/datastore"
} }
cfg.Datastore.Path = dspath cfg.Datastore.Path = dspath
cfg.Datastore.Type = "leveldb" cfg.Datastore.Type = "leveldb"
...@@ -122,12 +117,7 @@ func initCmd(c *commander.Command, inp []string) error { ...@@ -122,12 +117,7 @@ func initCmd(c *commander.Command, inp []string) error {
}, },
} }
path, err := u.TildeExpansion(config.DefaultConfigFilePath) err = config.WriteConfigFile(filename, cfg)
if err != nil {
return err
}
err = config.WriteConfigFile(path, cfg)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -53,7 +53,11 @@ Use "ipfs help <command>" for more information about a command. ...@@ -53,7 +53,11 @@ Use "ipfs help <command>" for more information about a command.
} }
func init() { 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 { func ipfsCmd(c *commander.Command, args []string) error {
...@@ -74,7 +78,12 @@ func main() { ...@@ -74,7 +78,12 @@ func main() {
} }
func localNode(confdir string, online bool) (*core.IpfsNode, error) { 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 { if err != nil {
return nil, err return nil, err
} }
...@@ -83,16 +92,19 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) { ...@@ -83,16 +92,19 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
} }
// Gets the config "-c" flag from the command, or returns // 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) { func getConfigDir(c *commander.Command) (string, error) {
conf := c.Flag.Lookup("c").Value.Get() conf := c.Flag.Lookup("c").Value.Get()
if conf == nil { if conf == nil {
return "", nil return config.GetDefaultPathRoot()
} }
confStr, ok := conf.(string) confStr, ok := conf.(string)
if !ok { if !ok {
return "", errors.New("failed to retrieve config flag value.") return "", errors.New("failed to retrieve config flag value.")
} }
if len(confStr) == 0 {
return config.GetDefaultPathRoot()
}
return u.TildeExpansion(confStr) return u.TildeExpansion(confStr)
} }
...@@ -42,11 +42,26 @@ type Config struct { ...@@ -42,11 +42,26 @@ type Config struct {
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
} }
// DefaultPathRoot is the default parth for the IPFS node's root dir. // Return the default configuration root directory
const DefaultPathRoot = "~/.go-ipfs" 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. // Return the configuration file path given a configuration root directory
const DefaultConfigFilePath = DefaultPathRoot + "/config" // 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 // DecodePrivateKey is a helper to decode the users PrivateKey
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) { func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
...@@ -60,30 +75,15 @@ 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) 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. // Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, 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 nothing is there, fail. User must run 'ipfs init'
if _, err := os.Stat(filename); os.IsNotExist(err) { if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, errors.New("ipfs not initialized, please run 'ipfs init'") return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
} }
var cfg Config var cfg Config
err = ReadConfigFile(filename, &cfg) err := ReadConfigFile(filename, &cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论