1use bitwarden_core::ClientSettings;
2use clap::{Args, Subcommand};
3
4mod login;
5
6use crate::render::CommandResult;
7
8#[derive(Args, Clone)]
10pub struct LoginArgs {
11 #[command(subcommand)]
12 pub command: LoginCommands,
13
14 #[arg(short = 's', long, global = true, help = "Server URL")]
15 pub server: Option<String>,
16}
17
18#[derive(Subcommand, Clone)]
19pub enum LoginCommands {
20 Password {
21 #[arg(short = 'e', long, help = "Email address")]
22 email: Option<String>,
23 },
24 ApiKey {
25 client_id: Option<String>,
26 client_secret: Option<String>,
27 },
28 Device {
29 #[arg(short = 'e', long, help = "Email address")]
30 email: Option<String>,
31 device_identifier: Option<String>,
32 },
33}
34
35impl LoginArgs {
36 pub async fn run(self) -> CommandResult {
37 let settings = self.server.map(|server| ClientSettings {
38 api_url: format!("{server}/api"),
39 identity_url: format!("{server}/identity"),
40 ..Default::default()
41 });
42 let client = bitwarden_core::Client::new(settings);
43
44 match self.command {
45 LoginCommands::Password { email } => {
47 login::login_password(client, email).await?;
48 }
49 LoginCommands::ApiKey {
50 client_id,
51 client_secret,
52 } => login::login_api_key(client, client_id, client_secret).await?,
53 LoginCommands::Device {
54 email,
55 device_identifier,
56 } => {
57 login::login_device(client, email, device_identifier).await?;
58 }
59 }
60 Ok("Successfully logged in!".into())
61 }
62}