offline-cli 0.3.3

The Offline Protocol developer CLI
//! `offline login` / `logout` / `whoami`: device-flow auth (see docs/AUTH.md).

use anyhow::Result;
use clap::Args;

use crate::auth::login::{LoginOptions, LogoutOptions, WhoamiOptions};

#[derive(Args)]
pub struct LoginArgs {
    /// Sign in with an application API key instead of a developer session. For CI only: it
    /// can't run `offline setup`, `orgs`, `apps` or `keys`.
    #[arg(long)]
    pub api_key: Option<String>,
    /// Sign in with an emailed code instead of the browser, for SSH and agents. Pass an address or
    /// omit the value to be prompted. You get the same developer session as the browser login.
    #[arg(long, conflicts_with = "no_browser", num_args = 0..=1, default_missing_value = "")]
    pub email: Option<String>,
    /// The code from the email, to finish `--email` without a prompt. Without a terminal,
    /// `offline login --email you@example.com` sends the code and exits; rerun with `--code`.
    #[arg(long, requires = "email")]
    pub code: Option<String>,
    /// Don't open a browser; print the developer portal URL to complete login (same session as the browser login)
    #[arg(long, conflicts_with = "email")]
    pub no_browser: bool,
}

#[derive(Args)]
pub struct LogoutArgs {
    /// Only clear local credentials; skip server-side key revocation
    #[arg(long)]
    pub local_only: bool,
}

#[derive(Args)]
pub struct WhoamiArgs {
    /// Output JSON
    #[arg(long)]
    pub json: bool,
    /// Validate the stored API key and app id against the developer API
    #[arg(long)]
    pub verify: bool,
}

pub async fn login(args: LoginArgs) -> Result<()> {
    crate::auth::login::run_login(LoginOptions {
        api_key: args.api_key,
        email: args.email,
        code: args.code,
        no_browser: args.no_browser,
    })
    .await
}

pub async fn logout(args: LogoutArgs) -> Result<()> {
    crate::auth::login::run_logout(LogoutOptions {
        local_only: args.local_only,
    })
    .await
}

pub async fn whoami(args: WhoamiArgs) -> Result<()> {
    crate::auth::login::run_whoami(WhoamiOptions {
        json: args.json,
        verify: args.verify,
    })
    .await
}