Add support for CLI option --conf.

This commit is contained in:
Arthur Britto
2012-06-11 20:54:05 -07:00
parent 72660369de
commit 253df289c3
3 changed files with 32 additions and 20 deletions

View File

@@ -33,7 +33,7 @@
Config theConfig; Config theConfig;
Config::Config() void Config::setup(const std::string& strConf)
{ {
boost::system::error_code ec; boost::system::error_code ec;
@@ -45,12 +45,21 @@ Config::Config()
CONFIG_DIR = boost::filesystem::current_path(); CONFIG_DIR = boost::filesystem::current_path();
CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME; CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME;
DATA_DIR = CONFIG_DIR / "db";
if (exists(CONFIG_FILE) if (!strConf.empty())
{
// --conf=<path> : everything is relative that file.
CONFIG_FILE = strConf;
CONFIG_DIR = CONFIG_FILE;
CONFIG_DIR.remove_filename();
DATA_DIR = CONFIG_DIR / "db";
}
else if (exists(CONFIG_FILE)
|| (!getenv("HOME") && !getenv("XDG_CONFIG_HOME"))) || (!getenv("HOME") && !getenv("XDG_CONFIG_HOME")))
{ {
// Current place is fine, put dbs in a subdir. // Current working directory is fine, put dbs in a subdir.
DATA_DIR = CONFIG_DIR / "db"; nothing();
} }
else else
{ {
@@ -66,21 +75,20 @@ Config::Config()
strXdgConfigHome = str(boost::format("%s/.config") % strHome); strXdgConfigHome = str(boost::format("%s/.config") % strHome);
} }
CONFIG_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgConfigHome);
boost::filesystem::create_directories(CONFIG_DIR, ec);
if (ec)
throw std::runtime_error(str(boost::format("Can not create %s") % CONFIG_DIR));
if (strXdgDataHome.empty()) if (strXdgDataHome.empty())
{ {
// $XDG_DATA_HOME was not set, use default based on $HOME. // $XDG_DATA_HOME was not set, use default based on $HOME.
strXdgDataHome = str(boost::format("%s/.local/share") % strHome); strXdgDataHome = str(boost::format("%s/.local/share") % strHome);
} }
CONFIG_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgConfigHome);
CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME;
DATA_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgDataHome); DATA_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgDataHome);
boost::filesystem::create_directories(CONFIG_DIR, ec);
if (ec)
throw std::runtime_error(str(boost::format("Can not create %s") % CONFIG_DIR));
} }
boost::filesystem::create_directories(DATA_DIR, ec); boost::filesystem::create_directories(DATA_DIR, ec);
@@ -88,6 +96,7 @@ Config::Config()
if (ec) if (ec)
throw std::runtime_error(str(boost::format("Can not create %s") % DATA_DIR)); throw std::runtime_error(str(boost::format("Can not create %s") % DATA_DIR));
std::cerr << "CONFIG FILE: " << CONFIG_FILE << std::endl;
std::cerr << "CONFIG DIR: " << CONFIG_DIR << std::endl; std::cerr << "CONFIG DIR: " << CONFIG_DIR << std::endl;
std::cerr << "DATA DIR: " << DATA_DIR << std::endl; std::cerr << "DATA DIR: " << DATA_DIR << std::endl;
@@ -97,8 +106,6 @@ Config::Config()
VERSION = 1; VERSION = 1;
CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME;
NETWORK_START_TIME = 1319844908; NETWORK_START_TIME = 1319844908;
PEER_PORT = SYSTEM_PEER_PORT; PEER_PORT = SYSTEM_PEER_PORT;
@@ -128,15 +135,17 @@ Config::Config()
FEE_DEFAULT = DEFAULT_FEE_DEFAULT; FEE_DEFAULT = DEFAULT_FEE_DEFAULT;
ACCOUNT_PROBE_MAX = 10; ACCOUNT_PROBE_MAX = 10;
load();
} }
void Config::load() void Config::load()
{ {
std::ifstream ifsConfig(CONFIG_FILE_NAME, std::ios::in); std::ifstream ifsConfig(CONFIG_FILE.c_str(), std::ios::in);
if (!ifsConfig) if (!ifsConfig)
{ {
std::cerr << "Failed to open '" CONFIG_FILE_NAME "'." << std::endl; std::cerr << "Failed to open '" << CONFIG_FILE << "'." << std::endl;
} }
else else
{ {
@@ -147,7 +156,7 @@ void Config::load()
if (ifsConfig.bad()) if (ifsConfig.bad())
{ {
std::cerr << "Failed to read '" CONFIG_FILE_NAME "'." << std::endl; std::cerr << "Failed to read '" << CONFIG_FILE << "'." << std::endl;
} }
else else
{ {

View File

@@ -89,8 +89,7 @@ public:
// Client behavior // Client behavior
int ACCOUNT_PROBE_MAX; // How far to scan for accounts. int ACCOUNT_PROBE_MAX; // How far to scan for accounts.
Config(); void setup(const std::string& strConf);
void load(); void load();
}; };

View File

@@ -82,7 +82,7 @@ int main(int argc, char* argv[])
po::options_description desc("Options"); po::options_description desc("Options");
desc.add_options() desc.add_options()
("help,h", "Display this message.") ("help,h", "Display this message.")
("conf", "Specify the configuration file.") ("conf", po::value<std::string>(), "Specify the configuration file.")
("rpc", "Perform rpc command (default).") ("rpc", "Perform rpc command (default).")
("test,t", "Perform unit tests.") ("test,t", "Perform unit tests.")
("parameters", po::value< vector<string> >(), "Specify comma separated parameters.") ("parameters", po::value< vector<string> >(), "Specify comma separated parameters.")
@@ -95,7 +95,6 @@ int main(int argc, char* argv[])
// //
// Prepare to run // Prepare to run
// //
theConfig.load();
if (!AddSystemEntropy()) if (!AddSystemEntropy())
{ {
@@ -127,6 +126,11 @@ int main(int argc, char* argv[])
} }
} }
if (!iResult)
{
theConfig.setup(vm.count("conf") ? vm["conf"].as<std::string>() : "");
}
if (iResult) if (iResult)
{ {
nothing(); nothing();