refactor(args): add a default path for linux kernel documentation

This commit is contained in:
Orhun Parmaksız 2021-11-18 21:43:42 +03:00
parent 5e0725472b
commit c75e1646d1
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 22 additions and 10 deletions

View file

@ -1,5 +1,6 @@
use std::env;
use std::io::{self, Stdout};
use std::path::Path;
use std::process::{Command, Stdio};
use systeroid_core::config::Config;
use systeroid_core::error::Result;
@ -35,8 +36,20 @@ impl<'a> App<'a> {
.try_for_each(|parameter| parameter.display_value(&self.config.color, &mut self.stdout))
}
/// Updates the documentation for kernel parameters.
fn fetch_documentation(&mut self, kernel_docs: &Path) -> Result<()> {
if !kernel_docs.exists() {
eprintln!(
"WARN: Linux kernel documentation is not found in path: {:?}",
kernel_docs.to_string_lossy()
);
}
self.sysctl.update_docs(kernel_docs)
}
/// Displays the documentation of a parameter.
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
pub fn display_documentation(&mut self, param_name: &str, kernel_docs: &Path) -> Result<()> {
self.fetch_documentation(kernel_docs)?;
if let Some(parameter) = self.sysctl.get_parameter(param_name) {
let mut fallback_to_default = false;
let pager = env::var("PAGER").unwrap_or_else(|_| String::from("less"));

View file

@ -16,7 +16,7 @@ For more details see {bin}(8)."#;
#[derive(Debug, Default)]
pub struct Args {
/// Path of the Linux kernel documentation.
pub kernel_docs: Option<PathBuf>,
pub kernel_docs: PathBuf,
/// Parameter to explain.
pub param_to_explain: Option<String>,
/// Parameter names.
@ -41,7 +41,7 @@ impl Args {
opts.optopt(
"d",
"docs",
"set the path of the kernel documentation",
"set the path of the kernel documentation\n(default: /usr/share/doc/linux/)",
"<path>",
);
@ -69,7 +69,11 @@ impl Args {
None
} else {
Some(Args {
kernel_docs: matches.opt_str("d").map(PathBuf::from),
kernel_docs: PathBuf::from(
matches
.opt_str("d")
.unwrap_or_else(|| String::from("/usr/share/doc/linux/")),
),
param_to_explain: matches.opt_str("explain"),
param_names: matches.free,
})

View file

@ -19,15 +19,10 @@ pub fn run(args: Args) -> Result<()> {
let mut config = Config::default();
config.color.no_color = env::var("NO_COLOR").is_ok();
let mut sysctl = Sysctl::init()?;
if let Some(kernel_docs) = args.kernel_docs {
sysctl.update_docs(&kernel_docs)?;
}
let mut app = App::new(&mut sysctl, &config);
if let Some(param) = args.param_to_explain {
app.display_documentation(&param)?;
app.display_documentation(&param, &args.kernel_docs)?;
} else if args.param_names.is_empty() {
app.display_parameters()?;
} else {