refactor(config): merge discrete configs into one struct

This commit is contained in:
Orhun Parmaksız 2021-12-11 02:18:29 +03:00
parent ab8617fe28
commit fcc3d32fd6
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
6 changed files with 41 additions and 49 deletions

View file

@ -12,18 +12,13 @@ macro_rules! map {
}}
}
/// General configuration.
#[derive(Debug, Default)]
/// Configuration.
#[derive(Clone, Debug)]
pub struct Config {
/// Application configuration.
pub app: AppConfig,
/// Sysctl configuration.
pub sysctl: SysctlConfig,
}
/// Sysctl configuration.
#[derive(Debug)]
pub struct AppConfig {
/// Whether if the verbose logging is enabled.
pub verbose: bool,
/// Whether if the errors should be ignored.
pub ignore_errors: bool,
/// Whether if the quiet mode is enabled.
pub quiet: bool,
/// Whether if the colors are disabled.
@ -38,9 +33,11 @@ pub struct AppConfig {
pub display_type: DisplayType,
}
impl Default for AppConfig {
impl Default for Config {
fn default() -> Self {
Self {
verbose: false,
ignore_errors: false,
quiet: false,
no_color: false,
no_pager: false,
@ -59,12 +56,3 @@ 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

@ -1,4 +1,4 @@
use crate::config::SysctlConfig;
use crate::config::Config;
use crate::error::Result;
use crate::parsers::parse_kernel_docs;
use crate::sysctl::parameter::Parameter;
@ -15,12 +15,12 @@ pub struct Sysctl {
/// Available kernel parameters.
pub parameters: Vec<Parameter>,
/// Configuration.
pub config: SysctlConfig,
pub config: Config,
}
impl Sysctl {
/// Constructs a new instance by fetching the available kernel parameters.
pub fn init(config: SysctlConfig) -> Result<Self> {
pub fn init(config: Config) -> Result<Self> {
let mut parameters = Vec::new();
for ctl in CtlIter::root().filter_map(StdResult::ok).filter(|ctl| {
ctl.flags()

View file

@ -1,5 +1,5 @@
/// Possible ways of displaying the kernel variables.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum DisplayType {
/// Print the kernel variable name along with its value.
Default,

View file

@ -1,4 +1,4 @@
use crate::config::AppConfig;
use crate::config::Config;
use crate::error::Result;
use crate::sysctl::display::DisplayType;
use crate::sysctl::section::Section;
@ -45,7 +45,7 @@ impl<'a> TryFrom<&'a Ctl> for Parameter {
impl Parameter {
/// Returns the parameter name with corresponding section colors.
pub fn colored_name(&self, config: &AppConfig) -> String {
pub fn colored_name(&self, config: &Config) -> String {
let fields = self.name.split('.').collect::<Vec<&str>>();
fields
.iter()
@ -69,7 +69,7 @@ impl Parameter {
}
/// Prints the kernel parameter to given output.
pub fn display_value<W: Write>(&self, config: &AppConfig, output: &mut W) -> Result<()> {
pub fn display_value<W: Write>(&self, config: &Config, output: &mut W) -> Result<()> {
if !config.no_color {
match config.display_type {
DisplayType::Name => {
@ -138,7 +138,7 @@ impl Parameter {
pub fn update_value<W: Write>(
&mut self,
new_value: &str,
config: &AppConfig,
config: &Config,
output: &mut W,
) -> Result<()> {
let ctl = Ctl::new(&self.name)?;

View file

@ -3,7 +3,6 @@ use std::io::{self, Stdout};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use systeroid_core::cache::{Cache, CacheData};
use systeroid_core::config::AppConfig;
use systeroid_core::error::Result;
use systeroid_core::parsers::KERNEL_DOCS_PATH;
use systeroid_core::regex::Regex;
@ -16,11 +15,9 @@ const PARAMETERS_CACHE_LABEL: &str = "parameters";
/// Application controller.
#[derive(Debug)]
pub struct App<'a> {
/// Sysctl manager.
/// Sysctl controller.
sysctl: &'a mut Sysctl,
/// Configuration.
config: &'a AppConfig,
/// Cache.
/// Application cache.
cache: Cache,
/// Standard output.
stdout: Stdout,
@ -28,10 +25,9 @@ pub struct App<'a> {
impl<'a> App<'a> {
/// Constructs a new instance.
pub fn new(sysctl: &'a mut Sysctl, config: &'a AppConfig) -> Result<Self> {
pub fn new(sysctl: &'a mut Sysctl) -> Result<Self> {
Ok(Self {
sysctl,
config,
cache: Cache::init()?,
stdout: io::stdout(),
})
@ -49,7 +45,9 @@ impl<'a> App<'a> {
true
}
})
.try_for_each(|parameter| parameter.display_value(self.config, &mut self.stdout))
.try_for_each(|parameter| {
parameter.display_value(&self.sysctl.config, &mut self.stdout)
})
}
/// Updates the documentation for kernel parameters.
@ -79,9 +77,10 @@ impl<'a> App<'a> {
/// Displays the documentation of a parameter.
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
let no_pager = self.sysctl.config.no_pager;
if let Some(parameter) = self.sysctl.get_parameter(param_name) {
let mut fallback_to_default = false;
if self.config.no_pager {
if no_pager {
parameter.display_documentation(&mut self.stdout)?;
return Ok(());
}
@ -122,14 +121,17 @@ impl<'a> App<'a> {
None
};
if let Some(new_value) = new_value {
let config = self.sysctl.config.clone();
if let Some(param) = self.sysctl.get_parameter(&parameter) {
param.update_value(&new_value, self.config, &mut self.stdout)?;
param.update_value(&new_value, &config, &mut self.stdout)?;
}
} else if display_value {
self.sysctl
.get_parameters(&parameter)
.iter()
.try_for_each(|parameter| parameter.display_value(self.config, &mut self.stdout))?;
.try_for_each(|parameter| {
parameter.display_value(&self.sysctl.config, &mut self.stdout)
})?;
}
Ok(())
}

View file

@ -16,15 +16,17 @@ 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.quiet = args.quiet;
config.app.no_color = env::var("NO_COLOR").is_ok();
config.app.no_pager = args.no_pager;
config.app.display_type = args.display_type;
let mut sysctl = Sysctl::init(config.sysctl)?;
let mut app = App::new(&mut sysctl, &config.app)?;
let config = Config {
verbose: args.verbose,
ignore_errors: args.ignore_errors,
quiet: args.quiet,
no_pager: args.no_pager,
display_type: args.display_type,
no_color: env::var("NO_COLOR").is_ok(),
..Default::default()
};
let mut sysctl = Sysctl::init(config)?;
let mut app = App::new(&mut sysctl)?;
if let Some(param) = args.param_to_explain {
app.update_documentation(args.kernel_docs.as_ref())?;