feat(docs): support glob patterns for documentation path

This commit is contained in:
Orhun Parmaksız 2022-03-19 00:40:13 +03:00
parent e356664e1f
commit c47b6c35ac
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 26 additions and 16 deletions

View file

@ -5,14 +5,15 @@ use parseit::regex::RegexBuilder;
use rayon::prelude::*;
use std::path::Path;
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-doc/"),
Path::new("/usr/share/doc/linux-docs/")
];
/// Possible locations for the Linux kernel documentation.
pub const KERNEL_DOCS_PATH: &[&str] = &[
"/usr/share/doc/linux/*",
"/usr/share/doc/linux-doc/*",
"/usr/share/doc/linux-docs/*",
"/usr/share/doc/kernel-doc-*/Documentation/*",
];
lazy_static! {
/// Pre-defined parsers for parsing the kernel documentation.
pub static ref PARSERS: Vec<Parser<'static>> = vec![
Parser {

View file

@ -5,6 +5,7 @@ use crate::parsers::{parse_kernel_docs, KERNEL_DOCS_PATH};
use crate::sysctl::parameter::Parameter;
use crate::sysctl::section::Section;
use crate::sysctl::{DISABLE_CACHE_ENV, PARAMETERS_CACHE_LABEL, PROC_PATH};
use parseit::globwalk;
use rayon::prelude::*;
use std::convert::TryFrom;
use std::env;
@ -78,9 +79,21 @@ impl Sysctl {
kernel_docs: Option<&PathBuf>,
cache: &Cache,
) -> Result<()> {
let mut kernel_docs_path = KERNEL_DOCS_PATH.clone();
if let Some(path) = kernel_docs {
kernel_docs_path.insert(0, path);
let mut kernel_docs_path = if let Some(path) = kernel_docs {
vec![path.to_path_buf()]
} else {
Vec::new()
};
for path in KERNEL_DOCS_PATH {
if let Some(mut path) = globwalk::glob(path).ok().and_then(|glob| {
glob.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().is_file())
.map(|entry| entry.into_path())
.next()
}) {
path.pop();
kernel_docs_path.push(path);
}
}
if let Some(path) = kernel_docs_path.iter().find(|path| path.exists()) {
if cache.exists(PARAMETERS_CACHE_LABEL) && kernel_docs.is_none() {
@ -160,10 +173,10 @@ impl Sysctl {
#[cfg(test)]
mod tests {
use super::*;
use crate::parsers::KERNEL_DOCS_PATH;
#[test]
fn test_sysctl_controller() -> Result<()> {
env::set_var(DISABLE_CACHE_ENV, "1");
let config = Config::default();
let mut sysctl = Sysctl::init(config)?;
assert!(sysctl.get_parameter("kernel.hostname").is_some());
@ -174,11 +187,7 @@ mod tests {
);
assert!(sysctl.get_parameters("---").is_empty());
for path in KERNEL_DOCS_PATH.iter() {
if path.exists() {
sysctl.update_docs(&path)?;
}
}
sysctl.update_docs_from_cache(None, &Cache::init()?)?;
let parameter = sysctl.get_parameter("kernel.hostname").unwrap().clone();
let old_value = parameter.docs_title;