feat(sysctl): ignore errors when '-' is used while preloading

This commit is contained in:
Orhun Parmaksız 2022-03-24 22:28:25 +03:00
parent d4662da5b4
commit 53d94d0dfc
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 11 additions and 14 deletions

View file

@ -189,8 +189,7 @@ impl<'a, Output: Write> App<'a, Output> {
}
/// Processes the parameters in the given file.
pub fn preload_from_file(&mut self, file: String) -> Result<()> {
let path = PathBuf::from(file);
pub fn preload_from_file(&mut self, path: PathBuf) -> Result<()> {
if !path.exists() {
eprintln!(
"{}: cannot open {:?}: No such file or directory",
@ -204,7 +203,13 @@ impl<'a, Output: Write> App<'a, Output> {
.lines()
.filter(|v| !(v.starts_with('#') || v.starts_with(';') || v.is_empty()))
{
self.process_parameter(parameter.to_string(), false, false)?;
let process_result =
self.process_parameter(parameter.trim_start_matches('-').to_string(), false, false);
if !parameter.starts_with('-') {
process_result?;
} else if let Err(e) = process_result {
eprintln!("{}: {}", env!("CARGO_PKG_NAME"), e);
}
}
Ok(())
}
@ -218,16 +223,7 @@ impl<'a, Output: Write> App<'a, Output> {
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()
.filter(|v| !(v.starts_with('#') || v.starts_with(';') || v.is_empty()))
{
if let Err(e) = self.process_parameter(parameter.to_string(), false, false)
{
eprintln!("{}: {}", env!("CARGO_PKG_NAME"), e);
}
}
self.preload_from_file(file.path().to_path_buf())?;
}
}
}

View file

@ -12,6 +12,7 @@ pub mod output;
use crate::app::App;
use crate::args::Args;
use std::io::Write;
use std::path::PathBuf;
use systeroid_core::config::Config;
use systeroid_core::error::Result;
use systeroid_core::sysctl::controller::Sysctl;
@ -39,7 +40,7 @@ pub fn run<Output: Write>(args: Args, output: &mut Output) -> Result<()> {
}
} else if args.preload_files {
for file in args.values {
app.preload_from_file(file)?;
app.preload_from_file(PathBuf::from(file))?;
}
} else {
for param in args.values {