feat: Implement toml config for fmt command.
This implements a config option which can be used to set options for "yr fmt".
An example config file is included as "wxsfmt.toml" which currently has everything disabled just to demonstrate the behavior.
If any option is not set it will fall back to the default values provided by the "fmt" command which currently match the defaults provided by the library.
This is not ready to be merged yet, but it is at a point where the overall design should be discussed.
The idea is to have defaults that can be overridden with the config file, which is toml format. An example config file is:
[rule]
indent_section_headers = false
indent_section_contents = false
[meta]
align_values = false
[patterns]
align_values = false
I think it is worth breaking up the config file into sections, which will make it easier to write and document. Currently there are three sections:
-
rulefor anything that applies globally to all rules. This is currently things like indenting the section headers (strings:,meta:, etc) -
metafor anything related to metadata contents. Currently onlyalign_valuesis supported here. -
patternsfor anything related tostringssection. Currently onlyalign_valuesis supported here.
The idea is to expose the rest of the options in the formatter through the config file, but if we choose to change the format of the config file now is a good time to do that.
I'm open to changing names on any of these if you have any opinions on that.
I like the approach, and the figment create looks like a great choice, but I would make the configuration file a global one that not only affects the fmt command. The idea is using the same config file for other settings not related to fmt in the future. As such, the file should have a [fmt] section that contains the rest of the subsections you propose.
I would also like that users don't need to specify the config file in the command-line (that option can remain), but the program should look for well-known locations looking for a yara-x.toml with the configuration.
OK, I've added support for loading it from ${HOME}/.yara-x.toml and also moved things around a bit so that config related structures and loading are in their own module. The main yr binary now reads the config file and passes the fmt section to the fmt command, which is something we can easily extend to the other commands as the config file grows support for them.
Turns out the config file format I gave above is total garbage (I misread some stackoverflow post) and this is a much better way to do it:
# Config file for YARA-X. For full documentation see XXX (INSERT DOCS URL HERE)
[fmt]
rule.pants = false # Invalid keys are ignored.
rule.indent_section_headers = true
rule.indent_section_contents = false
meta.align_values = false
patterns.align_values = false
The above is my current .yara-x.toml file, which demonstrates what I have in mind for how this can be extended. Each command can have it's own section and we can express values using the dot notation if further nesting is necessary. If no config file is found it will use the defaults defined in lib/config/mod.rs
If you think this config format works well I'll update this PR with the actual documentation and then I can go about tackling the rest of the options in discussed in #192.
I like the latest changes.