aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/command/client/account/delete.rs
blob: 722c39ece9ae58bd7761db59d4f8f0b6608c683c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::atuin_client::{auth, settings::Settings};
use clap::Parser;
use eyre::{Result, bail};

use super::login::read_user_password;

#[derive(Parser, Debug)]
pub(crate) struct Cmd {
    #[clap(long, short)]
    pub(crate) password: Option<String>,

    /// The two-factor authentication code for your account, if any
    #[clap(long, short)]
    pub(crate) totp_code: Option<String>,
}

impl Cmd {
    pub(crate) async fn run(&self, settings: &Settings) -> Result<()> {
        if !settings.logged_in().await? {
            bail!("You are not logged in");
        }

        let client = auth::auth_client(settings).await;

        let password = self.password.clone().unwrap_or_else(read_user_password);

        if password.is_empty() {
            bail!("please provide your password");
        }

        let mut totp_code = self.totp_code.clone();

        client
            .delete_account(&password, totp_code.as_deref())
            .await?;

        // Clean up sessions from meta store
        let meta = Settings::meta_store().await?;
        meta.delete_session().await?;

        println!("Your account is deleted");

        Ok(())
    }
}