pub mod aquire;
pub mod attach;
pub mod create;
pub mod delete;
pub mod info;
pub mod list;
pub mod read;
pub mod release;
pub mod write;
use anyhow::Result;
use clap::builder::PossibleValue;
use clap::{Args, Subcommand, ValueEnum};
use nuts_container::Cipher;
use std::ops::Deref;
use crate::cli::container::aquire::ContainerAquireArgs;
use crate::cli::container::attach::ContainerAttachArgs;
use crate::cli::container::create::ContainerCreateArgs;
use crate::cli::container::delete::ContainerDeleteArgs;
use crate::cli::container::info::ContainerInfoArgs;
use crate::cli::container::list::ContainerListArgs;
use crate::cli::container::read::ContainerReadArgs;
use crate::cli::container::release::ContainerReleaseArgs;
use crate::cli::container::write::ContainerWriteArgs;
const AES128_GCM: &str = "aes128-gcm";
const AES192_GCM: &str = "aes192-gcm";
const AES256_GCM: &str = "aes256-gcm";
const AES128_CTR: &str = "aes128-ctr";
const AES192_CTR: &str = "aes192-ctr";
const AES256_CTR: &str = "aes256-ctr";
const NONE: &str = "none";
#[derive(Clone, Debug)]
pub struct CliCipher(Cipher);
impl PartialEq<Cipher> for CliCipher {
fn eq(&self, other: &Cipher) -> bool {
self.0 == *other
}
}
impl Deref for CliCipher {
type Target = Cipher;
fn deref(&self) -> &Cipher {
&self.0
}
}
impl ValueEnum for CliCipher {
fn value_variants<'a>() -> &'a [Self] {
&[
CliCipher(Cipher::Aes128Gcm),
CliCipher(Cipher::Aes192Gcm),
CliCipher(Cipher::Aes256Gcm),
CliCipher(Cipher::Aes192Ctr),
CliCipher(Cipher::Aes256Ctr),
CliCipher(Cipher::Aes128Ctr),
CliCipher(Cipher::None),
]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
let value = match self.0 {
Cipher::None => NONE,
Cipher::Aes128Ctr => AES128_CTR,
Cipher::Aes192Ctr => AES192_CTR,
Cipher::Aes256Ctr => AES256_CTR,
Cipher::Aes128Gcm => AES128_GCM,
Cipher::Aes192Gcm => AES192_GCM,
Cipher::Aes256Gcm => AES256_GCM,
};
Some(PossibleValue::new(value))
}
}
#[derive(Debug, Args)]
#[clap(args_conflicts_with_subcommands = true, subcommand_required = true)]
pub struct ContainerArgs {
#[clap(subcommand)]
command: Option<ContainerCommand>,
}
impl ContainerArgs {
pub fn run(&self) -> Result<()> {
self.command
.as_ref()
.map_or(Ok(()), |command| command.run())
}
}
#[derive(Debug, Subcommand)]
pub enum ContainerCommand {
Aquire(ContainerAquireArgs),
Attach(ContainerAttachArgs),
Create(ContainerCreateArgs),
Delete(ContainerDeleteArgs),
Info(ContainerInfoArgs),
List(ContainerListArgs),
Read(ContainerReadArgs),
Release(ContainerReleaseArgs),
Write(ContainerWriteArgs),
}
impl ContainerCommand {
pub fn run(&self) -> Result<()> {
match self {
Self::Aquire(args) => args.run(),
Self::Attach(args) => args.run(),
Self::Create(args) => args.run(),
Self::Delete(args) => args.run(),
Self::Info(args) => args.run(),
Self::List(args) => args.run(),
Self::Read(args) => args.run(),
Self::Release(args) => args.run(),
Self::Write(args) => args.run(),
}
}
}