~core/sage

ref: a5ea94fd04873a203d0a5e2066ca4177c5102c93 sage/src/cmds/ls.rs -rw-r--r-- 1.3 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::db::Database;
use crate::identity::{IdKeyData, escape};
use std::path::Path;
use std::process::exit;

pub fn list_keys(
    db_path: &Path,
    local_only: bool,
    peer_only: bool,
    full_pks: bool,
) -> anyhow::Result<()> {
    if local_only && peer_only {
        eprintln!("cannot show only local and only peer keys, pick one");
        exit(1);
    }
    let show_local = !peer_only;
    let show_peer = !full_pks;

    let db = Database::load_or_create(db_path)?;

    let mut displayed = 0;

    for key in &db.keys {
        match key.keys {
            IdKeyData::Local(_) => {
                if !show_local {
                    continue;
                };
                displayed += 1;
                println!("local: {:?}", key);
            }
            IdKeyData::Peer(_) => {
                if !show_peer {
                    continue;
                };
                displayed += 1;
                println!("peer: {:?}", key);
            }
        }

        if full_pks {
            println!(
                "   pk: sage/{}/{}/{}",
                key.keys.pk()?,
                escape(key.name.clone()),
                escape(key.email.clone())
            );
        } else {
            println!("   id: {}", key.keys.keyid()?);
        }
    }

    if displayed == 0 {
        println!("no keys found");
    }

    Ok(())
}