Skip to main content

crates_docs/cli/
mod.rs

1//! CLI module
2//!
3//! Command-line interface for the Crates Docs MCP Server.
4
5mod api_key_cmd;
6mod commands;
7mod config_cmd;
8mod health_cmd;
9mod list_api_keys_cmd;
10mod revoke_api_key_cmd;
11mod serve_cmd;
12mod test_cmd;
13mod version_cmd;
14
15use clap::Parser;
16use std::path::PathBuf;
17
18pub use api_key_cmd::run_generate_api_key_command;
19pub use commands::Commands;
20pub use config_cmd::run_config_command;
21pub use health_cmd::run_health_command;
22pub use list_api_keys_cmd::run_list_api_keys_command;
23pub use revoke_api_key_cmd::run_revoke_api_key_command;
24pub use serve_cmd::run_serve_command;
25pub use test_cmd::run_test_command;
26pub use version_cmd::run_version_command;
27
28/// CLI configuration
29#[derive(Parser)]
30#[command(name = "crates-docs")]
31#[command(version = env!("CARGO_PKG_VERSION"))]
32#[command(about = "High-performance Rust crate documentation query MCP server", long_about = None)]
33pub struct Cli {
34    /// CLI command to execute
35    #[command(subcommand)]
36    pub command: Commands,
37
38    /// Configuration file path
39    #[arg(short, long, global = true, default_value = "config.toml")]
40    pub config: PathBuf,
41
42    /// Enable debug logging
43    #[arg(short, long, global = true)]
44    pub debug: bool,
45
46    /// Enable verbose output (debug-level logging when serving)
47    #[arg(short, long, global = true)]
48    pub verbose: bool,
49}
50
51/// Run the CLI application
52pub async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
53    match cli.command {
54        Commands::Serve {
55            mode,
56            host,
57            port,
58            enable_oauth,
59            oauth_client_id,
60            oauth_client_secret,
61            oauth_redirect_uri,
62            enable_api_key,
63            api_keys,
64            api_key_header,
65            api_key_query_param,
66        } => {
67            run_serve_command(
68                &cli.config,
69                cli.debug,
70                cli.verbose,
71                mode,
72                host,
73                port,
74                enable_oauth,
75                oauth_client_id,
76                oauth_client_secret,
77                oauth_redirect_uri,
78                enable_api_key,
79                api_keys,
80                api_key_header,
81                api_key_query_param,
82            )
83            .await?;
84        }
85        Commands::GenerateApiKey { prefix } => {
86            run_generate_api_key_command(&prefix)?;
87        }
88        Commands::ListApiKeys { config } => {
89            run_list_api_keys_command(&config)?;
90        }
91        Commands::RevokeApiKey { config, key } => {
92            run_revoke_api_key_command(&config, &key)?;
93        }
94        Commands::Config { output, force } => {
95            run_config_command(&output, force)?;
96        }
97        Commands::Test {
98            tool,
99            crate_name,
100            item_path,
101            query,
102            sort,
103            version,
104            limit,
105            format,
106        } => {
107            run_test_command(
108                &cli.config,
109                &tool,
110                crate_name.as_deref(),
111                item_path.as_deref(),
112                query.as_deref(),
113                sort.as_deref(),
114                version.as_deref(),
115                limit,
116                &format,
117            )
118            .await?;
119        }
120        Commands::Health {
121            check_type,
122            verbose,
123        } => {
124            run_health_command(&cli.config, &check_type, verbose).await?;
125        }
126        Commands::Version => {
127            run_version_command();
128        }
129    }
130
131    Ok(())
132}