Skip to main content

asimov_cli/commands/module/
doc.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{BoxError, StandardOptions, SysexitsError::*};
4use asimov_module::ModuleName;
5use color_print::ceprintln;
6
7pub async fn doc(module_name: ModuleName, _flags: &StandardOptions) -> Result<(), BoxError> {
8    let registry = asimov_registry::Registry::default();
9
10    let readme = match registry.read_readme(&module_name).await {
11        Ok(Some(readme)) => readme,
12        Ok(None) => {
13            ceprintln!("<s,r>error:</> module `{module_name}` was installed without documentation");
14            ceprintln!(
15                "<s,dim>hint:</> Reinstall it to fetch the documentation: <s>asimov module uninstall {module_name} && asimov module install {module_name}</>"
16            );
17            ceprintln!(
18                "<s,dim>hint:</> Or try reading it online with: <s>asimov module browse {module_name}</>"
19            );
20            return Err(EX_NOINPUT.into());
21        },
22        Err(asimov_registry::error::ReadReadmeError::NotInstalled) => {
23            ceprintln!("<s,r>error:</> module `{module_name}` is not installed");
24            ceprintln!(
25                "<s,dim>hint:</> Install it with: <s>asimov module install {module_name}</>"
26            );
27            return Err(EX_UNAVAILABLE.into());
28        },
29        Err(e) => {
30            tracing::error!("failed to read README for module `{module_name}`: {e}");
31            return Err(EX_UNAVAILABLE.into());
32        },
33    };
34
35    print!("{readme}");
36    if !readme.ends_with('\n') {
37        println!();
38    }
39
40    Ok(())
41}