refactor: simplify command structure for Login and Import in CLI

This commit is contained in:
2026-04-30 09:59:36 +08:00
parent 87a2a85dd7
commit 7120a8a904
2 changed files with 36 additions and 14 deletions
+28 -8
View File
@@ -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<LoginCommands>,
#[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<ImportCommands>,
#[arg(long)]
pub file: Option<PathBuf>,
#[arg(long)]
pub codex_home: Option<PathBuf>,
#[arg(long)]
pub switch: bool,
}
#[derive(Subcommand)]
pub enum LoginCommands {
/// Start OpenAI OAuth login for Codex.
+8 -6
View File
@@ -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 {