feat(args): add --system flag for preloading from system directories

This commit is contained in:
Orhun Parmaksız 2021-12-15 17:22:19 +03:00
parent 5a4933fcbd
commit e49155a503
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
5 changed files with 47 additions and 3 deletions

View file

@ -10,9 +10,19 @@ pub mod display;
/// Kernel parameter.
pub mod parameter;
/// Default location to preload values.
/// Default configuration file to preload values from.
pub const DEFAULT_PRELOAD: &str = "/etc/sysctl.conf";
/// Default system configuration files to preload values from.
pub const SYSTEM_PRELOAD: &[&str] = &[
"/etc/sysctl.d",
"/run/sysctl.d",
"/usr/local/lib/sysctl.d",
"/usr/lib/sysctl.d",
"/lib/sysctl.d",
DEFAULT_PRELOAD,
];
/// Deprecated variables to skip while listing.
/// <https://bugzilla.redhat.com/show_bug.cgi?id=152435>
pub const DEPRECATED_VARIABLES: &[&str] = &["base_reachable_time", "retrans_time"];

View file

@ -5,6 +5,9 @@
/// Export regex crate.
pub use regex;
/// Export globwalk crate.
pub use globwalk;
/// Document parser.
pub mod parser;

View file

@ -7,7 +7,8 @@ use systeroid_core::error::Result;
use systeroid_core::parsers::KERNEL_DOCS_PATH;
use systeroid_core::regex::Regex;
use systeroid_core::sysctl::controller::Sysctl;
use systeroid_core::sysctl::DEPRECATED_VARIABLES;
use systeroid_core::sysctl::{DEPRECATED_VARIABLES, SYSTEM_PRELOAD};
use systeroid_parser::globwalk;
use systeroid_parser::reader;
/// Label for caching the kernel parameters.
@ -181,4 +182,27 @@ impl<'a> App<'a> {
}
Ok(())
}
/// Processes the parameters in files that are in predefined system directories.
pub fn preload_from_system(&mut self) -> Result<()> {
for preload_path in SYSTEM_PRELOAD
.iter()
.map(|v| PathBuf::from(v).join("*.conf"))
{
if let Ok(glob_walker) = globwalk::glob(preload_path.to_string_lossy()) {
for file in glob_walker.filter_map(|v| v.ok()) {
println!("* Applying {} ...", file.path().display());
let contents = reader::read_to_string(file.path())?;
for parameter in contents.lines() {
if let Err(e) = self.process_parameter(parameter.to_string(), false, false)
{
eprintln!("{}: {}", env!("CARGO_PKG_NAME"), e);
}
}
}
}
}
Ok(())
}
}

View file

@ -36,6 +36,8 @@ pub struct Args {
pub no_pager: bool,
/// Whether if files are given to preload values.
pub preload_files: bool,
/// Whether if the values will be preloaded from system.
pub preload_system_files: bool,
/// Pattern for matching the variables.
pub pattern: Option<Regex>,
/// Whether if the documentation should be shown.
@ -62,6 +64,7 @@ impl Args {
opts.optflag("b", "binary", "print only variable values without new line");
opts.optflag("p", "load", "read values from file");
opts.optflag("f", "", "alias of -p");
opts.optflag("S", "system", "read values from all system directories");
opts.optopt(
"r",
"pattern",
@ -100,6 +103,7 @@ impl Args {
let required_args_present = !matches.free.is_empty()
|| display_all
|| preload_files
|| matches.opt_present("S")
|| matches.opt_present("r")
|| matches.opt_present("E");
@ -151,6 +155,7 @@ impl Args {
ignore_errors: matches.opt_present("e"),
no_pager: matches.opt_present("P"),
preload_files,
preload_system_files: matches.opt_present("S"),
pattern: matches
.opt_str("r")
.map(|v| Regex::new(&v).expect("invalid regex")),

View file

@ -28,7 +28,9 @@ pub fn run(args: Args) -> Result<()> {
let mut sysctl = Sysctl::init(config)?;
let mut app = App::new(&mut sysctl)?;
if args.values.is_empty() {
if args.preload_system_files {
app.preload_from_system()?;
} else if args.values.is_empty() {
app.display_parameters(args.pattern, args.display_deprecated)?;
} else if args.explain_params {
app.update_documentation(args.kernel_docs.as_ref())?;