-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusers.rs
More file actions
296 lines (275 loc) · 9.76 KB
/
users.rs
File metadata and controls
296 lines (275 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::cli::{
show_values, CONFIRM_FLAG, DELETE_SUBCMD, DESCRIPTION_OPT, FORMAT_OPT, GET_SUBCMD,
INVITE_NAME_ARG, LIST_SUBCMD, NAME_ARG, ROLE_ARG, SET_SUBCMD, SHOW_TIMES_FLAG,
};
use crate::database::{Invitations, OpenApiConfig, UserDetails, Users};
use crate::table::Table;
use crate::utils::{
error_message, user_confirm, warn_missing_subcommand, warning_message, DEL_CONFIRM,
};
use clap::ArgMatches;
use color_eyre::eyre::Result;
use indoc::printdoc;
use std::process;
fn print_user(details: &UserDetails) {
printdoc!(
r#"
Name: {}
Type: {}
Role: {}
Email: {}
Organization: {}
Description: {}
Last Used At: {}
ID: {}
User URL: {}
Created At: {}
Modified At: {}
"#,
details.name,
details.account_type,
details.role,
details.email,
details.organization,
details.description,
details.last_used,
details.id,
details.user_url,
details.created_at,
details.modified_at,
);
}
fn proc_users_delete(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
users: &Users,
) -> Result<()> {
let user_name = subcmd_args.value_of(NAME_ARG).unwrap();
let response = users.get_id(rest_cfg, user_name)?;
if let Some(user_id) = response {
let mut confirmed = subcmd_args.is_present(CONFIRM_FLAG);
if !confirmed {
confirmed = user_confirm(format!("Delete user '{user_name}'"), DEL_CONFIRM);
}
if !confirmed {
warning_message(format!("User '{user_name}' not deleted!"));
} else {
users.delete_user(rest_cfg, &user_id)?;
println!("Deleted user '{user_name}'");
}
} else {
warning_message(format!("User '{user_name}' does not exist!"));
}
Ok(())
}
fn proc_users_get(subcmd_args: &ArgMatches, rest_cfg: &OpenApiConfig, users: &Users) -> Result<()> {
let user_name = subcmd_args.value_of(NAME_ARG).unwrap();
let response = users.get_details_by_name(rest_cfg, user_name)?;
if let Some(details) = response {
print_user(&details);
} else {
error_message(format!("The user '{user_name}' could not be found"));
process::exit(23);
}
Ok(())
}
fn proc_users_current(
_subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
users: &Users,
) -> Result<()> {
let details = users.get_current_user(rest_cfg)?;
print_user(&details);
Ok(())
}
fn proc_users_list(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
users: &Users,
) -> Result<()> {
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
let show_times = subcmd_args.is_present(SHOW_TIMES_FLAG);
let show_values = show_values(subcmd_args);
let details = users.get_user_details(rest_cfg)?;
if details.is_empty() {
println!("No users found!");
} else if !show_values {
let list = details
.iter()
.map(|n| n.name.clone())
.collect::<Vec<String>>();
println!("{}", list.join("\n"))
} else {
let mut hdr = vec!["Name", "Type", "Role", "Email", "Description"];
let mut properties = vec!["name", "type", "role", "email", "description"];
if show_times {
hdr.push("Created At");
hdr.push("Modified At");
hdr.push("Last Used At");
properties.push("created-at");
properties.push("modified-at");
properties.push("last-used");
}
let mut table = Table::new("user");
table.set_header(&hdr);
for entry in details {
let row = entry.get_properties(&properties);
table.add_row(row);
}
table.render(fmt)?;
}
Ok(())
}
fn proc_users_set(subcmd_args: &ArgMatches, rest_cfg: &OpenApiConfig, users: &Users) -> Result<()> {
let user_name = subcmd_args.value_of(NAME_ARG).unwrap();
let description = subcmd_args.value_of(DESCRIPTION_OPT);
let role = subcmd_args.value_of(ROLE_ARG);
let response = users.get_id(rest_cfg, user_name)?;
if let Some(user_id) = response {
if description.is_none() && role.is_none() {
warning_message(format!(
"User '{user_name}' not updated: no updated parameters provided"
));
} else {
users.update_user(rest_cfg, &user_id, role, description)?;
println!("Updated user '{user_name}'");
}
} else {
let details = users.create_user(rest_cfg, user_name, role, description)?;
println!(
"Created service account '{}' with api-key:\n{}\n",
user_name, details.api_key
);
}
Ok(())
}
fn proc_invite_delete(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
invitations: Invitations,
) -> Result<()> {
let email = subcmd_args.value_of(INVITE_NAME_ARG).unwrap();
let response = invitations.get_id(rest_cfg, email)?;
if let Some(invite_id) = response {
let mut confirmed = subcmd_args.is_present(CONFIRM_FLAG);
if !confirmed {
confirmed = user_confirm(format!("Delete invitation for '{email}'"), DEL_CONFIRM);
}
if !confirmed {
warning_message(format!("Invitation for '{email}' not deleted!"));
} else {
invitations.delete_invitation(rest_cfg, &invite_id)?;
println!("Deleted invitation for '{email}'");
}
} else {
warning_message(format!("Invitation for '{email}' does not exist!"));
}
Ok(())
}
fn proc_invite_list(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
invitations: Invitations,
) -> Result<()> {
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
let show_values = show_values(subcmd_args);
let details = invitations.get_invitation_details(rest_cfg)?;
if details.is_empty() {
println!("No invitations found!");
} else if !show_values {
let list = details
.iter()
.map(|n| n.email.clone())
.collect::<Vec<String>>();
println!("{}", list.join("\n"))
} else {
let hdr = vec!["Email", "Role", "Inviter", "State"];
let properties = vec!["email", "role", "inviter-name", "state"];
let mut table = Table::new("invitation");
table.set_header(&hdr);
for entry in details {
let row = entry.get_properties(&properties);
table.add_row(row);
}
table.render(fmt)?;
}
Ok(())
}
fn proc_invite_resend(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
invitations: Invitations,
) -> Result<()> {
let email = subcmd_args.value_of(INVITE_NAME_ARG).unwrap();
let invite_id = invitations.get_id(rest_cfg, email)?;
if let Some(invite_id) = invite_id {
invitations.resend_invitation(rest_cfg, &invite_id)?;
println!("Resent invitation for '{email}'");
} else {
error_message(format!("Pending invitation for '{email}' not found!"));
process::exit(29);
}
Ok(())
}
fn proc_invite_set(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
invitations: Invitations,
) -> Result<()> {
let email = subcmd_args.value_of(INVITE_NAME_ARG).unwrap();
let role = subcmd_args.value_of(ROLE_ARG);
let response = invitations.get_id(rest_cfg, email)?;
if let Some(invite_id) = response {
if role.is_none() {
warning_message(format!(
"Invitation for '{email}' not updated: no updated parameters provided"
));
} else {
invitations.update_invitation(rest_cfg, &invite_id, role)?;
println!("Updated invitation for '{email}'");
}
} else {
let details = invitations.create_invitation(rest_cfg, email, role.unwrap_or("viewer"))?;
println!("Sent '{}' invitation as '{}'", details.email, details.role);
}
Ok(())
}
fn proc_users_invite_command(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
_users: &Users,
) -> Result<()> {
let invitations = Invitations::new();
if let Some(subcmd_args) = subcmd_args.subcommand_matches(DELETE_SUBCMD) {
proc_invite_delete(subcmd_args, rest_cfg, invitations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(LIST_SUBCMD) {
proc_invite_list(subcmd_args, rest_cfg, invitations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches("resend") {
proc_invite_resend(subcmd_args, rest_cfg, invitations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(SET_SUBCMD) {
proc_invite_set(subcmd_args, rest_cfg, invitations)?;
} else {
warn_missing_subcommand("users invitations");
}
Ok(())
}
/// Process the 'users' sub-command
pub fn process_users_command(subcmd_args: &ArgMatches, rest_cfg: &OpenApiConfig) -> Result<()> {
let users = Users::new();
if let Some(subcmd_args) = subcmd_args.subcommand_matches(DELETE_SUBCMD) {
proc_users_delete(subcmd_args, rest_cfg, &users)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(GET_SUBCMD) {
proc_users_get(subcmd_args, rest_cfg, &users)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(LIST_SUBCMD) {
proc_users_list(subcmd_args, rest_cfg, &users)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(SET_SUBCMD) {
proc_users_set(subcmd_args, rest_cfg, &users)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches("current") {
proc_users_current(subcmd_args, rest_cfg, &users)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches("invitations") {
proc_users_invite_command(subcmd_args, rest_cfg, &users)?;
} else {
warn_missing_subcommand("users");
}
Ok(())
}