mirror of
https://github.com/orhun/systeroid
synced 2024-07-21 10:25:00 +00:00
feat(args): add --pattern
argument for matching parameters
This commit is contained in:
parent
85010f8c64
commit
b9d55a2302
|
@ -56,6 +56,7 @@ impl Default for AppConfig {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sysctl configuration.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SysctlConfig {
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
/// Export regex crate.
|
||||
pub use systeroid_parser::regex;
|
||||
|
||||
/// Kernel parameter handler.
|
||||
pub mod sysctl;
|
||||
|
||||
|
|
|
@ -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(¶meter.name)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
.try_for_each(|parameter| parameter.display_value(self.config, &mut self.stdout))
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ pub fn run(args: Args) -> Result<()> {
|
|||
app.update_documentation(args.kernel_docs.as_ref())?;
|
||||
app.display_documentation(¶m)?;
|
||||
} 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)?;
|
||||
|
|
Loading…
Reference in a new issue