feat(args): add --pattern argument for matching parameters

This commit is contained in:
Orhun Parmaksız 2021-12-04 15:12:25 +03:00
parent 85010f8c64
commit b9d55a2302
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
5 changed files with 28 additions and 4 deletions

View file

@ -56,6 +56,7 @@ impl Default for AppConfig {
}
}
}
/// Sysctl configuration.
#[derive(Debug, Default)]
pub struct SysctlConfig {

View file

@ -5,6 +5,9 @@
#[macro_use]
extern crate lazy_static;
/// Export regex crate.
pub use systeroid_parser::regex;
/// Kernel parameter handler.
pub mod sysctl;

View file

@ -6,6 +6,7 @@ use systeroid_core::cache::{Cache, CacheData};
use systeroid_core::config::AppConfig;
use systeroid_core::error::Result;
use systeroid_core::parsers::KERNEL_DOCS_PATH;
use systeroid_core::regex::Regex;
use systeroid_core::sysctl::Sysctl;
/// Label for caching the kernel parameters.
@ -35,11 +36,18 @@ impl<'a> App<'a> {
})
}
/// Displays all of the available kernel modules.
pub fn display_parameters(&mut self) -> Result<()> {
/// Displays all of the available kernel parameters.
pub fn display_parameters(&mut self, pattern: Option<Regex>) -> Result<()> {
self.sysctl
.parameters
.iter()
.filter(|parameter| {
if let Some(pattern) = &pattern {
pattern.is_match(&parameter.name)
} else {
true
}
})
.try_for_each(|parameter| parameter.display_value(self.config, &mut self.stdout))
}

View file

@ -2,6 +2,7 @@ use getopts::Options;
use std::env;
use std::path::PathBuf;
use systeroid_core::display::DisplayType;
use systeroid_core::regex::Regex;
/// Help message for the arguments.
const HELP_MESSAGE: &str = r#"
@ -24,6 +25,8 @@ pub struct Args {
pub ignore_errors: bool,
/// Do not pipe output into a pager.
pub no_pager: bool,
/// Pattern for matching the parameters.
pub pattern: Option<Regex>,
/// Parameter to explain.
pub param_to_explain: Option<String>,
/// Parameter names.
@ -41,6 +44,12 @@ impl Args {
opts.optflag("e", "ignore", "ignore unknown variables errors");
opts.optflag("N", "names", "print variable names without values");
opts.optflag("n", "values", "print only values of the given variable(s)");
opts.optopt(
"r",
"pattern",
"select setting that match expression",
"<expression>",
);
opts.optopt(
"E",
"explain",
@ -94,7 +103,10 @@ impl Args {
display_type,
ignore_errors: matches.opt_present("e"),
no_pager: matches.opt_present("P"),
param_to_explain: matches.opt_str("explain"),
pattern: matches
.opt_str("r")
.map(|v| Regex::new(&v).expect("invalid regex")),
param_to_explain: matches.opt_str("E"),
param_names: matches.free,
})
}

View file

@ -28,7 +28,7 @@ pub fn run(args: Args) -> Result<()> {
app.update_documentation(args.kernel_docs.as_ref())?;
app.display_documentation(&param)?;
} else if args.param_names.is_empty() {
app.display_parameters()?;
app.display_parameters(args.pattern)?;
} else {
for param_name in args.param_names {
app.process_parameter(param_name)?;