ez-token 0.1.0

CLI tool for generating OAuth2 access tokens via PKCE and Client Credentials for Microsoft Entra ID and Auth0
Documentation
//! Entry point for the `ez-token` CLI application.
//!
//! Parses CLI arguments, loads the active configuration profile, and
//! dispatches to the appropriate subcommand handler. If no subcommand
//! is provided, defaults to an interactive PKCE login using the default profile.

use clap::Parser;
use ez_token::cli::args::{AuthArgs, Cli, Commands};
use ez_token::commands;
use ez_token::config::cli_config::CliConfig;
use miette::Result;

/// Executes the primary CLI workflow.
///
/// # Errors
///
/// Returns an error if:
/// - The configuration file cannot be loaded.
/// - The selected subcommand fails (e.g., authentication error, port conflict).
#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let mut config = CliConfig::load()?;
    let profile_name = cli.profile;
    let profile = config
        .profiles
        .get(&profile_name)
        .cloned()
        .unwrap_or_default();

    match cli.command {
        None => {
            if profile.provider.is_some() {
                commands::pkce::run(profile, AuthArgs::default(), 3000).await?;
            } else {
                miette::bail!(
                    help = "Set up a default profile with `ez-token config set` or run `ez-token login` / `ez-token m2m` directly",
                    "No command provided and no default profile configured"
                );
            }
        }
        Some(Commands::Login { auth, port }) => {
            commands::pkce::run(profile, auth, port).await?;
        }
        Some(Commands::M2m {
            auth,
            client_secret,
        }) => {
            commands::client_credentials::run(profile, auth, client_secret).await?;
        }
        Some(Commands::Config { action }) => {
            commands::config::handle(action, &mut config, &profile_name)?;
        }
    }

    Ok(())
}