Skip to content

Commit 8049388

Browse files
committed
ref: used formating for download command
1 parent 7f40fd0 commit 8049388

File tree

2 files changed

+38
-152
lines changed

2 files changed

+38
-152
lines changed

cli/src/command/download.rs

Lines changed: 36 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use reqwest::header::{ACCEPT, USER_AGENT};
44
use serde::Deserialize;
55
use std::fs;
66
use std::fs::File;
7+
use std::path::Path;
78
use zip::ZipArchive;
89
use crate::formatter::{error_without_trace, info, success};
910

@@ -28,7 +29,6 @@ pub async fn handle_download(tag: Option<String>, features: Option<Vec<String>>)
2829
info("Starting download process...".to_string());
2930
let bytes = match download_definitions_as_bytes(tag).await {
3031
Some(bytes) => {
31-
success(format!("Successfully downloaded {} bytes", bytes.len()));
3232
bytes
3333
}
3434
None => {
@@ -38,7 +38,6 @@ pub async fn handle_download(tag: Option<String>, features: Option<Vec<String>>)
3838
};
3939

4040
// Extract the zip file
41-
info("Extracting definitions...".to_string());
4241
convert_bytes_to_folder(bytes, zip_path).await;
4342

4443
// Handle feature filtering if specified
@@ -49,7 +48,8 @@ pub async fn handle_download(tag: Option<String>, features: Option<Vec<String>>)
4948
info("Extracted all features!".to_string());
5049
}
5150

52-
success(format!("Download was successful. Definitions are now available in {out_folder_path}."));
51+
let path = Path::new(out_folder_path);
52+
success(format!("Download was successful. Definitions are now available: {}.", path.display()));
5353
}
5454

5555
async fn download_definitions_as_bytes(tag: Option<String>) -> Option<bytes::Bytes> {
@@ -118,105 +118,55 @@ async fn download_definitions_as_bytes(tag: Option<String>) -> Option<bytes::Byt
118118
if response.status().is_success() {
119119
match response.bytes().await {
120120
Ok(bytes) => {
121-
println!(
122-
"{} {}",
123-
"✅".green(),
124-
"Download completed successfully".green()
125-
);
121+
info("Download completed successfully".to_string());
126122
Some(bytes)
127123
}
128124
Err(e) => {
129-
println!(
130-
"{} {}",
131-
"❌".red(),
132-
format!("Failed to read download data: {e}").red()
133-
);
125+
error_without_trace(format!("Failed to read download data: {e}"));
134126
None
135127
}
136128
}
137129
} else {
138-
println!(
139-
"{} {}",
140-
"❌".red(),
141-
format!("Download failed with status: {}", response.status()).red()
142-
);
130+
error_without_trace(format!("Download failed with status: {}", response.status()));
143131
None
144132
}
145133
}
146134
Err(e) => {
147-
println!(
148-
"{} {}",
149-
"❌".red(),
150-
format!("Download request failed: {e}").red()
151-
);
152-
None
135+
panic!("Download request failed: {e}");
153136
}
154137
}
155138
}
156139

157140
async fn convert_bytes_to_folder(bytes: Bytes, zip_path: &str) {
158-
println!("{} Writing zip file to disk...", "💾".bright_blue());
159141

160142
if let Err(e) = fs::write(zip_path, &bytes) {
161-
println!(
162-
"{} {}",
163-
"❌".red(),
164-
format!("Failed to write zip file: {e}").red()
165-
);
166-
return;
143+
panic!("Failed to write zip file: {e}")
167144
}
168-
println!(
169-
"{} {}",
170-
"✅".green(),
171-
"Zip file written successfully".green()
172-
);
173145

174-
println!("{} Opening zip archive...", "📂".bright_blue());
175146
let zip_file = match File::open(zip_path) {
176147
Ok(file) => file,
177148
Err(e) => {
178-
println!(
179-
"{} {}",
180-
"❌".red(),
181-
format!("Failed to open zip file: {e}").red()
182-
);
183-
return;
149+
panic!("Failed to open zip file: {e}");
184150
}
185151
};
186152

187153
let mut archive = match ZipArchive::new(zip_file) {
188154
Ok(archive) => {
189-
println!(
190-
"{} {}",
191-
"✅".green(),
192-
format!("Successfully opened archive with {} files", archive.len()).green()
193-
);
194155
archive
195156
}
196157
Err(e) => {
197-
println!(
198-
"{} {}",
199-
"❌".red(),
200-
format!("Failed to read zip archive: {e}").red()
201-
);
202-
return;
158+
panic!("Failed to read zip archive: {e}");
203159
}
204160
};
205161

206-
println!("{} Extracting files...", "📤".bright_blue());
207-
let mut extracted_count = 0;
162+
info("Extracting files...".to_string());
208163
let total_files = archive.len();
209164

210165
for i in 0..archive.len() {
211166
let mut file = match archive.by_index(i) {
212167
Ok(file) => file,
213168
Err(e) => {
214-
println!(
215-
"{} {}",
216-
"⚠️".yellow(),
217-
format!("Warning: Failed to read file at index {i}: {e}").yellow()
218-
);
219-
continue;
169+
panic!("Failed to read file at index {i}: {e}");
220170
}
221171
};
222172

@@ -226,81 +176,49 @@ async fn convert_bytes_to_folder(bytes: Bytes, zip_path: &str) {
226176
};
227177

228178
if file.name().ends_with('/') {
229-
if let Err(e) = std::fs::create_dir_all(&out_path) {
230-
println!(
231-
"{} {}",
232-
"⚠️".yellow(),
233-
format!(
234-
"Warning: Failed to create directory {}: {}",
235-
out_path.display(),
236-
e
237-
)
238-
.yellow()
179+
if let Err(e) = fs::create_dir_all(&out_path) {
180+
panic!(
181+
"Failed to create directory {}: {}",
182+
out_path.display(),
183+
e
239184
);
240185
}
241186
} else {
242187
if let Some(p) = out_path.parent() {
243188
if !p.exists() {
244-
if let Err(e) = std::fs::create_dir_all(p) {
245-
println!(
246-
"{} {}",
247-
"⚠️".yellow(),
248-
format!(
249-
"Warning: Failed to create parent directory {}: {}",
250-
p.display(),
251-
e
252-
)
253-
.yellow()
189+
if let Err(e) = fs::create_dir_all(p) {
190+
panic!(
191+
"Warning: Failed to create parent directory {}: {}",
192+
p.display(),
193+
e
254194
);
255-
continue;
256195
}
257196
}
258197
}
259198

260199
match File::create(&out_path) {
261200
Ok(mut outfile) => {
262201
if let Err(e) = std::io::copy(&mut file, &mut outfile) {
263-
println!(
264-
"{} {}",
265-
"⚠️".yellow(),
266-
format!("Warning: Failed to extract {}: {}", out_path.display(), e)
267-
.yellow()
268-
);
269-
} else {
270-
extracted_count += 1;
202+
panic!("Warning: Failed to extract {}: {}", out_path.display(), e);
271203
}
272204
}
273205
Err(e) => {
274-
println!(
275-
"{} {}",
276-
"⚠️".yellow(),
277-
format!(
278-
"Warning: Failed to create file {}: {}",
279-
out_path.display(),
280-
e
281-
)
282-
.yellow()
206+
panic!(
207+
"Failed to create file {}: {}",
208+
out_path.display(),
209+
e
283210
);
284211
}
285212
}
286213
}
287214
}
288215

289-
println!(
290-
"{} {}",
291-
"✅".green(),
292-
format!("Successfully extracted {extracted_count}/{total_files} files").green()
293-
);
216+
info(format!("Successfully extracted {total_files} files"));
217+
info("Cleaning up temporary files...".to_string());
294218

295-
// Clean up zip file
296-
println!("{} Cleaning up temporary files...", "🧹".bright_blue());
297219
match fs::remove_file(zip_path) {
298-
Ok(_) => println!("{} {}", "✅".green(), "Temporary zip file removed".green()),
299-
Err(e) => println!(
300-
"{} {}",
301-
"⚠️".yellow(),
302-
format!("Warning: Failed to remove temporary zip file: {e}").yellow()
303-
),
220+
Ok(_) => info("Temporary zip file removed".to_string()),
221+
Err(e) => error_without_trace(format!("Warning: Failed to remove temporary zip file: {e}"))
304222
}
305223
}
306224

@@ -309,62 +227,30 @@ async fn filter_features(selected_features: Vec<String>) {
309227

310228
match fs::read_dir(definitions_path) {
311229
Ok(entries) => {
312-
let mut removed_count = 0;
313-
let mut kept_count = 0;
314230

315231
for entry in entries {
316232
let directory = match entry {
317233
Ok(directory) => directory,
318234
Err(e) => {
319-
println!(
320-
"{} {}",
321-
"⚠️".yellow(),
322-
format!("Warning: Failed to read directory entry: {e}").yellow()
323-
);
324-
continue;
235+
panic!("{}", format!("Warning: Failed to read directory entry: {e}"));
325236
}
326237
};
327238

328239
let name = directory.file_name().to_str().unwrap_or("").to_string();
329240

330241
if !selected_features.contains(&name) {
331-
println!(" {} Removing feature: {}", "🗑️".red(), name.red());
332242
match fs::remove_dir_all(directory.path()) {
333-
Ok(_) => {
334-
println!(" {} Successfully removed", "✅".green());
335-
removed_count += 1;
336-
}
243+
Ok(_) => {}
337244
Err(e) => {
338-
println!(
339-
" {} Failed to remove: {}",
340-
"❌".red(),
341-
e.to_string().red()
342-
);
245+
error_without_trace(format!("Warning: Failed to remove directory: {e}"))
343246
}
344247
}
345-
} else {
346-
println!(" {} Keeping feature: {}", "📁".green(), name.green());
347-
kept_count += 1;
348248
}
349249
}
350-
351-
println!("\n{} Feature filtering completed:", "📊".bright_blue());
352-
println!(
353-
" {} Features kept: {}",
354-
"✅".green(),
355-
kept_count.to_string().green().bold()
356-
);
357-
println!(
358-
" {} Features removed: {}",
359-
"🗑️".red(),
360-
removed_count.to_string().red().bold()
361-
);
362250
}
363251
Err(e) => {
364-
println!(
365-
"{} {}",
366-
"❌".red(),
367-
format!("Failed to read definitions directory: {e}").red()
252+
error_without_trace(
253+
format!("Failed to read definitions directory: {e}")
368254
);
369255
}
370256
}

cli/src/formatter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub fn error(string: String, path: &String) -> String {
3838
format!("\n{}: {} {}", "error".red(), string, print_path(path))
3939
}
4040

41-
pub fn error_without_trace(string: String) -> String {
42-
format!("\n{}: {}", "error".red(), string)
41+
pub fn error_without_trace(string: String) {
42+
println!("\n{}: {}", "error".red(), string)
4343
}
4444

4545
pub fn error_highlight(highlight: String, string: String) {

0 commit comments

Comments
 (0)