refactor(app): use generic type for application output

This commit is contained in:
Orhun Parmaksız 2021-12-18 14:04:09 +03:00
parent d26bbf6093
commit 86b7390ecb
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 19 additions and 18 deletions

View file

@ -106,7 +106,7 @@ impl Parameter {
}
/// Prints the kernel parameter to given output.
pub fn display_value<W: Write>(&self, config: &Config, output: &mut W) -> Result<()> {
pub fn display_value<Output: Write>(&self, config: &Config, output: &mut Output) -> Result<()> {
match config.display_type {
DisplayType::Name => {
writeln!(output, "{}", self.colored_name(config))?;
@ -145,7 +145,7 @@ impl Parameter {
}
/// Prints the description of the kernel parameter to the given output.
pub fn display_documentation<W: Write>(&self, output: &mut W) -> Result<()> {
pub fn display_documentation<Output: Write>(&self, output: &mut Output) -> Result<()> {
if let Some(documentation) = self.get_documentation() {
writeln!(output, "{}\n", documentation)?;
} else {
@ -155,11 +155,11 @@ impl Parameter {
}
/// Sets a new value for the kernel parameter.
pub fn update_value<W: Write>(
pub fn update_value<Output: Write>(
&mut self,
new_value: &str,
config: &Config,
output: &mut W,
output: &mut Output,
) -> Result<()> {
let ctl = Ctl::new(&self.name)?;
let new_value = ctl.set_value_string(new_value)?;

View file

@ -1,5 +1,5 @@
use std::env;
use std::io::{self, Stdout};
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use systeroid_core::cache::{Cache, CacheData};
@ -18,25 +18,25 @@ const PARAMETERS_CACHE_LABEL: &str = "parameters";
/// Application controller.
#[derive(Debug)]
pub struct App<'a> {
pub struct App<'a, Output: Write> {
/// Sysctl controller.
sysctl: &'a mut Sysctl,
/// Application cache.
cache: Cache,
/// Standard output.
output: &'a mut Output,
/// Whether if the output will be in tree format.
tree_output: bool,
/// Standard output.
stdout: Stdout,
}
impl<'a> App<'a> {
impl<'a, Output: Write> App<'a, Output> {
/// Constructs a new instance.
pub fn new(sysctl: &'a mut Sysctl, tree_output: bool) -> Result<Self> {
pub fn new(sysctl: &'a mut Sysctl, output: &'a mut Output, tree_output: bool) -> Result<Self> {
Ok(Self {
sysctl,
cache: Cache::init()?,
output,
tree_output,
stdout: io::stdout(),
})
}
@ -55,11 +55,10 @@ impl<'a> App<'a> {
.map(|v| v.as_ref()),
);
});
Tree::new(root_node.childs)
.print(&mut self.stdout, self.sysctl.config.default_color)?;
Tree::new(root_node.childs).print(self.output, self.sysctl.config.default_color)?;
} else {
parameters.try_for_each(|parameter| {
parameter.display_value(&self.sysctl.config, &mut self.stdout)
parameter.display_value(&self.sysctl.config, self.output)
})?;
}
Ok(())
@ -117,7 +116,7 @@ impl<'a> App<'a> {
for parameter in self.sysctl.get_parameters(param_name) {
let mut fallback_to_default = false;
if no_pager {
parameter.display_documentation(&mut self.stdout)?;
parameter.display_documentation(self.output)?;
continue;
}
let pager = env::var("PAGER").unwrap_or_else(|_| String::from("less"));
@ -138,7 +137,7 @@ impl<'a> App<'a> {
}
}
if fallback_to_default {
parameter.display_documentation(&mut self.stdout)?;
parameter.display_documentation(self.output)?;
}
}
Ok(())
@ -171,7 +170,7 @@ impl<'a> App<'a> {
parameter
);
} else {
param.update_value(&new_value, &config, &mut self.stdout)?;
param.update_value(&new_value, &config, self.output)?;
}
}
} else if write_mode {

View file

@ -9,6 +9,7 @@ pub mod args;
use crate::app::App;
use crate::args::Args;
use std::io;
use systeroid_core::config::Config;
use systeroid_core::error::Result;
use systeroid_core::sysctl::controller::Sysctl;
@ -23,8 +24,9 @@ pub fn run(args: Args) -> Result<()> {
display_type: args.display_type,
..Default::default()
};
let mut stdout = io::stdout();
let mut sysctl = Sysctl::init(config)?;
let mut app = App::new(&mut sysctl, args.tree_output)?;
let mut app = App::new(&mut sysctl, &mut stdout, args.tree_output)?;
if args.preload_system_files {
app.preload_from_system()?;