use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{
entity::role::GroupWorkerRole,
schema::{
RemoveGroupWorkerRoleReq, ReplaceWorkerLabelsReq, ReplaceWorkerTagsReq,
UpdateGroupWorkerRoleReq, WorkerShutdownOp, WorkersQueryReq, WorkersShutdownByFilterReq,
WorkersShutdownByUuidsReq,
},
};
use super::parse_key_val_colon;
#[derive(Serialize, Debug, Deserialize, Args, derive_more::From, Clone)]
pub struct WorkersArgs {
#[command(subcommand)]
pub command: WorkersCommands,
}
#[derive(Subcommand, Serialize, Debug, Deserialize, derive_more::From, Clone)]
pub enum WorkersCommands {
Cancel(CancelWorkerArgs),
CancelMany(CancelWorkersArgs),
CancelList(CancelWorkersByUuidsArgs),
UpdateTags(WorkerUpdateTagsArgs),
UpdateLabels(WorkerUpdateLabelsArgs),
UpdateRoles(UpdateWorkerGroupArgs),
RemoveRoles(RemoveWorkerGroupArgs),
Get(GetWorkerArgs),
Query(WorkersQueryArgs),
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct CancelWorkerArgs {
pub uuid: Uuid,
#[arg(short, long)]
pub force: bool,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct CancelWorkersArgs {
#[arg(short, long)]
pub group: Option<String>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub role: Vec<GroupWorkerRole>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub tags: Vec<String>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub labels: Vec<String>,
#[arg(long)]
pub creator: Option<String>,
#[arg(short, long)]
pub force: bool,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct CancelWorkersByUuidsArgs {
#[arg(num_args = 1.., value_delimiter = ',')]
pub uuids: Vec<Uuid>,
#[arg(short, long)]
pub force: bool,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct WorkerUpdateTagsArgs {
pub uuid: Uuid,
#[arg(num_args = 0..)]
pub tags: Vec<String>,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct WorkerUpdateLabelsArgs {
pub uuid: Uuid,
#[arg(num_args = 0..)]
pub labels: Vec<String>,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct UpdateWorkerGroupArgs {
pub uuid: Uuid,
#[arg(num_args = 0.., value_parser = parse_key_val_colon::<String, GroupWorkerRole>)]
pub roles: Vec<(String, GroupWorkerRole)>,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct RemoveWorkerGroupArgs {
pub uuid: Uuid,
#[arg(num_args = 0..)]
pub groups: Vec<String>,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct GetWorkerArgs {
pub uuid: Uuid,
}
#[derive(Serialize, Debug, Deserialize, Args, Clone)]
pub struct WorkersQueryArgs {
#[arg(short, long)]
pub group: Option<String>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub role: Vec<GroupWorkerRole>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub tags: Vec<String>,
#[arg(short, long, num_args = 0.., value_delimiter = ',')]
pub labels: Vec<String>,
#[arg(long)]
pub creator: Option<String>,
#[arg(short, long)]
pub verbose: bool,
#[arg(long)]
pub count: bool,
}
impl From<WorkersQueryArgs> for WorkersQueryReq {
fn from(args: WorkersQueryArgs) -> Self {
Self {
group_name: args.group,
role: if args.role.is_empty() {
None
} else {
Some(args.role.into_iter().collect())
},
tags: if args.tags.is_empty() {
None
} else {
Some(args.tags.into_iter().collect())
},
labels: if args.labels.is_empty() {
None
} else {
Some(args.labels.into_iter().collect())
},
creator_username: args.creator,
count: args.count,
}
}
}
impl From<UpdateWorkerGroupArgs> for UpdateGroupWorkerRoleReq {
fn from(args: UpdateWorkerGroupArgs) -> Self {
Self {
relations: args.roles.into_iter().collect(),
}
}
}
impl From<WorkerUpdateTagsArgs> for ReplaceWorkerTagsReq {
fn from(args: WorkerUpdateTagsArgs) -> Self {
Self {
tags: args.tags.into_iter().collect(),
}
}
}
impl From<WorkerUpdateLabelsArgs> for ReplaceWorkerLabelsReq {
fn from(args: WorkerUpdateLabelsArgs) -> Self {
Self {
labels: args.labels.into_iter().collect(),
}
}
}
impl From<RemoveWorkerGroupArgs> for RemoveGroupWorkerRoleReq {
fn from(args: RemoveWorkerGroupArgs) -> Self {
Self {
groups: args.groups.into_iter().collect(),
}
}
}
impl From<CancelWorkersArgs> for WorkersShutdownByFilterReq {
fn from(args: CancelWorkersArgs) -> Self {
Self {
group_name: args.group,
role: if args.role.is_empty() {
None
} else {
Some(args.role.into_iter().collect())
},
tags: if args.tags.is_empty() {
None
} else {
Some(args.tags.into_iter().collect())
},
labels: if args.labels.is_empty() {
None
} else {
Some(args.labels.into_iter().collect())
},
creator_username: args.creator,
op: if args.force {
WorkerShutdownOp::Force
} else {
WorkerShutdownOp::Graceful
},
}
}
}
impl From<CancelWorkersByUuidsArgs> for WorkersShutdownByUuidsReq {
fn from(args: CancelWorkersByUuidsArgs) -> Self {
Self {
uuids: args.uuids,
op: if args.force {
WorkerShutdownOp::Force
} else {
WorkerShutdownOp::Graceful
},
}
}
}