feat(args): add -e flag for ignoring unknown variable errors

This commit is contained in:
Orhun Parmaksız 2021-11-20 19:26:34 +03:00
parent 079b65c409
commit c97854424f
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
5 changed files with 30 additions and 14 deletions

View file

@ -14,10 +14,19 @@ macro_rules! map {
/// General configuration.
#[derive(Debug, Default)]
pub struct Config {
/// Sysctl configuration.
pub sysctl: SysctlConfig,
/// Color configuration.
pub color: ColorConfig,
}
/// Sysctl configuration.
#[derive(Debug, Default)]
pub struct SysctlConfig {
/// Whether if the errors should be ignored.
pub ignore_errors: bool,
}
/// Sysctl configuration.
#[derive(Debug)]
pub struct ColorConfig {

View file

@ -1,4 +1,4 @@
use crate::config::ColorConfig;
use crate::config::{ColorConfig, SysctlConfig};
use crate::error::Result;
use crate::parsers::parse_kernel_docs;
use colored::*;
@ -196,11 +196,13 @@ impl<'a> TryFrom<&'a Ctl> for Parameter {
pub struct Sysctl {
/// Available kernel parameters.
pub parameters: Vec<Parameter>,
/// Configuration.
pub config: SysctlConfig,
}
impl Sysctl {
/// Constructs a new instance by fetching the available kernel parameters.
pub fn init() -> Result<Self> {
pub fn init(config: SysctlConfig) -> Result<Self> {
let mut parameters = Vec::new();
for ctl in CtlIter::root().filter_map(StdResult::ok).filter(|ctl| {
ctl.flags()
@ -216,7 +218,7 @@ impl Sysctl {
}
}
}
Ok(Self { parameters })
Ok(Self { parameters, config })
}
/// Searches and returns the parameter if it exists.
@ -225,7 +227,7 @@ impl Sysctl {
.parameters
.iter_mut()
.find(|param| param.name == *param_name);
if parameter.is_none() {
if parameter.is_none() && !self.config.ignore_errors {
eprintln!(
"{}: cannot stat /proc/{}: No such file or directory",
env!("CARGO_PKG_NAME").split('-').collect::<Vec<_>>()[0],

View file

@ -2,7 +2,7 @@ use std::env;
use std::io::{self, Stdout};
use std::path::Path;
use std::process::{Command, Stdio};
use systeroid_core::config::Config;
use systeroid_core::config::ColorConfig;
use systeroid_core::error::Result;
use systeroid_core::sysctl::Sysctl;
@ -11,19 +11,19 @@ use systeroid_core::sysctl::Sysctl;
pub struct App<'a> {
/// Sysctl manager.
sysctl: &'a mut Sysctl,
/// Configuration.
config: &'a Config,
/// Color configuration.
color_config: &'a ColorConfig,
/// Standard output.
stdout: Stdout,
}
impl<'a> App<'a> {
/// Constructs a new instance.
pub fn new(sysctl: &'a mut Sysctl, config: &'a Config) -> Self {
pub fn new(sysctl: &'a mut Sysctl, color_config: &'a ColorConfig) -> Self {
let stdout = io::stdout();
Self {
sysctl,
config,
color_config,
stdout,
}
}
@ -33,7 +33,7 @@ impl<'a> App<'a> {
self.sysctl
.parameters
.iter()
.try_for_each(|parameter| parameter.display_value(&self.config.color, &mut self.stdout))
.try_for_each(|parameter| parameter.display_value(self.color_config, &mut self.stdout))
}
/// Updates the documentation for kernel parameters.
@ -89,9 +89,9 @@ impl<'a> App<'a> {
};
if let Some(parameter) = self.sysctl.get_parameter(&param_name) {
if let Some(new_value) = new_value {
parameter.update_value(&new_value, &self.config.color, &mut self.stdout)?;
parameter.update_value(&new_value, self.color_config, &mut self.stdout)?;
} else {
parameter.display_value(&self.config.color, &mut self.stdout)?;
parameter.display_value(self.color_config, &mut self.stdout)?;
}
}
Ok(())

View file

@ -17,6 +17,8 @@ For more details see {bin}(8)."#;
pub struct Args {
/// Path of the Linux kernel documentation.
pub kernel_docs: PathBuf,
/// Whether if the unknown variable errors should be ignored.
pub ignore_errors: bool,
/// Parameter to explain.
pub param_to_explain: Option<String>,
/// Parameter names.
@ -32,6 +34,7 @@ impl Args {
opts.optflag("a", "all", "display all variables");
opts.optflag("A", "", "alias of -a");
opts.optflag("X", "", "alias of -a");
opts.optflag("e", "ignore", "ignore unknown variables errors");
opts.optopt(
"",
"explain",
@ -74,6 +77,7 @@ impl Args {
.opt_str("d")
.unwrap_or_else(|| String::from("/usr/share/doc/linux/")),
),
ignore_errors: matches.opt_present("e"),
param_to_explain: matches.opt_str("explain"),
param_names: matches.free,
})

View file

@ -17,9 +17,10 @@ use systeroid_core::sysctl::Sysctl;
/// Runs `systeroid`.
pub fn run(args: Args) -> Result<()> {
let mut config = Config::default();
config.sysctl.ignore_errors = args.ignore_errors;
config.color.no_color = env::var("NO_COLOR").is_ok();
let mut sysctl = Sysctl::init()?;
let mut app = App::new(&mut sysctl, &config);
let mut sysctl = Sysctl::init(config.sysctl)?;
let mut app = App::new(&mut sysctl, &config.color);
if let Some(param) = args.param_to_explain {
app.display_documentation(&param, &args.kernel_docs)?;