use std::error::Error;
use std::io::{self, Write};
use open;
use orign::config::GlobalConfig;
use rpassword;
pub async fn execute() -> Result<(), Box<dyn Error>> {
let hub_address = "https://hub.agentlabs.xyz";
let url = format!("{}/cli-login", hub_address);
println!("\nVisit {} to get an API key\n", url);
if let Err(e) = open::that(&url) {
eprintln!("Failed to open browser: {}", e);
}
print!("Enter your API key: ");
io::stdout().flush()?;
let api_key = rpassword::read_password()?;
let mut config = GlobalConfig::read()?;
config.api_key = Some(api_key);
config.server = Some("https://orign.agentlabs.xyz".to_string());
config.write()?;
println!("\nLogin successful!");
Ok(())
}