use crate::cmds::accounts::get_current_account;
use clap::Subcommand;
use ordinary_api::client::OrdinaryApiClient;
use serde_json::Value;
#[derive(Clone, Debug)]
pub enum UuidVersion {
V4,
V7,
}
impl UuidVersion {
fn as_str(&self) -> &'static str {
match self {
Self::V4 => "v4",
Self::V7 => "v7",
}
}
}
impl clap::ValueEnum for UuidVersion {
fn value_variants<'a>() -> &'a [Self] {
&[Self::V4, Self::V7]
}
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
match self {
Self::V4 => Some(clap::builder::PossibleValue::new("v4")),
Self::V7 => Some(clap::builder::PossibleValue::new("v7")),
}
}
}
#[derive(Subcommand, Debug)]
pub enum Models {
Add {
name: String,
uuid_version: Option<UuidVersion>,
},
Items {
#[command(subcommand)]
item: Items,
},
}
#[derive(Subcommand, Debug)]
pub enum Items {
List {
name: String,
#[arg(long, default_value_t = false)]
json: bool,
},
}
impl Models {
pub async fn handle(
&self,
api_domain: Option<&str>,
accept_invalid_certs: bool,
project: &str,
insecure: bool,
) -> anyhow::Result<()> {
let account = get_current_account(insecure)?;
let client = OrdinaryApiClient::new(
&account.host,
&account.name,
api_domain,
accept_invalid_certs,
crate::USER_AGENT,
)?;
match self {
Self::Add { name, uuid_version } => {
ordinary_modify::add_model(
project,
name,
uuid_version.to_owned().map(|uv| uv.as_str()),
)?;
}
Self::Items { item } => match item {
Items::List { name, json } => {
let res = client.items_list(project, name).await?;
if json == &true {
print!("{res}");
} else {
let items: Vec<Value> = serde_json::from_str(&res)?;
for item in items {
tracing::info!(%item);
}
}
}
},
}
Ok(())
}
}