search for yaml config file

In order:
1) cmdline opt
2) in-tree path
3) /etc/digwatch.yaml
This commit is contained in:
Henri DF 2016-04-12 16:01:52 -07:00
parent 73ec593931
commit 42de0507fa
2 changed files with 49 additions and 3 deletions

View File

@ -4,7 +4,8 @@
#define DIGWATCH_LUA_DIR "/usr/share/digwatch/lua/"
#define DIGWATCH_SOURCE_DIR "${PROJECT_SOURCE_DIR}"
#define DIGWATCH_CONF_FILE "${PROJECT_SOURCE_DIR}/digwatch.yaml"
#define DIGWATCH_SOURCE_CONF_FILE "${PROJECT_SOURCE_DIR}/digwatch.yaml"
#define DIGWATCH_INSTALL_CONF_FILE "/etc/digwatch.yaml"
#define DIGWATCH_SOURCE_LUA_DIR "${PROJECT_SOURCE_DIR}/userspace/digwatch/lua/"

View File

@ -173,6 +173,7 @@ int digwatch_init(int argc, char **argv)
string lua_main_filename;
string output_name = "stdout";
string infile;
string conf_filename;
string lua_dir = DIGWATCH_LUA_DIR;
lua_State* ls = NULL;
@ -192,7 +193,7 @@ int digwatch_init(int argc, char **argv)
// Parse the args
//
while((op = getopt_long(argc, argv,
"ho:r:",
"c:ho:R:",
long_options, &long_index)) != -1)
{
switch(op)
@ -200,6 +201,9 @@ int digwatch_init(int argc, char **argv)
case 'h':
usage();
goto exit;
case 'c':
conf_filename = optarg;
break;
case 'o':
valid = std::find(valid_output_names.begin(), valid_output_names.end(), optarg) != valid_output_names.end();
if (!valid)
@ -250,8 +254,49 @@ int digwatch_init(int argc, char **argv)
}
ifstream* conf_stream;
if (conf_filename.size())
{
conf_stream = new ifstream(conf_filename);
if (!conf_stream->good())
{
fprintf(stderr, "Could not find configuration file at %s \n", conf_filename.c_str());
result = EXIT_FAILURE;
goto exit;
}
}
else
{
conf_stream = new ifstream(DIGWATCH_SOURCE_CONF_FILE);
if (conf_stream->good())
{
conf_filename = DIGWATCH_SOURCE_CONF_FILE;
}
else
{
conf_stream = new ifstream(DIGWATCH_INSTALL_CONF_FILE);
if (conf_stream->good())
{
conf_filename = DIGWATCH_INSTALL_CONF_FILE;
}
else
{
conf_filename = "";
}
}
}
digwatch_configuration config;
config.init();
if (conf_filename.size())
{
cout << "Using configuration file " + conf_filename + "\n";
config.init(conf_filename);
}
else
{
cout << "No configuration file found, proceeding with defaults\n";
config.init();
}
if(signal(SIGINT, signal_callback) == SIG_ERR)
{