Skip to content

Commit d3e8d37

Browse files
committed
add functions that handle error and info output
1 parent ea5b573 commit d3e8d37

File tree

2 files changed

+69
-96
lines changed

2 files changed

+69
-96
lines changed

src/core.rs

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -513,18 +513,8 @@ pub fn retrieve_asset(
513513
} else if url.scheme() == "file" {
514514
// Check if parent_url is also a file: URL (if not, then we don't embed the asset)
515515
if parent_url.scheme() != "file" {
516-
if !options.silent {
517-
eprintln!(
518-
"{}{} (Security Error){}",
519-
if options.no_color { "" } else { ANSI_COLOR_RED },
520-
&url,
521-
if options.no_color {
522-
""
523-
} else {
524-
ANSI_COLOR_RESET
525-
},
526-
);
527-
}
516+
print_error_message(&format!("{} (security error)", &url), options);
517+
528518
// Provoke error
529519
client.get("").send()?;
530520
}
@@ -533,27 +523,14 @@ pub fn retrieve_asset(
533523
let path: &Path = path_buf.as_path();
534524
if path.exists() {
535525
if path.is_dir() {
536-
if !options.silent {
537-
eprintln!(
538-
"{}{} (is a directory){}",
539-
if options.no_color { "" } else { ANSI_COLOR_RED },
540-
&url,
541-
if options.no_color {
542-
""
543-
} else {
544-
ANSI_COLOR_RESET
545-
},
546-
);
547-
}
526+
print_error_message(&format!("{} (is a directory)", &url), options);
548527

549528
// Provoke error
550529
Err(client.get("").send().unwrap_err())
551530
} else {
552-
if !options.silent {
553-
eprintln!("{}", &url);
554-
}
531+
print_info_message(&format!("{}", &url), options);
555532

556-
let file_blob: Vec<u8> = fs::read(path).expect("Unable to read file");
533+
let file_blob: Vec<u8> = fs::read(path).expect("unable to read file");
557534

558535
Ok((
559536
file_blob.clone(),
@@ -563,18 +540,7 @@ pub fn retrieve_asset(
563540
))
564541
}
565542
} else {
566-
if !options.silent {
567-
eprintln!(
568-
"{}{} (not found){}",
569-
if options.no_color { "" } else { ANSI_COLOR_RED },
570-
&url,
571-
if options.no_color {
572-
""
573-
} else {
574-
ANSI_COLOR_RESET
575-
},
576-
);
577-
}
543+
print_error_message(&format!("{} (not found)", &url), options);
578544

579545
// Provoke error
580546
Err(client.get("").send().unwrap_err())
@@ -584,9 +550,7 @@ pub fn retrieve_asset(
584550

585551
if cache.is_some() && cache.as_ref().unwrap().contains_key(&cache_key) {
586552
// URL is in cache, we get and return it
587-
if !options.silent {
588-
eprintln!("{} (from cache)", &url);
589-
}
553+
print_info_message(&format!("{} (from cache)", &url), options);
590554

591555
Ok((
592556
cache.as_ref().unwrap().get(&cache_key).unwrap().0.to_vec(),
@@ -627,31 +591,18 @@ pub fn retrieve_asset(
627591
match client.get(url.as_str()).headers(headers).send() {
628592
Ok(response) => {
629593
if !options.ignore_errors && response.status() != reqwest::StatusCode::OK {
630-
if !options.silent {
631-
eprintln!(
632-
"{}{} ({}){}",
633-
if options.no_color { "" } else { ANSI_COLOR_RED },
634-
&url,
635-
response.status(),
636-
if options.no_color {
637-
""
638-
} else {
639-
ANSI_COLOR_RESET
640-
},
641-
);
642-
}
594+
print_error_message(&format!("{} ({})", &url, response.status()), options);
595+
643596
// Provoke error
644597
return Err(client.get("").send().unwrap_err());
645598
}
646599

647600
let response_url: Url = response.url().clone();
648601

649-
if !options.silent {
650-
if url.as_str() == response_url.as_str() {
651-
eprintln!("{}", &url);
652-
} else {
653-
eprintln!("{} -> {}", &url, &response_url);
654-
}
602+
if url.as_str() == response_url.as_str() {
603+
print_info_message(&format!("{}", &url), options);
604+
} else {
605+
print_info_message(&format!("{} -> {}", &url, &response_url), options);
655606
}
656607

657608
let new_cache_key: String = clean_url(response_url.clone()).to_string();
@@ -672,18 +623,7 @@ pub fn retrieve_asset(
672623
data = b.to_vec();
673624
}
674625
Err(error) => {
675-
if !options.silent {
676-
eprintln!(
677-
"{}{}{}",
678-
if options.no_color { "" } else { ANSI_COLOR_RED },
679-
error,
680-
if options.no_color {
681-
""
682-
} else {
683-
ANSI_COLOR_RESET
684-
},
685-
);
686-
}
626+
print_error_message(&format!("{}", error), options);
687627
}
688628
}
689629

@@ -701,19 +641,7 @@ pub fn retrieve_asset(
701641
Ok((data, response_url, media_type, charset))
702642
}
703643
Err(error) => {
704-
if !options.silent {
705-
eprintln!(
706-
"{}{} ({}){}",
707-
if options.no_color { "" } else { ANSI_COLOR_RED },
708-
&url,
709-
error,
710-
if options.no_color {
711-
""
712-
} else {
713-
ANSI_COLOR_RESET
714-
},
715-
);
716-
}
644+
print_error_message(&format!("{} ({})", &url, error), options);
717645

718646
Err(client.get("").send().unwrap_err())
719647
}
@@ -730,3 +658,38 @@ pub fn read_stdin() -> Vec<u8> {
730658
Err(_) => buffer,
731659
}
732660
}
661+
662+
use std::io::Write;
663+
664+
pub fn print_error_message(text: &str, options: &Options) {
665+
if !options.silent {
666+
let stderr = io::stderr();
667+
let mut handle = stderr.lock();
668+
669+
if handle
670+
.write_all(
671+
format!(
672+
"{}{}{}\n",
673+
if options.no_color { "" } else { ANSI_COLOR_RED },
674+
&text,
675+
if options.no_color {
676+
""
677+
} else {
678+
ANSI_COLOR_RESET
679+
},
680+
)
681+
.as_bytes(),
682+
)
683+
.is_ok()
684+
{}
685+
}
686+
}
687+
688+
pub fn print_info_message(text: &str, options: &Options) {
689+
if !options.silent {
690+
let stderr = io::stderr();
691+
let mut handle = stderr.lock();
692+
693+
if handle.write_all(format!("{}\n", &text).as_bytes()).is_ok() {}
694+
}
695+
}

src/main.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tempfile::Builder;
88

99
use monolith::cache::Cache;
1010
use monolith::cookies::parse_cookie_file_contents;
11-
use monolith::core::{create_monolithic_document, Options};
11+
use monolith::core::{create_monolithic_document, print_error_message, Options};
1212

1313
enum Output {
1414
Stdout(io::Stdout),
@@ -193,18 +193,30 @@ fn main() {
193193

194194
// Read and parse cookie file
195195
if let Some(opt_cookie_file) = cookie_file_path.clone() {
196-
match fs::read_to_string(opt_cookie_file) {
196+
match fs::read_to_string(&opt_cookie_file) {
197197
Ok(str) => match parse_cookie_file_contents(&str) {
198198
Ok(parsed_cookies_from_file) => {
199199
options.cookies = parsed_cookies_from_file;
200200
}
201201
Err(_) => {
202-
eprintln!("Could not parse specified cookie file");
202+
print_error_message(
203+
&format!(
204+
"could not parse specified cookie file \"{}\"",
205+
opt_cookie_file
206+
),
207+
&options,
208+
);
203209
process::exit(1);
204210
}
205211
},
206212
Err(_) => {
207-
eprintln!("Could not read specified cookie file");
213+
print_error_message(
214+
&format!(
215+
"could not read specified cookie file \"{}\"",
216+
opt_cookie_file
217+
),
218+
&options,
219+
);
208220
process::exit(1);
209221
}
210222
}
@@ -213,15 +225,13 @@ fn main() {
213225
match create_monolithic_document(source, &options, &mut Some(cache)) {
214226
Ok(result) => {
215227
// Define output
216-
let mut output = Output::new(&destination).expect("Could not prepare output");
228+
let mut output = Output::new(&destination).expect("could not prepare output");
217229

218230
// Write result into STDOUT or file
219-
output.write(&result).expect("Could not write output");
231+
output.write(&result).expect("could not write output");
220232
}
221233
Err(error) => {
222-
if !options.silent {
223-
eprintln!("Error: {}", error);
224-
}
234+
print_error_message(&format!("Error: {}", error), &options);
225235

226236
process::exit(1);
227237
}

0 commit comments

Comments
 (0)