coman/cli/manager_ops.rs
1use crate::{cli::manager::ManagerCommands, helper, Method};
2use colored::Colorize;
3
4impl ManagerCommands {
5 pub fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
6 let manager = ManagerCommands::get_manager();
7
8 match self {
9 // List collections and endpoints
10 Self::List {
11 col,
12 endpoint,
13 quiet,
14 verbose,
15 } => {
16 let collections = manager.load_collections()?;
17 if collections.is_empty() {
18 return Err("No collections found.".into());
19 } else {
20 for collection in collections {
21 if !col.is_empty() && &collection.name != col {
22 continue;
23 }
24 println!(
25 "[{}] - {}",
26 collection.name.bright_magenta(),
27 collection.url
28 );
29 if *quiet {
30 continue;
31 }
32 if !collection.headers.is_empty() {
33 println!(" Headers:");
34 for (key, value) in &collection.headers {
35 println!(" {}: {}", key.bright_cyan(), value.bright_cyan());
36 }
37 }
38 if let Some(requests) = collection.requests {
39 for request in requests {
40 if !endpoint.is_empty() && &request.name != endpoint {
41 continue;
42 }
43 println!(
44 " [{}] {} - {} - {} - {}",
45 request.name.bright_yellow(),
46 request.method.to_string().bright_green(),
47 request.endpoint.bright_white(),
48 request.headers.len(),
49 request.body.as_ref().map_or(0, |b| b.len())
50 );
51 if *verbose {
52 // check if headers present
53 if !request.headers.is_empty() {
54 println!(" Headers:");
55 for (key, value) in &request.headers {
56 println!(
57 " {}: {}",
58 key.bright_cyan(),
59 value.bright_cyan()
60 );
61 }
62 }
63 // check if body present
64 if request.body.is_some() {
65 println!(" Body:");
66 if let Some(body) = &request.body {
67 println!(" {}", body.bright_cyan());
68 };
69 }
70 }
71 }
72 }
73 }
74 }
75 }
76
77 // Delete a collection or endpoint
78 Self::Delete {
79 collection,
80 endpoint,
81 yes,
82 } => {
83 if endpoint.is_empty() {
84 // Deleting a collection
85 println!("Deleting collection '{}'", collection);
86 let confirm = if !yes {
87 helper::confirm("Are you sure you want to delete this collection?")
88 } else {
89 true
90 };
91 if confirm {
92 manager.delete_collection(collection)?;
93 println!("Collection deleted successfully!");
94 } else {
95 return Err("Deletion cancelled.".into());
96 }
97 } else {
98 // Deleting an endpoint
99 println!("Deleting endpoint '{}'", endpoint);
100 let confirm = if !yes {
101 helper::confirm("Are you sure you want to delete this endpoint?")
102 } else {
103 true
104 };
105 if confirm {
106 manager.delete_endpoint(collection, endpoint)?;
107 println!("Endpoint deleted successfully!");
108 } else {
109 return Err("Deletion cancelled.".into());
110 }
111 }
112 }
113
114 // Copy a collection or endpoint
115 Self::Copy {
116 collection,
117 endpoint,
118 to_col,
119 new_name,
120 } => {
121 if endpoint.is_empty() {
122 // Copy collection
123 manager.copy_collection(collection, new_name)?;
124 } else if *to_col {
125 // Copy endpoint to another collection
126 manager.copy_endpoint(collection, endpoint, new_name, Some(new_name))?;
127 } else {
128 // Copy endpoint with new name in same collection
129 manager.copy_endpoint(collection, endpoint, new_name, None)?;
130 }
131 println!("Copy command successful!");
132 }
133
134 // Update a collection or endpoint headers and body
135 Self::Update {
136 collection,
137 endpoint,
138 url,
139 headers,
140 body,
141 } => {
142 if endpoint.is_empty() {
143 // Update collection
144 let url_opt = if url.is_empty() {
145 None
146 } else {
147 Some(url.as_str())
148 };
149 let headers_opt = if headers.is_empty() {
150 None
151 } else {
152 Some(headers.clone())
153 };
154 manager.update_collection(collection, url_opt, headers_opt)?;
155 } else {
156 // Update endpoint
157 let url_opt = if url.is_empty() {
158 None
159 } else {
160 Some(url.as_str())
161 };
162 let headers_opt = if headers.is_empty() {
163 None
164 } else {
165 Some(headers.clone())
166 };
167 let body_opt = if body.is_empty() {
168 Some(String::new()) // Empty body clears the existing body
169 } else {
170 Some(body.clone())
171 };
172 manager.update_endpoint(
173 collection,
174 endpoint,
175 url_opt,
176 headers_opt,
177 body_opt,
178 )?;
179 }
180 println!("Collection updated successfully!");
181 }
182
183 // Add a new collection or update an existing one
184 Self::Col { name, url, headers } => {
185 let exists = manager.get_collection(name).is_ok();
186 manager.add_collection(name, url, headers.clone())?;
187 if exists {
188 eprintln!("Collection with name '{}' already exists.", name);
189 println!("Collection updated successfully!");
190 } else {
191 println!("Collection added successfully!");
192 }
193 }
194
195 // Add a new endpoint to a collection or update an existing one
196 Self::Endpoint {
197 collection,
198 name,
199 path,
200 method,
201 headers,
202 body,
203 } => {
204 let method: Method = method
205 .to_uppercase()
206 .parse()
207 .map_err(|_| format!("Invalid HTTP method: {}", method))?;
208
209 let body_opt = if body.trim().is_empty() {
210 None
211 } else {
212 Some(body.clone())
213 };
214
215 manager.add_endpoint(collection, name, path, method, headers.clone(), body_opt)?;
216 println!("Endpoint added successfully!");
217 }
218 }
219
220 Ok(())
221 }
222}