config: defer creating the base lnd dir until all flag parsing is done

In this commit, we defer creating the base lnd directory until all flag
parsing is done. We do this as it's possible that the config file
specifies a lnddir, but it isn't actually used as the directory has
already been created.
This commit is contained in:
Wilmer Paulino 2018-08-27 22:21:01 -07:00
parent 58ab6c1912
commit fd61c3c9b0
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

View File

@ -325,23 +325,42 @@ func loadConfig() (*config, error) {
os.Exit(0)
}
// If the config file path has not been modified by the user, then we'll
// use the default config file path. However, if the user has modified
// their lnddir, then we should assume they intend to use the config
// file within it.
configFileDir := cleanAndExpandPath(preCfg.LndDir)
configFilePath := cleanAndExpandPath(preCfg.ConfigFile)
if configFileDir != defaultLndDir {
if configFilePath == defaultConfigFile {
configFilePath = filepath.Join(
configFileDir, defaultConfigFilename,
)
}
}
// Next, load any additional configuration options from the file.
var configFileError error
cfg := preCfg
if err := flags.IniParse(configFilePath, &cfg); err != nil {
configFileError = err
}
// Finally, parse the remaining command line options again to ensure
// they take precedence.
if _, err := flags.Parse(&cfg); err != nil {
return nil, err
}
// If the provided lnd directory is not the default, we'll modify the
// path to all of the files and directories that will live within it.
lndDir := cleanAndExpandPath(preCfg.LndDir)
configFilePath := cleanAndExpandPath(preCfg.ConfigFile)
lndDir := cleanAndExpandPath(cfg.LndDir)
if lndDir != defaultLndDir {
// If the config file path has not been modified by the user,
// then we'll use the default config file path. However, if the
// user has modified their lnddir, then we should assume they
// intend to use the config file within it.
if configFilePath == defaultConfigFile {
preCfg.ConfigFile = filepath.Join(lndDir, defaultConfigFilename)
}
preCfg.DataDir = filepath.Join(lndDir, defaultDataDirname)
preCfg.TLSCertPath = filepath.Join(lndDir, defaultTLSCertFilename)
preCfg.TLSKeyPath = filepath.Join(lndDir, defaultTLSKeyFilename)
preCfg.LogDir = filepath.Join(lndDir, defaultLogDirname)
preCfg.Tor.V2PrivateKeyPath = filepath.Join(lndDir, defaultTorV2PrivateKeyFilename)
cfg.DataDir = filepath.Join(lndDir, defaultDataDirname)
cfg.TLSCertPath = filepath.Join(lndDir, defaultTLSCertFilename)
cfg.TLSKeyPath = filepath.Join(lndDir, defaultTLSKeyFilename)
cfg.LogDir = filepath.Join(lndDir, defaultLogDirname)
cfg.Tor.V2PrivateKeyPath = filepath.Join(lndDir, defaultTorV2PrivateKeyFilename)
}
// Create the lnd directory if it doesn't already exist.
@ -363,19 +382,6 @@ func loadConfig() (*config, error) {
return nil, err
}
// Next, load any additional configuration options from the file.
var configFileError error
cfg := preCfg
if err := flags.IniParse(cfg.ConfigFile, &cfg); err != nil {
configFileError = err
}
// Finally, parse the remaining command line options again to ensure
// they take precedence.
if _, err := flags.Parse(&cfg); err != nil {
return nil, err
}
// As soon as we're done parsing configuration options, ensure all paths
// to directories and files are cleaned and expanded before attempting
// to use them later on.