Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement toml config for fmt command. #205

Merged
merged 6 commits into from
Oct 3, 2024

Conversation

wxsBSD
Copy link
Contributor

@wxsBSD wxsBSD commented Sep 24, 2024

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 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.
@wxsBSD
Copy link
Contributor Author

wxsBSD commented Sep 24, 2024

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:

  • rule for anything that applies globally to all rules. This is currently things like indenting the section headers (strings:, meta:, etc)
  • meta for anything related to metadata contents. Currently only align_values is supported here.
  • patterns for anything related to strings section. Currently only align_values is 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.

@plusvic
Copy link
Member

plusvic commented Sep 24, 2024

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.

Move the config structures and loading to a separate module, so that it can be
used by each command.

Pass the "fmt" section of the config to the "fmt" command. As we add support for
other commands to use the config file we can easily pass their sub-structure
into the command.

Load the config file from %{HOME}/.yara-x.toml - or whatever the corresponding
location is on Windows.

The exact format of the config is still to be documented, but now that it is
generic and not specific to formatting I've moved the docs to a new file that is
more appropriately named.
@wxsBSD
Copy link
Contributor Author

wxsBSD commented Sep 29, 2024

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.

@plusvic
Copy link
Member

plusvic commented Oct 2, 2024

I like the latest changes.

This adds a "rule.indent_spaces" option to the fmt config. It is a u8 where 0
means "use tabs" and any positive integer will use that many spaces per
indent-level.

While here, document the config file format. I intend to keep updating this
documentation as I add more options to the "fmt" command, and also as I extend
the config file to work with more than just "fmt".
@@ -212,6 +212,8 @@ pub(crate) enum Token<'a> {
// Non-control tokens
//
Whitespace,
#[allow(dead_code)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plusvic I'm not sure this is the right way to do this.

The config file will be used by the CLI only, not by the YARA-X library as a whole. Every configurable setting in the library is set through some method in `Compiler`, `Scanner` or any of the types exported by the library.
@plusvic plusvic merged commit a9486d3 into VirusTotal:main Oct 3, 2024
16 checks passed
plusvic pushed a commit that referenced this pull request Oct 3, 2024
This implements a config options for `yr fmt`. The options reside in a config file 
 at `%{HOME}/.yara-x.toml` - or whatever the corresponding location is on Windows.
The exact format of the config file is documented in `docs/YARA-X Config Guide.md`.
@wxsBSD wxsBSD deleted the fmt_improvements branch October 4, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants