use clap::Subcommand;
use gitflow_core::{CliOutput, auth::AuthProvider};
use gitflow_gitcode::GitCodeAuthProvider;
use gitflow_github::GitHubAuthProvider;
use gitflow_gitlab::GitLabAuthProvider;
use crate::OutputFormat;
#[derive(Debug, Subcommand)]
pub enum AuthCommand {
Login {
#[arg(long)]
token: Option<String>,
},
Logout,
Status,
Token,
}
#[allow(
clippy::too_many_lines,
reason = "Command dispatch: each match arm maps to one operation"
)]
pub async fn handle(
command: AuthCommand,
platform: &str,
repo: &str,
output_format: OutputFormat,
) -> miette::Result<()> {
let provider: Box<dyn AuthProvider> = match platform {
"github" => Box::new(GitHubAuthProvider::new()),
"gitlab" => Box::new(GitLabAuthProvider::new()),
"gitcode" => Box::new(GitCodeAuthProvider::new()),
other => {
return Err(miette::miette!(
"Platform '{other}' not yet supported for auth commands"
));
}
};
let _ = repo;
match command {
AuthCommand::Login { token } => {
provider
.login(token.as_deref())
.await
.map_err(|e| miette::miette!("Failed to login: {e}"))?;
let result = serde_json::json!({
"action": "login",
"success": true,
});
let output = CliOutput::success(result, platform, "auth login");
print_output(&output, &output_format)?;
}
AuthCommand::Logout => {
provider
.logout()
.await
.map_err(|e| miette::miette!("Failed to logout: {e}"))?;
let result = serde_json::json!({
"action": "logout",
"success": true,
});
let output = CliOutput::success(result, platform, "auth logout");
print_output(&output, &output_format)?;
}
AuthCommand::Status => {
let status = provider
.status()
.await
.map_err(|e| miette::miette!("Failed to get auth status: {e}"))?;
let output = CliOutput::success(status, platform, "auth status");
print_output(&output, &output_format)?;
}
AuthCommand::Token => {
let token = provider
.token()
.await
.map_err(|e| miette::miette!("Failed to get token: {e}"))?;
let output = CliOutput::success(
serde_json::json!({ "token": token }),
platform,
"auth token",
);
print_output(&output, &output_format)?;
}
}
Ok(())
}
fn print_output<T: serde::Serialize>(value: &T, format: &OutputFormat) -> miette::Result<()> {
crate::commands::output::print_output(value, format)
}
#[cfg(test)]
#[allow(
clippy::panic,
reason = "Test code: panic is acceptable for assertion failures"
)]
mod tests {
use super::*;
#[test]
fn test_should_print_json_output() {
let value = serde_json::json!({"action": "login", "success": true});
let result = print_output(&value, &OutputFormat::Json);
assert!(result.is_ok());
}
#[test]
fn test_should_accept_text_output() {
let value = serde_json::json!({"number": 1});
let result = print_output(&value, &OutputFormat::Text);
assert!(result.is_ok());
}
#[test]
fn test_should_print_toon_output() {
let value = serde_json::json!({"action": "login", "success": true});
let result = print_output(&value, &OutputFormat::Toon);
assert!(result.is_ok());
}
#[test]
fn test_should_print_auto_output() {
let value = serde_json::json!({"action": "login", "success": true});
let result = print_output(&value, &OutputFormat::Auto);
assert!(result.is_ok());
}
#[test]
fn test_should_parse_auth_login() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "auth", "login"]).expect("parse");
match cli.command {
crate::Commands::Auth(AuthCommand::Login { token }) => {
assert!(token.is_none());
}
_ => panic!("Expected AuthCommand::Login"),
}
}
#[test]
fn test_should_parse_auth_login_with_token() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "auth", "login", "--token", "my-token"])
.expect("parse");
match cli.command {
crate::Commands::Auth(AuthCommand::Login { token }) => {
assert_eq!(token, Some("my-token".to_string()));
}
_ => panic!("Expected AuthCommand::Login"),
}
}
#[test]
fn test_should_parse_auth_logout() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "auth", "logout"]).expect("parse");
match cli.command {
crate::Commands::Auth(AuthCommand::Logout) => {}
_ => panic!("Expected AuthCommand::Logout"),
}
}
#[test]
fn test_should_parse_auth_status() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "auth", "status"]).expect("parse");
match cli.command {
crate::Commands::Auth(AuthCommand::Status) => {}
_ => panic!("Expected AuthCommand::Status"),
}
}
#[test]
fn test_should_parse_auth_token() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "auth", "token"]).expect("parse");
match cli.command {
crate::Commands::Auth(AuthCommand::Token) => {}
_ => panic!("Expected AuthCommand::Token"),
}
}
}