1use clap::{Args, Subcommand};
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5use crate::{
6 entity::role::GroupWorkerRole,
7 schema::{
8 RemoveGroupWorkerRoleReq, ReplaceWorkerLabelsReq, ReplaceWorkerTagsReq,
9 UpdateGroupWorkerRoleReq, WorkersQueryReq,
10 },
11};
12
13use super::parse_key_val_colon;
14
15#[derive(Serialize, Debug, Deserialize, Args, derive_more::From, Clone)]
16pub struct WorkersArgs {
17 #[command(subcommand)]
18 pub command: WorkersCommands,
19}
20
21#[derive(Subcommand, Serialize, Debug, Deserialize, derive_more::From, Clone)]
22pub enum WorkersCommands {
23 Cancel(CancelWorkerArgs),
25 UpdateTags(WorkerUpdateTagsArgs),
27 UpdateLabels(WorkerUpdateLabelsArgs),
29 UpdateRoles(UpdateWorkerGroupArgs),
31 RemoveRoles(RemoveWorkerGroupArgs),
33 Get(GetWorkerArgs),
35 Query(WorkersQueryArgs),
37}
38
39#[derive(Serialize, Debug, Deserialize, Args, Clone)]
40pub struct CancelWorkerArgs {
41 pub uuid: Uuid,
43 #[arg(short, long)]
46 pub force: bool,
47}
48
49#[derive(Serialize, Debug, Deserialize, Args, Clone)]
50pub struct WorkerUpdateTagsArgs {
51 pub uuid: Uuid,
53 #[arg(num_args = 0..)]
55 pub tags: Vec<String>,
56}
57
58#[derive(Serialize, Debug, Deserialize, Args, Clone)]
59pub struct WorkerUpdateLabelsArgs {
60 pub uuid: Uuid,
62 #[arg(num_args = 0..)]
64 pub labels: Vec<String>,
65}
66
67#[derive(Serialize, Debug, Deserialize, Args, Clone)]
68pub struct UpdateWorkerGroupArgs {
69 pub uuid: Uuid,
71 #[arg(num_args = 0.., value_parser = parse_key_val_colon::<String, GroupWorkerRole>)]
73 pub roles: Vec<(String, GroupWorkerRole)>,
74}
75
76#[derive(Serialize, Debug, Deserialize, Args, Clone)]
77pub struct RemoveWorkerGroupArgs {
78 pub uuid: Uuid,
80 #[arg(num_args = 0..)]
82 pub groups: Vec<String>,
83}
84
85#[derive(Serialize, Debug, Deserialize, Args, Clone)]
86pub struct GetWorkerArgs {
87 pub uuid: Uuid,
89}
90
91#[derive(Serialize, Debug, Deserialize, Args, Clone)]
92pub struct WorkersQueryArgs {
93 #[arg(short, long)]
95 pub group: Option<String>,
96 #[arg(short, long, num_args = 0.., value_delimiter = ',')]
98 pub role: Vec<GroupWorkerRole>,
99 #[arg(short, long, num_args = 0.., value_delimiter = ',')]
101 pub tags: Vec<String>,
102 #[arg(short, long, num_args = 0.., value_delimiter = ',')]
104 pub labels: Vec<String>,
105 #[arg(long)]
107 pub creator: Option<String>,
108 #[arg(short, long)]
110 pub verbose: bool,
111 #[arg(long)]
113 pub count: bool,
114}
115
116impl From<WorkersQueryArgs> for WorkersQueryReq {
117 fn from(args: WorkersQueryArgs) -> Self {
118 Self {
119 group_name: args.group,
120 role: if args.role.is_empty() {
121 None
122 } else {
123 Some(args.role.into_iter().collect())
124 },
125 tags: if args.tags.is_empty() {
126 None
127 } else {
128 Some(args.tags.into_iter().collect())
129 },
130 labels: if args.labels.is_empty() {
131 None
132 } else {
133 Some(args.labels.into_iter().collect())
134 },
135 creator_username: args.creator,
136 count: args.count,
137 }
138 }
139}
140
141impl From<UpdateWorkerGroupArgs> for UpdateGroupWorkerRoleReq {
142 fn from(args: UpdateWorkerGroupArgs) -> Self {
143 Self {
144 relations: args.roles.into_iter().collect(),
145 }
146 }
147}
148
149impl From<WorkerUpdateTagsArgs> for ReplaceWorkerTagsReq {
150 fn from(args: WorkerUpdateTagsArgs) -> Self {
151 Self {
152 tags: args.tags.into_iter().collect(),
153 }
154 }
155}
156
157impl From<WorkerUpdateLabelsArgs> for ReplaceWorkerLabelsReq {
158 fn from(args: WorkerUpdateLabelsArgs) -> Self {
159 Self {
160 labels: args.labels.into_iter().collect(),
161 }
162 }
163}
164
165impl From<RemoveWorkerGroupArgs> for RemoveGroupWorkerRoleReq {
166 fn from(args: RemoveWorkerGroupArgs) -> Self {
167 Self {
168 groups: args.groups.into_iter().collect(),
169 }
170 }
171}