arti/subcommands/
raw.rs

1//! Low-level, plumbing subcommands.
2
3use std::str::FromStr;
4
5use anyhow::Result;
6use clap::{ArgMatches, Args, FromArgMatches, Parser, Subcommand};
7
8use arti_client::{InertTorClient, TorClient, TorClientConfig};
9use tor_keymgr::KeystoreId;
10use tor_rtcompat::Runtime;
11
12/// The `keys-raw` subcommands the arti CLI will be augmented with.
13#[derive(Debug, Parser)]
14pub(crate) enum RawSubcommands {
15    /// Run plumbing key management commands.
16    #[command(subcommand)]
17    KeysRaw(RawSubcommand),
18}
19
20#[derive(Subcommand, Debug, Clone)]
21pub(crate) enum RawSubcommand {
22    /// Remove keystore entry by raw ID.
23    RemoveById(RemoveByIdArgs),
24}
25
26/// The arguments of the [`RemoveById`](RawSubcommand::RemoveById) subcommand.
27#[derive(Debug, Clone, Args)]
28pub(crate) struct RemoveByIdArgs {
29    /// The raw ID of the keystore entry to remove.
30    ///
31    /// The raw ID of an entry can be obtained from the field "Location" of the
32    /// output of `arti keys list`.
33    raw_entry_id: String,
34
35    /// Identifier of the keystore to remove the entry from.
36    /// If omitted, the primary store will be used ("arti").
37    #[arg(short, long, default_value_t = String::from("arti"))]
38    keystore_id: String,
39}
40
41/// Run the `keys-raw` subcommand.
42pub(crate) fn run<R: Runtime>(
43    runtime: R,
44    keys_matches: &ArgMatches,
45    config: &TorClientConfig,
46) -> Result<()> {
47    let subcommand =
48        RawSubcommand::from_arg_matches(keys_matches).expect("Could not parse keys subcommand");
49    let client = TorClient::with_runtime(runtime)
50        .config(config.clone())
51        .create_inert()?;
52
53    match subcommand {
54        RawSubcommand::RemoveById(args) => run_raw_remove(&args, &client),
55    }
56}
57
58/// Run `key raw-remove-by-id` subcommand.
59fn run_raw_remove(args: &RemoveByIdArgs, client: &InertTorClient) -> Result<()> {
60    let keymgr = client.keymgr()?;
61    let keystore_id = KeystoreId::from_str(&args.keystore_id)?;
62    keymgr.remove_unchecked(&args.raw_entry_id, &keystore_id)?;
63
64    Ok(())
65}