feat(sysctl): support displaying only the given parameters

This commit is contained in:
Orhun Parmaksız 2021-11-03 01:01:42 +03:00
parent 9d3910f6b3
commit e4507feb5c
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 19 additions and 5 deletions

View file

@ -176,8 +176,17 @@ impl Sysctl {
}
/// Prints the available kernel parameters to the given output.
pub fn print_all<W: Write>(&self, output: &mut W, colored: bool) -> Result<()> {
for parameter in &self.parameters {
pub fn display<W: Write>(
&self,
output: &mut W,
names: Vec<String>,
colored: bool,
) -> Result<()> {
for parameter in self
.parameters
.iter()
.filter(|param| names.is_empty() || names.iter().any(|name| param.name == *name))
{
if colored {
writeln!(
output,

View file

@ -21,6 +21,8 @@ pub struct Args {
pub display_all: bool,
/// Disable colored output.
pub no_color: bool,
/// Parameter names.
pub param_names: Vec<String>,
}
impl Args {
@ -45,8 +47,10 @@ impl Args {
.map_err(|e| eprintln!("error: {}", e))
.ok()?;
let display_all =
matches.opt_present("a") || matches.opt_present("A") || matches.opt_present("X");
let display_all = matches.opt_present("a")
|| matches.opt_present("A")
|| matches.opt_present("X")
|| !matches.free.is_empty();
if matches.opt_present("h") || !display_all {
let usage = opts.usage_with_format(|opts| {
@ -64,6 +68,7 @@ impl Args {
kernel_docs: matches.opt_str("d").map(PathBuf::from),
display_all,
no_color: matches.opt_present("no-color"),
param_names: matches.free,
})
}
}

View file

@ -22,7 +22,7 @@ pub fn run(args: Args) -> Result<()> {
}
if args.display_all {
sysctl.print_all(&mut stdout, !args.no_color)?;
sysctl.display(&mut stdout, args.param_names, !args.no_color)?;
}
Ok(())