use anyhow::Result;
use clap::{Args, Command, Subcommand};
#[derive(Subcommand, Debug)]
pub enum AuthCmd {
Login,
Logout,
Status,
}
#[derive(Args, Debug)]
pub struct AuthArgs {
#[command(subcommand)]
pub cmd: AuthCmd,
}
impl AuthCmd {
#[must_use]
pub fn command() -> Command {
Command::new("auth")
.about("Authentication commands")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(Command::new("login").about("Log in to OpenCrates"))
.subcommand(Command::new("logout").about("Log out from OpenCrates"))
.subcommand(Command::new("status").about("Show current auth status"))
}
pub async fn run(&self) -> Result<()> {
match self {
AuthCmd::Login => {
println!("đ Authenticating with OpenAI...");
if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
if !api_key.is_empty() && api_key != "your_openai_api_key_here" {
println!("â
OpenAI API key found and appears valid");
println!("đ Authentication successful!");
} else {
println!("â OpenAI API key is empty or using placeholder value");
println!("đĄ Please set your OPENAI_API_KEY environment variable");
}
} else {
println!("â OPENAI_API_KEY not found in environment");
println!("đĄ Please set your OPENAI_API_KEY environment variable");
println!("đĄ You can add it to your .env file in the project root");
}
Ok(())
}
AuthCmd::Logout => {
println!("đ Logging out from OpenCrates...");
println!("âšī¸ Note: This removes cached credentials but doesn't modify environment variables");
println!("â
Logout completed");
Ok(())
}
AuthCmd::Status => {
println!("đ Checking authentication status...");
if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
if !api_key.is_empty() && api_key != "your_openai_api_key_here" {
println!("â
Authenticated with OpenAI");
println!("đ API Key: {}...{}",
&api_key[..std::cmp::min(8, api_key.len())],
&api_key[std::cmp::max(0, api_key.len() - 4)..]);
} else {
println!("â Not authenticated - API key is empty or placeholder");
}
} else {
println!("â Not authenticated - OPENAI_API_KEY not set");
}
if std::env::var("ANTHROPIC_API_KEY").is_ok() {
println!("â
Anthropic API key also configured");
}
Ok(())
}
}
}
}