upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/logout.rs
blob: 682c017e2dfc0936c8ce8d062e30c1dddd2d186a (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use anyhow::{Context, Result};
use ngit::{
    git::remove_git_config_item,
    login::{existing::load_existing_login, SignerInfoSource},
};

use crate::{
    git::Repo,
    sub_commands::login::{format_items_as_list, get_global_login_config_items_set},
};

pub async fn launch() -> Result<()> {
    let git_repo_result = Repo::discover().context("failed to find a git repository");
    let git_repo = {
        match git_repo_result {
            Ok(git_repo) => Some(git_repo),
            Err(_) => None,
        }
    };
    logout(git_repo.as_ref()).await
}

async fn logout(git_repo: Option<&Repo>) -> Result<()> {
    for source in if std::env::var("NGITTEST").is_ok() {
        vec![SignerInfoSource::GitLocal]
    } else {
        vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal]
    } {
        if let Ok((_, user_ref, source)) =
            load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await
        {
            for item in [
                "nostr.nsec",
                "nostr.npub",
                "nostr.bunker-uri",
                "nostr.bunker-app-key",
            ] {
                if let Err(error) = remove_git_config_item(
                    if source == SignerInfoSource::GitLocal {
                        &git_repo
                    } else {
                        &None
                    },
                    item,
                ) {
                    println!(
                        "failed to log out {}as {}",
                        if source == SignerInfoSource::GitLocal {
                            "from local git repository "
                        } else {
                            ""
                        },
                        user_ref.metadata.name
                    );
                    eprintln!("{error:?}");
                    eprintln!(
                        "consider manually removing {} git config items: {}",
                        if source == SignerInfoSource::GitGlobal {
                            "global"
                        } else {
                            "local"
                        },
                        format_items_as_list(&get_global_login_config_items_set())
                    );
                    return Ok(());
                }
            }
            println!(
                "logged out {}as {}",
                if source == SignerInfoSource::GitLocal {
                    "from local git repository "
                } else {
                    ""
                },
                user_ref.metadata.name
            );
            return Ok(());
        }
    }
    Ok(())
}