~core/sage

ref: a5ea94fd04873a203d0a5e2066ca4177c5102c93 sage/src/cmds/show.rs -rw-r--r-- 1.2 KiB
a5ea94fd — core feat: better err msg 21 days ago
                                                                                
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
use crate::db::Database;
use crate::identity::{IdKeyData, escape};
use std::path::Path;
use std::process::exit;

pub fn show_key(db_path: &Path, search: String, expose_secret: bool) -> anyhow::Result<()> {
    let db = Database::load_or_create(db_path)?;

    let matches = db.fuzzy_search(search);

    if matches.len() > 1 {
        println!("there were multiple best matches, showing all");
    } else if matches.is_empty() {
        println!("no matches found");
        exit(0);
    }

    for each_match in &matches {
        println!("match: {:?}", each_match);

        let show_sk = expose_secret && matches!(each_match.keys, IdKeyData::Local(..));

        if !show_sk {
            println!(
                "   pk: sage/{}/{}/{}",
                each_match.keys.pk()?,
                escape(each_match.name.clone()),
                escape(each_match.email.clone())
            );
        } else {
            let IdKeyData::Local(k) = &each_match.keys else {
                continue;
            };
            println!(
                "   sk: sage/{}/{}/{}",
                k,
                escape(each_match.name.clone()),
                escape(each_match.email.clone())
            );
        }
    }

    Ok(())
}