mod create;
mod default;
mod delete;
mod list;
mod show;
use colorful::Colorful;
pub use create::CreateCommand;
pub(crate) use delete::DeleteCommand;
pub(crate) use list::ListCommand;
pub(crate) use show::ShowCommand;
use crate::identity::default::DefaultCommand;
use crate::terminal::OckamColor;
use crate::{docs, fmt_log, fmt_ok, CommandGlobalOpts, PARSER_LOGS};
use clap::{Args, Subcommand};
use ockam_api::cli_state::traits::StateDirTrait;
use ockam_api::cli_state::CliState;
const LONG_ABOUT: &str = include_str!("./static/long_about.txt");
#[derive(Clone, Debug, Args)]
#[command(
arg_required_else_help = true,
subcommand_required = true,
long_about = docs::about(LONG_ABOUT),
)]
pub struct IdentityCommand {
#[command(subcommand)]
subcommand: IdentitySubcommand,
}
#[derive(Clone, Debug, Subcommand)]
pub enum IdentitySubcommand {
Create(CreateCommand),
Show(ShowCommand),
List(ListCommand),
Default(DefaultCommand),
Delete(DeleteCommand),
}
impl IdentityCommand {
pub fn run(self, options: CommandGlobalOpts) {
match self.subcommand {
IdentitySubcommand::Create(c) => c.run(options),
IdentitySubcommand::Show(c) => c.run(options),
IdentitySubcommand::List(c) => c.run(options),
IdentitySubcommand::Delete(c) => c.run(options),
IdentitySubcommand::Default(c) => c.run(options),
}
}
}
pub fn initialize_identity_if_default(opts: &CommandGlobalOpts, name: &Option<String>) {
let name = get_identity_name(&opts.state, name);
if name == "default" && opts.state.identities.default().is_err() {
create_default_identity(opts);
}
}
pub fn get_identity_name(cli_state: &CliState, identity_name: &Option<String>) -> String {
identity_name
.clone()
.unwrap_or_else(|| get_default_identity_name(cli_state))
}
pub fn get_default_identity_name(cli_state: &CliState) -> String {
cli_state
.identities
.default()
.map(|i| i.name().to_string())
.unwrap_or_else(|_| "default".to_string())
}
pub fn create_default_identity(opts: &CommandGlobalOpts) {
let default = "default";
let create_command = CreateCommand::new(default.into(), None);
create_command.run(opts.clone().set_quiet());
let identifier = match opts.state.identities.get(default) {
Ok(i) => i.identifier().to_string(),
Err(_) => default.to_string(),
};
if let Ok(mut logs) = PARSER_LOGS.lock() {
logs.push(fmt_log!(
"There is no identity, on this machine, marked as your default."
));
logs.push(fmt_log!("Creating a new Ockam identity for you..."));
logs.push(fmt_ok!(
"Created: {}",
identifier.color(OckamColor::PrimaryResource.color())
));
logs.push(fmt_log!(
"Marked this new identity as your default, on this machine.\n"
));
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::GlobalArgs;
use ockam_api::cli_state::StateItemTrait;
#[test]
fn test_initialize() {
let state = CliState::test().unwrap();
let opts = CommandGlobalOpts::new_for_test(GlobalArgs::default(), state);
assert!(opts.state.identities.default().is_err());
initialize_identity_if_default(&opts, &None);
assert!(opts.state.identities.default().is_ok());
opts.state.identities.default().unwrap().delete().unwrap();
initialize_identity_if_default(&opts, &Some("default".into()));
assert!(opts.state.identities.default().is_ok());
opts.state.identities.default().unwrap().delete().unwrap();
initialize_identity_if_default(&opts, &Some("other".into()));
assert!(opts.state.identities.default().is_err());
}
}