Feature/include config: Support including additional config files using new config file directive#623
Open
cpitchford wants to merge 3 commits intominiupnp:masterfrom
Open
Feature/include config: Support including additional config files using new config file directive#623cpitchford wants to merge 3 commits intominiupnp:masterfrom
cpitchford wants to merge 3 commits intominiupnp:masterfrom
Conversation
ConfigLocations_create():
Creates a config file location handle
ConfigLocations_fgets():
Read a line from the current config file that is open
ConfigLocations_open_file():
Open a new current config file
ConfigLocations_open_folder():
Open all config files (sorted alphanumerically) from a folder
ConfigLocations_open_glob():
Open all config files matching a fnmatch/glob pattern
ConfigLocations_close()
Close the current config file (and move to next)
ConfigLocations_free()
Close all config files and free all resources
The fgets method also returns the line number/file path of the current file
As we process the lines returned by fgets we're able to ask the
handle to open a new pattern:
```C
int debug_flag = 1
const char * config_file_path = "my_config.conf";
char buffer[1024];
const char * current_file;
int current_line_number;
struct ConfigLocations *handle = ConfigLocations_create();
ConfigLocations_open_file(handle, config_file_path, debug_flag);
while (ConfigLocations_fgets(handle, &buffer, sizeof(buffer),
¤t_file, ¤t_line_number,
debug_flag))
{
// Remove new line
chat * end = strchr(buffer, '\n');
if (end) *end = '\0';
// Dump current file, line number, and line that was read:
printf("%s[%d]: Read \"%s\"\n", current_file, line_number, buffer);
// Look for lines starting "include "
// We'll use this as an instruction to include additional files
if (memcmp(buffer, "include ", sizeof("include ")-1) == 0) {
// Remove the "include " from the start of the line
char * pattern = buffer + sizeof("include ") - 1;
// Treat the remainder as a glob:
ConfigLocations_open_glob(handle, pattern, debug_flag);
// Continue with the read loop that will now jump
// to the included files
continue;
}
// Do something with the config file lines we read
}
ConfigLocations_free(handle);
```
In this demo, we parse our config file (my_config.conf).
When we encounter a line "include glob_patter", we resolve all the
files that match this pattern and open them in order.
Each call to ConfigLocations_fgets reads from the current file
At EOF, it jumps to the next file matching the current glob
before finally returned back to the previous file
After a line like:
include my_config.d/*.conf
each subsequent call to fgets will return a line from each *.conf file in that folder
before resuming reading from my_config.conf
…ective Using the ConfigLocations methods, the options fgets loop now supports including additional config files based on the glob set in the config the fgets call will stop reading the current file and start reading from each config file that matched the pattern/glob before finally resuming the original config file. The number of inclusions is limited by the CONFIG_RECURSION_DEPTH defined in the configlocations.h
Used in the options.c loop that reads from the config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR add support in the configuration file for an "include" directive.
If line appears in the config file loaded by the daemon:
It will pause processing the config file and begin processing each config that matches the pattern/glob in turn before resuming processing of the original file.
The directive is recursive, but limited to a depth of 5 config files.
This process allows for a config folder, or including a "local" file to override package defaults
I have in my setup, used:
This capability is enabled using a new .h/.c file configlocations.
struct ConfigLocationsis a handle that provides a fgets like interface to read lines.The handle can be instructed to open a file, folder, or glob pattern.
calling the fgets method against the handle will return a line from the current file, automatically rolling forward to other files matched by folder or glob and rolling back to paused files.
The "include" directive is included in the config file reading loop. When an "include" directive is found, the parameter is passed into the open_glob method to load all config files that match the pattern.
This functionality is proving useful for me as I dynamically create the allow list based on other system information and I would prefer not to re-write the entire config file each time. I also like having the ability to override defaults in the package config independently