feat(args): add --verbose flag for logging verbosity

This commit is contained in:
Orhun Parmaksız 2021-12-07 18:26:15 +03:00
parent 6e2709c332
commit 830535a170
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 11 additions and 2 deletions

View file

@ -60,6 +60,8 @@ impl Default for AppConfig {
/// Sysctl configuration.
#[derive(Debug, Default)]
pub struct SysctlConfig {
/// Whether if the verbose logging is enabled.
pub verbose: bool,
/// Whether if the errors should be ignored.
pub ignore_errors: bool,
}

View file

@ -32,7 +32,9 @@ impl Sysctl {
parameters.push(parameter);
}
Err(e) => {
eprintln!("error: `{} ({})`", e, ctl.name()?);
if config.verbose {
eprintln!("{} ({})", e, ctl.name()?);
}
}
}
}

View file

@ -17,6 +17,8 @@ For more details see {bin}(8)."#;
/// Command-line arguments.
#[derive(Debug, Default)]
pub struct Args {
/// Whether if the verbose logging is enabled.
pub verbose: bool,
/// Path of the Linux kernel documentation.
pub kernel_docs: Option<PathBuf>,
/// Display type of the variables.
@ -62,7 +64,8 @@ impl Args {
"set the path of the kernel documentation",
"<path>",
);
opts.optflag("P", "no-pager", "Do not pipe output into a pager");
opts.optflag("P", "no-pager", "do not pipe output into a pager");
opts.optflag("v", "verbose", "enable the verbose logging");
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
@ -108,6 +111,7 @@ impl Args {
DisplayType::Default
};
Some(Args {
verbose: matches.opt_present("v"),
kernel_docs: matches.opt_str("d").map(PathBuf::from),
display_type,
ignore_errors: matches.opt_present("e"),

View file

@ -17,6 +17,7 @@ use systeroid_core::sysctl::controller::Sysctl;
/// Runs `systeroid`.
pub fn run(args: Args) -> Result<()> {
let mut config = Config::default();
config.sysctl.verbose = args.verbose;
config.sysctl.ignore_errors = args.ignore_errors;
config.app.display_type = args.display_type;
config.app.no_color = env::var("NO_COLOR").is_ok();