summaryrefslogtreecommitdiffstats
path: root/ipc/examples/assuan-client.rs
blob: 5fb0dc93aea67c674c32476b24f44a51a8800c53 (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
use std::path::PathBuf;

use clap::CommandFactory;
use clap::FromArgMatches;
use clap::Parser;

use futures::StreamExt;
use sequoia_ipc as ipc;
use crate::ipc::assuan::Client;

use sequoia_openpgp as openpgp;
use openpgp::Result;

/// Defines the CLI.
#[derive(Parser, Debug)]
#[clap(
    name = "assuan-client",
    about = "Connects to and sends commands to assuan servers.",
)]
pub struct Cli {
    #[clap(
        long,
        value_name = "PATH",
        help = "Server to connect to",
    )]
    server: PathBuf,

    #[clap(
        long,
        value_name = "COMMAND",
        help = "Commands to send to the server",
        required = true,
    )]
    commands: Vec<String>,
}

fn main() -> Result<()> {
    let version = format!(
        "{} (sequoia-openpgp {}, using {})",
        env!("CARGO_PKG_VERSION"),
        sequoia_openpgp::VERSION,
        sequoia_openpgp::crypto::backend()
    );
    let cli = Cli::command().version(version);
    let matches = Cli::from_arg_matches(&cli.get_matches())?;

    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let mut c = Client::connect(matches.server).await.unwrap();
        for command in matches.commands {
            eprintln!("> {}", command);
            c.send(command).unwrap();
            while let Some(response) = c.next().await {
                eprintln!("< {:?}", response);
            }
        }
    });

    Ok(())
}