use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use tldr_core::types::ModuleInfo;
use tldr_core::{extract_file, Language};
use crate::commands::daemon_router::{params_with_file, try_daemon_route};
use crate::output::{format_module_info_text, OutputFormat, OutputWriter};
#[derive(Debug, Args)]
pub struct ExtractArgs {
pub file: PathBuf,
#[arg(long, short = 'l')]
pub lang: Option<Language>,
}
impl ExtractArgs {
pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
let writer = OutputWriter::new(format, quiet);
let project = self.file.parent().unwrap_or(&self.file);
if let Some(result) =
try_daemon_route::<ModuleInfo>(project, "extract", params_with_file(&self.file))
{
if writer.is_text() {
writer.write_text(&format_module_info_text(&result))?;
} else {
writer.write(&result)?;
}
return Ok(());
}
writer.progress(&format!(
"Extracting module info from {}...",
self.file.display()
));
let result = extract_file(&self.file, None)?;
if writer.is_text() {
writer.write_text(&format_module_info_text(&result))?;
} else {
writer.write(&result)?;
}
Ok(())
}
}