~core/sage

ref: f11e86509b6263d2d92c20f8aefdc2472a1285bf sage/src/cmds/show.rs -rw-r--r-- 1.0 KiB
f11e8650 — core initial 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
use std::path::Path;
use std::process::exit;
use crate::db::Database;
use crate::identity::{escape, IdKeyData};

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(())
}