use clap::{
Args,
Subcommand,
};
use super::context::Context;
pub use export_key::ExportKey;
pub use generate_key::GenerateKey;
pub use import_key::ImportKey;
pub use list_keys::ListKeys;
pub use purge_key::PurgeKey;
mod export_key;
mod generate_key;
mod import_key;
mod list_keys;
mod purge_key;
pub const KEY_LOCATION_HELP: &str = "The path to where the private keys are stored";
#[derive(Debug, Args)]
pub struct Key {
#[command(subcommand)]
command: Option<KeyCommand>,
#[arg(short, long, help = KEY_LOCATION_HELP)]
location: Option<String>,
}
#[derive(Debug, Subcommand)]
enum KeyCommand {
#[command(visible_aliases = ["gen"], about = "Generate a new private key")]
GenerateKey(GenerateKey),
#[command(visible_aliases = ["K", "ls"], about = "List all private keys")]
ListKeys(ListKeys),
#[command(visible_aliases = ["rm"], about = "Purge and remove a private key")]
PurgeKey(PurgeKey),
#[command(visible_aliases = ["export", "e"], about = "Export selected private key")]
ExportKey(ExportKey),
#[command(visible_aliases = ["import"], about = "Import a GPG key reference from the local keyring")]
ImportKey(ImportKey),
}
impl Key {
pub fn execute(&self, context: &mut Context) -> anyhow::Result<()> {
if let Some(location) = &self.location {
context.set_keys_location(location);
}
match &self.command {
Some(command) => command.run(context),
None => Err(anyhow::anyhow!(
"No key subcommand given. Run 'mk secrets key --help' to see available subcommands."
)),
}
}
}
impl KeyCommand {
pub fn run(&self, context: &Context) -> anyhow::Result<()> {
match self {
KeyCommand::GenerateKey(args) => args.execute(context),
KeyCommand::ListKeys(args) => args.execute(context),
KeyCommand::PurgeKey(args) => args.execute(context),
KeyCommand::ExportKey(args) => args.execute(context),
KeyCommand::ImportKey(args) => args.execute(context),
}
}
}