(input: Option<&String>, file: Option<&String>)
| 434 | } |
| 435 | |
| 436 | pub fn get_input(input: Option<&String>, file: Option<&String>) -> String { |
| 437 | trace!("Input: {input:?}, File: {file:?}"); |
| 438 | let value = if let Some(input) = input { |
| 439 | debug!("{}", t!("util.readingInput")); |
| 440 | |
| 441 | // see if user accidentally passed in a file path |
| 442 | if Path::new(input).exists() { |
| 443 | error!("{}", t!("util.inputIsFile")); |
| 444 | exit(EXIT_INVALID_INPUT); |
| 445 | } |
| 446 | input.clone() |
| 447 | } else if let Some(path) = file { |
| 448 | debug!("{} {path}", t!("util.readingInputFromFile")); |
| 449 | // check if need to read from STDIN |
| 450 | if path == "-" { |
| 451 | info!("{}", t!("util.readingInputFromStdin")); |
| 452 | let mut stdin = Vec::<u8>::new(); |
| 453 | match std::io::stdin().read_to_end(&mut stdin) { |
| 454 | Ok(_) => { |
| 455 | match String::from_utf8(stdin) { |
| 456 | Ok(input) => { |
| 457 | input |
| 458 | }, |
| 459 | Err(err) => { |
| 460 | error!("{}: {err}", t!("util.invalidUtf8")); |
| 461 | exit(EXIT_INVALID_INPUT); |
| 462 | } |
| 463 | } |
| 464 | }, |
| 465 | Err(err) => { |
| 466 | error!("{}: {err}", t!("util.failedToReadStdin")); |
| 467 | exit(EXIT_INVALID_INPUT); |
| 468 | } |
| 469 | } |
| 470 | } else { |
| 471 | // see if an extension should handle this file |
| 472 | let mut discovery = Discovery::new(); |
| 473 | let path_buf = Path::new(path); |
| 474 | for extension in discovery.get_extensions(&Capability::Import) { |
| 475 | if let Ok(content) = extension.import(path_buf) { |
| 476 | return content; |
| 477 | } |
| 478 | } |
| 479 | match std::fs::read_to_string(path) { |
| 480 | Ok(input) => { |
| 481 | // check if it contains UTF-8 BOM and remove it |
| 482 | if input.as_bytes().starts_with(&[0xEF, 0xBB, 0xBF]) { |
| 483 | info!("{}", t!("util.removingUtf8Bom")); |
| 484 | input[3..].to_string() |
| 485 | } else { |
| 486 | input |
| 487 | } |
| 488 | }, |
| 489 | Err(err) => { |
| 490 | error!("{}: {err}", t!("util.failedToReadFile")); |
| 491 | exit(EXIT_INVALID_INPUT); |
| 492 | } |
| 493 | } |
no test coverage detected