Skip to main content

codeberg_cli/actions/auth/
logout.rs

1use crate::actions::GlobalArgs;
2use crate::paths::token_path;
3use crate::render::ui::confirm_with_prompt;
4use crate::types::context::BergContext;
5
6use clap::Parser;
7use miette::{Context, IntoDiagnostic};
8
9/// Logout. Delete currently stored authentication token
10#[derive(Parser, Debug)]
11pub struct LogoutArgs {
12    /// flag that skips the confirmation dialog if set
13    #[arg(short, long)]
14    pub skip_confirmation: bool,
15}
16
17impl LogoutArgs {
18    pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
19        let _ = global_args;
20        let ctx = BergContext::new(self, global_args).await?;
21        if !ctx.args.skip_confirmation
22            && !confirm_with_prompt(
23                "Logging out deletes your current token. Do you want to proceed?",
24            )?
25        {
26            return Ok(());
27        }
28
29        let token_path = token_path(ctx.config.base_url)?;
30
31        std::fs::remove_file(token_path)
32            .into_diagnostic()
33            .context("Failed to remove token!")?;
34
35        Ok(())
36    }
37}