From 7120a8a90429bfe0f70cc97daf6aa9886ee0804b Mon Sep 17 00:00:00 2001 From: chuan Date: Thu, 30 Apr 2026 09:59:36 +0800 Subject: [PATCH] refactor: simplify command structure for Login and Import in CLI --- src/cli.rs | 36 ++++++++++++++++++++++++++++-------- src/main.rs | 14 ++++++++------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a880344..26f8056 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,15 +12,9 @@ pub struct Cli { #[derive(Subcommand)] pub enum Commands { /// Login to Codex through OpenAI OAuth. - Login { - #[command(subcommand)] - command: LoginCommands, - }, + Login(LoginArgs), /// Import an existing Codex auth file. - Import { - #[command(subcommand)] - command: ImportCommands, - }, + Import(ImportArgs), /// List saved accounts. List { #[arg(long)] @@ -73,6 +67,32 @@ pub enum Commands { }, } +#[derive(Args)] +pub struct LoginArgs { + /// Start OpenAI OAuth login for Codex. + #[command(subcommand)] + pub command: Option, + #[arg(long)] + pub manual: bool, + #[arg(long, default_value_t = 1455)] + pub port: u16, + #[arg(long)] + pub switch: bool, +} + +#[derive(Args)] +pub struct ImportArgs { + /// Import OAuth/API-key auth from auth.json. + #[command(subcommand)] + pub command: Option, + #[arg(long)] + pub file: Option, + #[arg(long)] + pub codex_home: Option, + #[arg(long)] + pub switch: bool, +} + #[derive(Subcommand)] pub enum LoginCommands { /// Start OpenAI OAuth login for Codex. diff --git a/src/main.rs b/src/main.rs index f99b768..4adbd3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,19 +30,21 @@ use crate::cli::{ async fn main() -> Result<()> { let cli = Cli::parse(); match cli.command.unwrap_or(Commands::List { json: false }) { - Commands::Login { command } => match command { - LoginCommands::Oauth { + Commands::Login(args) => match args.command { + Some(LoginCommands::Oauth { manual, port, switch, - } => oauth::login_oauth(manual, port, switch).await, + }) => oauth::login_oauth(manual, port, switch).await, + None => oauth::login_oauth(args.manual, args.port, args.switch).await, }, - Commands::Import { command } => match command { - ImportCommands::Auth { + Commands::Import(args) => match args.command { + Some(ImportCommands::Auth { file, codex_home, switch, - } => account::import_auth(file, codex_home, switch), + }) => account::import_auth(file, codex_home, switch), + None => account::import_auth(args.file, args.codex_home, args.switch), }, Commands::List { json } => account::list_accounts(json), Commands::Switch {