Skip to content

Commit

Permalink
Config handling fixes.
Browse files Browse the repository at this point in the history
Move file io out of config helper function and generate better error messages
when the file can't be parsed.
  • Loading branch information
wxsBSD committed Jan 9, 2025
1 parent 5616581 commit e3a50e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
11 changes: 5 additions & 6 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use std::path::Path;

use yara_x::config::MetaValueType;

Expand Down Expand Up @@ -96,15 +95,15 @@ impl Default for Config {
}
}

/// Load config file from a given path. Path must contain a valid TOML file or
/// this function will propagate the error. For structure of the config file
/// Load config file from a string which must contain a valid TOML file or this
/// function will propagate the error. For structure of the config file
/// see "YARA-X Config Guide.md".
pub fn load_config_from_file(
config_file: &Path,
pub fn load_config_from_str(
config_contents: &str,
) -> Result<Config, figment::Error> {
let config: Config =
Figment::from(Serialized::defaults(Config::default()))
.merge(Toml::file_exact(config_file))
.merge(Toml::string(config_contents))
.extract()?;
Ok(config)
}
38 changes: 33 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod config;
mod help;
mod walk;

use crate::config::{load_config_from_file, Config};
use crate::config::{load_config_from_str, Config};
use crossterm::tty::IsTty;
use std::path::PathBuf;
use std::{io, panic, process};
use std::{fs, io, panic, process};
use yansi::Color::Red;
use yansi::Paint;

Expand Down Expand Up @@ -56,13 +56,41 @@ fn main() -> anyhow::Result<()> {
let config_file = args.get_one::<PathBuf>("config");

let config: Config = if config_file.is_some() {
load_config_from_file(config_file.unwrap())?
let config_path = config_file.unwrap();
let config_contents = fs::read_to_string(config_path)?;
load_config_from_str(config_contents.as_str()).map_err(|e| {
figment::Error::from(format!(
"Unable to parse {}: {}",
config_path.to_string_lossy(),
e.to_string(),
))
})?
} else {
match home::home_dir() {
Some(home_path) if !home_path.as_os_str().is_empty() => {
load_config_from_file(&home_path.join(crate::CONFIG_FILE))?
let config_path = home_path.join(crate::CONFIG_FILE);
let config_contents = match fs::read_to_string(&config_path) {
// If the default file doesn't exist, just use an empty
// string.
Err(e) if e.kind() == io::ErrorKind::NotFound => {
"".to_string()
}
r => r?,
};
load_config_from_str(config_contents.as_str()).map_err(
|e| {
figment::Error::from(format!(
"Unable to parse {}: {}",
config_path.to_string_lossy(),
e.to_string(),
))
},
)?
}
_ => {
println!("could not find home directory");
Config::default()
}
_ => Config::default(),
}
};

Expand Down

0 comments on commit e3a50e1

Please sign in to comment.