feat(app): check for alternate locations for kernel documentation

This commit is contained in:
Orhun Parmaksız 2021-11-29 03:00:09 +03:00
parent 22c9f5e92a
commit b8f871c8bd
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 25 additions and 18 deletions

View file

@ -6,6 +6,12 @@ use systeroid_parser::parser::Parser;
use systeroid_parser::regex::RegexBuilder;
lazy_static! {
/// Possible locations for the Linux kernel documentation.
pub static ref KERNEL_DOCS_PATH: Vec<&'static Path> = vec![
Path::new("/usr/share/doc/linux/"),
Path::new("/usr/share/doc/linux-docs/")
];
/// Pre-defined parsers for parsing the kernel documentation.
pub static ref PARSERS: Vec<Parser<'static>> = vec![
Parser {

View file

@ -1,9 +1,10 @@
use std::env;
use std::io::{self, Stdout};
use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use systeroid_core::config::AppConfig;
use systeroid_core::error::Result;
use systeroid_core::parsers::KERNEL_DOCS_PATH;
use systeroid_core::sysctl::Sysctl;
/// Application controller.
@ -37,19 +38,22 @@ impl<'a> App<'a> {
}
/// Updates the documentation for kernel parameters.
fn fetch_documentation(&mut self, kernel_docs: &Path) -> Result<()> {
if !kernel_docs.exists() {
eprintln!(
"warning: `linux kernel documentation is not found in path: {:?}`",
kernel_docs.to_string_lossy()
);
pub fn update_documentation(&mut self, kernel_docs: Option<&PathBuf>) -> Result<()> {
let mut kernel_docs_path = KERNEL_DOCS_PATH.clone();
if let Some(path) = kernel_docs {
kernel_docs_path.insert(0, path);
}
self.sysctl.update_docs(kernel_docs)
for path in KERNEL_DOCS_PATH.iter() {
if path.exists() {
return self.sysctl.update_docs(path);
}
}
eprintln!("warning: `Linux kernel documentation cannot be found. Please specify a path via '-d' argument`",);
Ok(())
}
/// Displays the documentation of a parameter.
pub fn display_documentation(&mut self, param_name: &str, kernel_docs: &Path) -> Result<()> {
self.fetch_documentation(kernel_docs)?;
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
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

@ -17,7 +17,7 @@ For more details see {bin}(8)."#;
#[derive(Debug, Default)]
pub struct Args {
/// Path of the Linux kernel documentation.
pub kernel_docs: PathBuf,
pub kernel_docs: Option<PathBuf>,
/// Display type of the variables.
pub display_type: DisplayType,
/// Whether if the unknown variable errors should be ignored.
@ -48,7 +48,7 @@ impl Args {
opts.optopt(
"d",
"docs",
"set the path of the kernel documentation\n(default: /usr/share/doc/linux/)",
"set the path of the kernel documentation",
"<path>",
);
opts.optflag("h", "help", "display this help and exit");
@ -87,11 +87,7 @@ impl Args {
DisplayType::Default
};
Some(Args {
kernel_docs: PathBuf::from(
matches
.opt_str("d")
.unwrap_or_else(|| String::from("/usr/share/doc/linux/")),
),
kernel_docs: matches.opt_str("d").map(PathBuf::from),
display_type,
ignore_errors: matches.opt_present("e"),
param_to_explain: matches.opt_str("explain"),

View file

@ -24,7 +24,8 @@ pub fn run(args: Args) -> Result<()> {
let mut app = App::new(&mut sysctl, &config.app);
if let Some(param) = args.param_to_explain {
app.display_documentation(&param, &args.kernel_docs)?;
app.update_documentation(args.kernel_docs.as_ref())?;
app.display_documentation(&param)?;
} else if args.param_names.is_empty() {
app.display_parameters()?;
} else {