context7-cli 0.5.0

Search library documentation from your terminal — zero runtime, bilingual (EN/PT), multi-key rotation
Documentation
//! context7-cli library crate.
//!
//! Exposes the public module hierarchy and the top-level [`run`] entry point
//! used by the binary crate in `src/main.rs`.
//!
//! # Module overview
//!
//! | Module | Responsibility |
//! |---|---|
//! | [`errors`] | Structured error types ([`errors::ErroContext7`]) |
//! | [`i18n`] | Bilingual i18n (EN/PT) — [`i18n::Mensagem`] variants and [`i18n::t`] lookup |
//! | [`storage`] | XDG config storage, four-layer key hierarchy, `keys` subcommand operations |
//! | [`api`] | HTTP client, retry-with-rotation, Context7 API calls and response types |
//! | [`output`] | All terminal output — the **only** module allowed to use `println!` |
//! | [`cli`] | Clap structs, subcommand dispatchers |

pub mod api;
pub mod cli;
pub mod errors;
pub mod i18n;
pub mod output;
pub mod platform;
pub mod storage;

use anyhow::{Context, Result};
use clap::{CommandFactory, Parser};
use std::path::PathBuf;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

use cli::{Cli, Comando};

// ─── LOGGING ─────────────────────────────────────────────────────────────────

/// Wraps the `WorkerGuard` from `tracing-appender`.
///
/// **Must** be kept alive until the end of `main()` to guarantee that the
/// non-blocking log writer flushes its buffer before the process exits.
pub struct GuardaLog(#[allow(dead_code)] tracing_appender::non_blocking::WorkerGuard);

/// Initialises dual logging: terminal (stderr with ANSI) and log file.
///
/// Deletes the previous log file before starting (rotation-by-deletion).
/// Returns [`GuardaLog`] — the caller **must** keep it alive until exit.
pub fn inicializar_logging() -> Result<GuardaLog> {
    const NOME_BINARIO: &str = env!("CARGO_PKG_NAME");

    // Attempt XDG state/log directory; fall back to relative `logs/`
    let pasta_logs = storage::descobrir_caminho_logs_xdg().unwrap_or_else(|| {
        let raiz_compile = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        if raiz_compile.join("Cargo.toml").exists() {
            raiz_compile.join("logs")
        } else {
            PathBuf::from("logs")
        }
    });

    let caminho_log = pasta_logs.join(format!("{}.log", NOME_BINARIO));

    // Rotation by deletion: remove previous log before initialising
    if caminho_log.exists() {
        std::fs::remove_file(&caminho_log)
            .with_context(|| format!("Falha ao deletar log anterior: {}", caminho_log.display()))?;
    }

    std::fs::create_dir_all(&pasta_logs)
        .with_context(|| format!("Falha ao criar pasta de logs: {}", pasta_logs.display()))?;

    let appender_arquivo =
        tracing_appender::rolling::never(&pasta_logs, format!("{}.log", NOME_BINARIO));
    let (escritor_nao_bloqueante, guard) = tracing_appender::non_blocking(appender_arquivo);

    // Respect RUST_LOG; otherwise default to "context7=info"
    let filtro = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(format!("{}=info", NOME_BINARIO)));

    let camada_terminal = tracing_subscriber::fmt::layer()
        .with_ansi(true)
        .with_target(false)
        .with_writer(std::io::stderr);

    let camada_arquivo = tracing_subscriber::fmt::layer()
        .with_ansi(false)
        .with_target(true)
        .with_writer(escritor_nao_bloqueante);

    tracing_subscriber::registry()
        .with(filtro)
        .with(camada_terminal)
        .with(camada_arquivo)
        .init();

    Ok(GuardaLog(guard))
}

// ─── ENTRY POINT ─────────────────────────────────────────────────────────────

/// Main library entry point called from `src/main.rs`.
///
/// Parses CLI arguments, initialises the i18n language setting, then
/// dispatches to the appropriate subcommand handler.
/// Returns `Ok(())` on success or propagates any `anyhow::Error`.
pub async fn run() -> Result<()> {
    let args = Cli::parse();

    // Resolve and lock the UI language as early as possible so every
    // downstream call to `i18n::t()` sees a consistent language.
    let idioma = i18n::resolver_idioma(args.lang.as_deref());
    i18n::definir_idioma(idioma);

    // Respeitar convenções de cores: NO_COLOR (qualquer valor) desabilita
    if std::env::var("NO_COLOR").is_ok() {
        colored::control::set_override(false);
    }
    // CLICOLOR_FORCE=1 força cores mesmo em pipe
    if std::env::var("CLICOLOR_FORCE")
        .map(|v| v == "1")
        .unwrap_or(false)
    {
        colored::control::set_override(true);
    }
    // Flags CLI têm prioridade sobre env vars
    if args.no_color || args.json || args.plain {
        colored::control::set_override(false);
    }

    tokio::select! {
        resultado = async {
            match args.comando {
                Comando::Keys { operacao } => cli::executar_keys(operacao, args.json),

                Comando::Library { name, query } => cli::executar_library(name, query, args.json).await,

                Comando::Docs {
                    library_id,
                    query,
                    text,
                } => cli::executar_docs(library_id, query, text, args.json).await,

                Comando::Completions { shell } => {
                    clap_complete::generate(
                        shell,
                        &mut cli::Cli::command(),
                        "context7",
                        &mut std::io::stdout(),
                    );
                    Ok(())
                }
            }
        } => resultado,
        _ = tokio::signal::ctrl_c() => {
            tracing::warn!("Interrompido pelo usuário (Ctrl+C)");
            std::process::exit(130)
        }
    }
}

// ─── TESTES ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod testes {
    /// Smoke test: verify that the Duration import from tokio::time compiles correctly.
    /// This guards against accidental removal of tokio::time re-exports.
    #[test]
    fn testa_duration_disponivel() {
        let _ = tokio::time::Duration::from_millis(500);
    }
}