Improve config behaviour
This commit is contained in:
parent
5796f88e05
commit
0c13c03a73
11 changed files with 2571 additions and 37 deletions
15
src/cli.c
15
src/cli.c
|
@ -5,25 +5,29 @@
|
|||
#include <argparse.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <cli.h>
|
||||
#include <logger.h>
|
||||
#include <helpers.h>
|
||||
#include <version.h>
|
||||
|
||||
cli_t *global_cli;
|
||||
|
||||
static const char *const usage[] = {
|
||||
"core [options]",
|
||||
NULL,
|
||||
};
|
||||
|
||||
void
|
||||
cli_parse(int argc, const char **argv, config_t *config)
|
||||
cli_parse(int argc, const char **argv, cli_t *cli)
|
||||
{
|
||||
const char *config_file = NULL;
|
||||
cli->config_file = NULL;
|
||||
|
||||
int version = 0;
|
||||
struct argparse_option options[] =
|
||||
{
|
||||
OPT_HELP(),
|
||||
OPT_GROUP("Basic options"),
|
||||
OPT_STRING('c', "config", &config_file, "path to config file", NULL, 0, OPT_NONEG),
|
||||
OPT_STRING('c', "config", &cli->config_file, "path to config file", NULL, 0, OPT_NONEG),
|
||||
OPT_BOOLEAN('v', "version", &version, "print version", NULL, 0, OPT_NONEG),
|
||||
OPT_END(),
|
||||
};
|
||||
|
@ -37,11 +41,6 @@ cli_parse(int argc, const char **argv, config_t *config)
|
|||
);
|
||||
argparse_parse(&argparse, argc, argv);
|
||||
|
||||
if(config_file)
|
||||
{
|
||||
config_load_string(&config->file, config_file);
|
||||
}
|
||||
|
||||
if(version)
|
||||
{
|
||||
printf("%s\n", EMGAUWA_CORE_VERSION);
|
||||
|
|
80
src/config.c
80
src/config.c
|
@ -200,7 +200,6 @@ config_init()
|
|||
{
|
||||
global_config = calloc(1, sizeof(config_t));
|
||||
|
||||
config_load_string(&global_config->file, DEFAULT_CONFIG_PATH);
|
||||
|
||||
global_config->ports.discovery = DEFAULT_DISCOVERY_PORT;
|
||||
global_config->ports.mqtt = DEFAULT_MQTT_PORT;
|
||||
|
@ -222,7 +221,6 @@ config_init()
|
|||
void
|
||||
config_free()
|
||||
{
|
||||
free(global_config->file);
|
||||
free(global_config->include);
|
||||
free(global_config->database);
|
||||
free(global_config->user);
|
||||
|
@ -251,23 +249,64 @@ config_load_string(char **holder, const char *value)
|
|||
*holder = new_holder;
|
||||
}
|
||||
|
||||
int
|
||||
config_load(config_t *config)
|
||||
static int
|
||||
config_try_file(const char *path)
|
||||
{
|
||||
if(access(path, F_OK) != 0)
|
||||
{
|
||||
LOGGER_DEBUG("'%s' does not exist\n", path);
|
||||
return 1;
|
||||
}
|
||||
if(access(path, R_OK) != 0)
|
||||
{
|
||||
LOGGER_DEBUG("no read access for '%s'\n", path);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
config_load(config_t *config, const char *cli_config_file)
|
||||
{
|
||||
if(cli_config_file)
|
||||
{
|
||||
if(config_try_file(cli_config_file) == 0)
|
||||
{
|
||||
config_load_file(config, cli_config_file);
|
||||
return;
|
||||
}
|
||||
LOGGER_CRIT("unable to open the passed config file '%s'\n", cli_config_file);
|
||||
exit(1);
|
||||
}
|
||||
if(config_try_file(DEFAULT_CONFIG_PATH) == 0)
|
||||
{
|
||||
config_load_file(config, DEFAULT_CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
if(config_try_file(DEFAULT_GLOBAL_CONFIG_PATH) == 0)
|
||||
{
|
||||
config_load_file(config, DEFAULT_GLOBAL_CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
config_load_file(config_t *config, const char *file_name)
|
||||
{
|
||||
FILE *fp;
|
||||
toml_table_t* config_toml;
|
||||
char errbuf[256];
|
||||
|
||||
/* Open the file and parse content */
|
||||
fp = fopen(global_config->file, "r");
|
||||
fp = fopen(file_name, "r");
|
||||
if(fp == NULL) {
|
||||
LOGGER_CRIT("unable to open config file '%s'\n", global_config->file);
|
||||
LOGGER_CRIT("unable to open config file '%s'\n", file_name);
|
||||
exit(1);
|
||||
}
|
||||
config_toml = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
||||
fclose(fp);
|
||||
if(config_toml == NULL) {
|
||||
LOGGER_CRIT("unable to parse config file '%s': %s\n", global_config->file, errbuf);
|
||||
LOGGER_CRIT("unable to parse config file '%s': %s\n", file_name, errbuf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -291,19 +330,19 @@ config_load(config_t *config)
|
|||
|
||||
toml_free(config_toml);
|
||||
|
||||
return 0;
|
||||
LOGGER_DEBUG("Loaded config from %s\n", file_name);
|
||||
}
|
||||
|
||||
void
|
||||
config_load_directory(config_t *config, char *directory_name)
|
||||
config_load_directory(config_t *config, const char *directory_name)
|
||||
{
|
||||
struct dirent *pDirent;
|
||||
DIR *pDir;
|
||||
struct dirent *directory_entry;
|
||||
DIR *directory;
|
||||
|
||||
(void)config;
|
||||
|
||||
pDir = opendir (directory_name);
|
||||
if(pDir == NULL)
|
||||
directory = opendir(directory_name);
|
||||
if(directory == NULL)
|
||||
{
|
||||
LOGGER_CRIT("cannot open directory '%s': %s\n", directory_name, strerror(errno));
|
||||
exit(1);
|
||||
|
@ -311,12 +350,21 @@ config_load_directory(config_t *config, char *directory_name)
|
|||
|
||||
// Process each entry.
|
||||
|
||||
while((pDirent = readdir(pDir)) != NULL)
|
||||
while((directory_entry = readdir(directory)) != NULL)
|
||||
{
|
||||
LOGGER_DEBUG("including: %s\n", pDirent->d_name);
|
||||
struct stat sb;
|
||||
const char *entry_name = directory_entry->d_name;
|
||||
if(stat(entry_name, &sb))
|
||||
{
|
||||
LOGGER_WARNING("failed to get info for '%s': %s\n", entry_name, strerror(errno));
|
||||
}
|
||||
if(S_ISREG(sb.st_mode))
|
||||
{
|
||||
config_load_file(config, entry_name);
|
||||
}
|
||||
}
|
||||
|
||||
// Close directory and exit.
|
||||
|
||||
closedir(pDir);
|
||||
closedir(directory);
|
||||
}
|
||||
|
|
|
@ -59,9 +59,10 @@ main(int argc, const char** argv)
|
|||
|
||||
config_init();
|
||||
|
||||
cli_parse(argc, argv, global_config);
|
||||
cli_t cli;
|
||||
cli_parse(argc, argv, &cli);
|
||||
|
||||
config_load(global_config);
|
||||
config_load(global_config, cli.config_file);
|
||||
|
||||
if(global_config->logging.file == NULL)
|
||||
{
|
||||
|
@ -73,8 +74,6 @@ main(int argc, const char** argv)
|
|||
config_load_directory(global_config, global_config->include);
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("Loaded config from %s\n", global_config->file);
|
||||
|
||||
|
||||
/******************** SETUP CONNECTION ********************/
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue