Skip to main content

context7_cli/
lib.rs

1//! context7-cli library crate.
2//!
3//! Exposes the public module hierarchy and the top-level [`run`] entry point
4//! used by the binary crate in `src/main.rs`.
5//!
6//! # Module overview
7//!
8//! | Module | Responsibility |
9//! |---|---|
10//! | [`errors`] | Structured error types ([`errors::ErroContext7`]) |
11//! | [`i18n`] | Bilingual i18n (EN/PT) — [`i18n::Mensagem`] variants and [`i18n::t`] lookup |
12//! | [`storage`] | XDG config storage, four-layer key hierarchy, `keys` subcommand operations |
13//! | [`api`] | HTTP client, retry-with-rotation, Context7 API calls and response types |
14//! | [`output`] | All terminal output — the **only** module allowed to use `println!` |
15//! | [`cli`] | Clap structs, subcommand dispatchers |
16
17pub mod api;
18pub mod cli;
19pub mod errors;
20pub mod i18n;
21pub mod output;
22pub mod platform;
23pub mod storage;
24
25use anyhow::{Context, Result};
26use clap::{CommandFactory, Parser};
27use std::path::PathBuf;
28use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
29
30use cli::{Cli, Comando};
31
32// ─── LOGGING ─────────────────────────────────────────────────────────────────
33
34/// Wraps the `WorkerGuard` from `tracing-appender`.
35///
36/// **Must** be kept alive until the end of `main()` to guarantee that the
37/// non-blocking log writer flushes its buffer before the process exits.
38pub struct GuardaLog(#[allow(dead_code)] tracing_appender::non_blocking::WorkerGuard);
39
40/// Initialises dual logging: terminal (stderr with ANSI) and log file.
41///
42/// Deletes the previous log file before starting (rotation-by-deletion).
43/// Returns [`GuardaLog`] — the caller **must** keep it alive until exit.
44pub fn inicializar_logging() -> Result<GuardaLog> {
45    const NOME_BINARIO: &str = env!("CARGO_PKG_NAME");
46
47    // Attempt XDG state/log directory; fall back to relative `logs/`
48    let pasta_logs = storage::descobrir_caminho_logs_xdg().unwrap_or_else(|| {
49        let raiz_compile = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
50        if raiz_compile.join("Cargo.toml").exists() {
51            raiz_compile.join("logs")
52        } else {
53            PathBuf::from("logs")
54        }
55    });
56
57    let caminho_log = pasta_logs.join(format!("{}.log", NOME_BINARIO));
58
59    // Rotation by deletion: remove previous log before initialising
60    if caminho_log.exists() {
61        std::fs::remove_file(&caminho_log)
62            .with_context(|| format!("Falha ao deletar log anterior: {}", caminho_log.display()))?;
63    }
64
65    std::fs::create_dir_all(&pasta_logs)
66        .with_context(|| format!("Falha ao criar pasta de logs: {}", pasta_logs.display()))?;
67
68    let appender_arquivo =
69        tracing_appender::rolling::never(&pasta_logs, format!("{}.log", NOME_BINARIO));
70    let (escritor_nao_bloqueante, guard) = tracing_appender::non_blocking(appender_arquivo);
71
72    // Respect RUST_LOG; otherwise default to "context7=info"
73    let filtro = EnvFilter::try_from_default_env()
74        .unwrap_or_else(|_| EnvFilter::new(format!("{}=info", NOME_BINARIO)));
75
76    let camada_terminal = tracing_subscriber::fmt::layer()
77        .with_ansi(true)
78        .with_target(false)
79        .with_writer(std::io::stderr);
80
81    let camada_arquivo = tracing_subscriber::fmt::layer()
82        .with_ansi(false)
83        .with_target(true)
84        .with_writer(escritor_nao_bloqueante);
85
86    tracing_subscriber::registry()
87        .with(filtro)
88        .with(camada_terminal)
89        .with(camada_arquivo)
90        .init();
91
92    Ok(GuardaLog(guard))
93}
94
95// ─── ENTRY POINT ─────────────────────────────────────────────────────────────
96
97/// Main library entry point called from `src/main.rs`.
98///
99/// Parses CLI arguments, initialises the i18n language setting, then
100/// dispatches to the appropriate subcommand handler.
101/// Returns `Ok(())` on success or propagates any `anyhow::Error`.
102pub async fn run() -> Result<()> {
103    let args = Cli::parse();
104
105    // Resolve and lock the UI language as early as possible so every
106    // downstream call to `i18n::t()` sees a consistent language.
107    let idioma = i18n::resolver_idioma(args.lang.as_deref());
108    i18n::definir_idioma(idioma);
109
110    // Respeitar convenções de cores: NO_COLOR (qualquer valor) desabilita
111    if std::env::var("NO_COLOR").is_ok() {
112        colored::control::set_override(false);
113    }
114    // CLICOLOR_FORCE=1 força cores mesmo em pipe
115    if std::env::var("CLICOLOR_FORCE")
116        .map(|v| v == "1")
117        .unwrap_or(false)
118    {
119        colored::control::set_override(true);
120    }
121    // Flags CLI têm prioridade sobre env vars
122    if args.no_color || args.json || args.plain {
123        colored::control::set_override(false);
124    }
125
126    tokio::select! {
127        resultado = async {
128            match args.comando {
129                Comando::Keys { operacao } => cli::executar_keys(operacao, args.json),
130
131                Comando::Library { name, query } => cli::executar_library(name, query, args.json).await,
132
133                Comando::Docs {
134                    library_id,
135                    query,
136                    text,
137                } => cli::executar_docs(library_id, query, text, args.json).await,
138
139                Comando::Completions { shell } => {
140                    clap_complete::generate(
141                        shell,
142                        &mut cli::Cli::command(),
143                        "context7",
144                        &mut std::io::stdout(),
145                    );
146                    Ok(())
147                }
148            }
149        } => resultado,
150        _ = tokio::signal::ctrl_c() => {
151            tracing::warn!("Interrompido pelo usuário (Ctrl+C)");
152            std::process::exit(130)
153        }
154    }
155}
156
157// ─── TESTES ───────────────────────────────────────────────────────────────────
158
159#[cfg(test)]
160mod testes {
161    /// Smoke test: verify that the Duration import from tokio::time compiles correctly.
162    /// This guards against accidental removal of tokio::time re-exports.
163    #[test]
164    fn testa_duration_disponivel() {
165        let _ = tokio::time::Duration::from_millis(500);
166    }
167}