Skip to content

Commit

Permalink
refactor(cli): change the way in which the output path is passed to t…
Browse files Browse the repository at this point in the history
…he `compile` command

The output path was passed as the last positional argument for the command, but this have an inconvenience. If the user pass multiple input files and forgets to specify an output path, the last input path is used as output and the input file is overwritten.

In order to prevent this, the output path is now passed with the `--output` option. If the option is missing, the default path `output.yarc` is used.

Closes #126.
  • Loading branch information
plusvic committed May 27, 2024
1 parent 5a70903 commit c7759f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
25 changes: 13 additions & 12 deletions cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ pub fn compile() -> Command {
.action(ArgAction::Append),
)
.arg(
arg!(<OUTPUT_PATH>)
.help("Path to file with compiled results")
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(--"path-as-namespace")
.help("Use file path as rule namespace"),
)
.arg(
arg!(--"relaxed-re-syntax")
.help("Use a more relaxed syntax check while parsing regular expressions"),
arg!(-o --"output" <OUTPUT_PATH>)
.help("Output file with compiled results")
.default_value("output.yarc")
.value_parser(value_parser!(PathBuf))
)
.arg(
Arg::new("define")
Expand All @@ -40,12 +33,20 @@ pub fn compile() -> Command {
.value_parser(external_var_parser)
.action(ArgAction::Append),
)
.arg(
arg!(--"path-as-namespace")
.help("Use file path as rule namespace"),
)
.arg(
arg!(--"relaxed-re-syntax")
.help("Use a more relaxed syntax check while parsing regular expressions"),
)
}

pub fn exec_compile(args: &ArgMatches) -> anyhow::Result<()> {
let rules_path = args.get_many::<PathBuf>("RULES_PATH").unwrap();
let output_path = args.get_one::<PathBuf>("OUTPUT_PATH").unwrap();
let path_as_namespace = args.get_flag("path-as-namespace");
let output_path = args.get_one::<PathBuf>("output").unwrap();

let external_vars: Option<Vec<(String, serde_json::Value)>> = args
.get_many::<(String, serde_json::Value)>("define")
Expand Down
8 changes: 5 additions & 3 deletions site/content/docs/intro/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ re-used for multiple scan operations.
The syntax for this command is:

```
yr compile [OPTIONS] <RULES_PATH>... <OUTPUT_PATH>
yr compile [OPTIONS] <RULES_PATH>...
```

Each `<RULES_PATH>` is the path of YARA source file or a directory containing
source files. When`<RULES_PATH>` is a directory YARA-X iterates the directory
recursively looking for any `*.yar` or `*.yara` files.

The `<OUTPUT_PATH>` is the path of the output binary file. The supported
options are:
### --output, -o <OUTPUT_PATH>

Specify the path for the output binary file containing the compiled rules. By
default, is `output.yarc`.

### --relaxed-re-syntax

Expand Down

0 comments on commit c7759f8

Please sign in to comment.