feat(sysctl): add -a flag for printing the parameters

This commit is contained in:
Orhun Parmaksız 2021-10-29 19:39:34 +03:00
parent da927e010f
commit da334a9732
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 14 additions and 10 deletions

View file

@ -2,6 +2,7 @@ use crate::error::Result;
use crate::parsers::parse_kernel_docs;
use rayon::prelude::*;
use std::fmt::{self, Display, Formatter};
use std::io::Write;
use std::path::Path;
use std::result::Result as StdResult;
use sysctl::{CtlFlags, CtlIter, Sysctl as SysctlImpl};
@ -143,4 +144,12 @@ impl Sysctl {
});
Ok(())
}
/// Prints the available kernel parameters to the given output.
pub fn print_all<W: Write>(&self, output: &mut W) -> Result<()> {
for parameter in &self.parameters {
writeln!(output, "{} = {}", parameter.name, parameter.value)?;
}
Ok(())
}
}

View file

@ -40,7 +40,7 @@ impl Args {
.map_err(|e| eprintln!("error: {}", e))
.ok()?;
if matches.opt_present("h") {
if matches.opt_present("h") || !matches.opt_present("a") {
let usage = opts.usage_with_format(|opts| {
HELP_MESSAGE
.replace("{bin}", env!("CARGO_PKG_NAME"))

View file

@ -6,26 +6,21 @@
pub mod args;
use crate::args::Args;
use std::io;
use systeroid_core::error::Result;
use systeroid_core::sysctl::Sysctl;
/// Runs `systeroid`.
pub fn run(args: Args) -> Result<()> {
let mut stdout = io::stdout();
let mut sysctl = Sysctl::init()?;
if let Some(kernel_docs) = args.kernel_docs {
sysctl.update_docs(&kernel_docs)?;
}
for param in sysctl.parameters {
println!(
"{}\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n{}\n(from: {:?})\n",
param.name,
param
.description
.unwrap_or_else(|| String::from("no documentation")),
param.document.map(|d| d.path).unwrap_or_default(),
);
if args.all {
sysctl.print_all(&mut stdout)?;
}
Ok(())