-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgroups.rs
More file actions
193 lines (181 loc) · 6.25 KB
/
groups.rs
File metadata and controls
193 lines (181 loc) · 6.25 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
use crate::cli::{
show_values, ADD_USER_OPT, CONFIRM_FLAG, DELETE_SUBCMD, DESCRIPTION_OPT, FORMAT_OPT,
GET_SUBCMD, LIST_SUBCMD, NAME_ARG, RENAME_OPT, RM_USER_OPT, SET_SUBCMD, SHOW_TIMES_FLAG,
};
use crate::database::{GroupDetails, Groups, OpenApiConfig, UserError, 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_group(details: &GroupDetails) {
printdoc!(
r#"
Name: {}
Description: {}
ID: {}
Group URL: {}
Created At: {}
Modified At: {}
Users: {}
"#,
details.name,
details.description,
details.id,
details.url,
details.created_at,
details.modified_at,
details.users.join(", ")
);
}
fn proc_groups_get(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
groups: &Groups,
) -> Result<()> {
let group_name = subcmd_args.value_of(NAME_ARG).unwrap();
let response = groups.get_group_details_by_name(rest_cfg, group_name)?;
if let Some(group) = response {
print_group(&group);
} else {
error_message(format!("The group '{group_name}' could not be found"));
process::exit(51);
}
Ok(())
}
fn proc_groups_list(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
groups: &Groups,
) -> Result<()> {
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
let show_values = show_values(subcmd_args);
let show_times = subcmd_args.is_present(SHOW_TIMES_FLAG);
let group_list = groups.get_group_details_list(rest_cfg)?;
if group_list.is_empty() {
println!("No groups found!");
} else if !show_values {
println!(
"{}",
group_list
.iter()
.map(|g| g.name.clone())
.collect::<Vec<String>>()
.join("\n")
);
} else {
let mut hdr = vec!["Name", "Description"];
let mut properties = vec!["name", "description"];
if show_times {
hdr.push("Created At");
hdr.push("Modified At");
properties.push("created-at");
properties.push("modified-at");
}
hdr.push("Users");
properties.push("users");
let mut table = Table::new("groups");
table.set_header(&hdr);
for entry in group_list {
let row = entry.get_properties(&properties);
table.add_row(row);
}
table.render(fmt)?;
}
Ok(())
}
fn proc_groups_delete(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
groups: &Groups,
) -> Result<()> {
let group_name = subcmd_args.value_of(NAME_ARG).unwrap();
let group_id = groups.get_id(rest_cfg, group_name)?;
if let Some(group_id) = group_id {
let mut confirmed = subcmd_args.is_present(CONFIRM_FLAG);
if !confirmed {
confirmed = user_confirm(format!("Delete group '{group_name}'"), DEL_CONFIRM);
}
if !confirmed {
warning_message(format!("Group '{group_name}' not deleted!"));
} else {
groups.delete_group(rest_cfg, &group_id)?;
println!("Deleted group '{group_name}'");
}
} else {
warning_message(format!("Group '{group_name}' does not exist!"));
}
Ok(())
}
fn proc_groups_set(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
groups: &Groups,
) -> Result<()> {
let group_name = subcmd_args.value_of(NAME_ARG).unwrap();
let description = subcmd_args.value_of(DESCRIPTION_OPT);
let rename = subcmd_args.value_of(RENAME_OPT);
let add_user_opt = subcmd_args.values_of(ADD_USER_OPT);
let rm_user_opt = subcmd_args.values_of(RM_USER_OPT);
/* Look for existing group */
let found_group = groups.get_group_by_name(rest_cfg, group_name)?;
/* Update existing group or create new group if not found */
let group = if let Some(group) = found_group {
if description.is_some() || rename.is_some() {
let name = rename.unwrap_or(group_name);
groups.update_group(rest_cfg, &group.id, Some(name), description)?;
println!("Updated group '{name}'");
} else if add_user_opt.is_none() && rm_user_opt.is_none() {
warning_message(format!(
"Group '{group_name}' not updated: no updated parameters provided"
));
return Ok(());
}
group
} else {
let group = groups.create_group(rest_cfg, group_name, description)?;
println!("Created group '{group_name}'");
group
};
/* Convert the provided user names into URLs */
let user_name_to_url = |name| {
let user = Users::new()
.get_details_by_name(rest_cfg, name)?
.ok_or_else(|| UserError::UserNotFound(name.to_string()))?;
Ok(user.user_url)
};
let add_user_urls = add_user_opt
.unwrap_or_default()
.map(user_name_to_url)
.collect::<Result<Vec<String>>>()?;
let rm_user_urls = rm_user_opt
.unwrap_or_default()
.map(user_name_to_url)
.collect::<Result<Vec<String>>>()?;
for user_url in add_user_urls {
groups.add_user_to_group(rest_cfg, &group, &user_url)?;
}
for user_url in rm_user_urls {
groups.remove_user_from_group(rest_cfg, &group, &user_url)?;
}
Ok(())
}
/// Process the 'groups' sub-command
pub fn process_groups_command(subcmd_args: &ArgMatches, rest_cfg: &OpenApiConfig) -> Result<()> {
let groups = Groups::new();
if let Some(subcmd_args) = subcmd_args.subcommand_matches(GET_SUBCMD) {
proc_groups_get(subcmd_args, rest_cfg, &groups)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(LIST_SUBCMD) {
proc_groups_list(subcmd_args, rest_cfg, &groups)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(DELETE_SUBCMD) {
proc_groups_delete(subcmd_args, rest_cfg, &groups)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(SET_SUBCMD) {
proc_groups_set(subcmd_args, rest_cfg, &groups)?;
} else {
warn_missing_subcommand("groups");
}
Ok(())
}