|
| 1 | +use std::fs::File; |
| 2 | +use std::io::{BufRead, BufReader}; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use anyhow::{Context, Result}; |
| 6 | + |
| 7 | +/// Searches for the BOOTC_IMAGE key in a given os-release file. |
| 8 | +/// Follows standard os-release(5) quoting rules. |
| 9 | +fn parse_bootc_image_from_reader<R: BufRead>(reader: R) -> Result<Option<String>> { |
| 10 | + let mut last_found = None; |
| 11 | + |
| 12 | + for line in reader.lines() { |
| 13 | + let line = line?; |
| 14 | + let line = line.trim(); |
| 15 | + |
| 16 | + if line.is_empty() || line.starts_with('#') { |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + if let Some((key, value)) = line.split_once('=') { |
| 21 | + if key.trim() == "BOOTC_IMAGE" { |
| 22 | + let value = value.trim(); |
| 23 | + |
| 24 | + if value.starts_with('"') && value.ends_with('"') && value.len() >= 2 { |
| 25 | + let unquoted = &value[1..value.len() - 1]; |
| 26 | + let processed = unquoted |
| 27 | + .replace(r#"\""#, "\"") |
| 28 | + .replace(r#"\\"#, "\\") |
| 29 | + .replace(r#"\$"#, "$") |
| 30 | + .replace(r#"\`"#, "`"); |
| 31 | + last_found = Some(processed); |
| 32 | + } else if value.starts_with('\'') && value.ends_with('\'') && value.len() >= 2 { |
| 33 | + last_found = Some(value[1..value.len() - 1].to_string()); |
| 34 | + } else { |
| 35 | + last_found = Some(value.to_string()); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + Ok(last_found) |
| 42 | +} |
| 43 | + |
| 44 | +/// Reads the provided os-release file and returns the BOOTC_IMAGE value if found. |
| 45 | +pub(crate) fn get_bootc_image_from_file<P: AsRef<Path>>(path: P) -> Result<Option<String>> { |
| 46 | + let file = File::open(path.as_ref()).with_context(|| format!("Opening {:?}", path.as_ref()))?; |
| 47 | + let reader = BufReader::new(file); |
| 48 | + parse_bootc_image_from_reader(reader) |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use super::*; |
| 54 | + use indoc::indoc; |
| 55 | + use std::io::Cursor; |
| 56 | + |
| 57 | + fn parse_str(content: &str) -> Option<String> { |
| 58 | + let reader = Cursor::new(content); |
| 59 | + parse_bootc_image_from_reader(reader).unwrap() |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_parse_os_release_standard() { |
| 64 | + let content = indoc! { " |
| 65 | + NAME=Fedora |
| 66 | + BOOTC_IMAGE=quay.io/example/image:latest |
| 67 | + VERSION=39 |
| 68 | + " }; |
| 69 | + assert_eq!(parse_str(content).unwrap(), "quay.io/example/image:latest"); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_parse_os_release_double_quotes() { |
| 74 | + let content = "BOOTC_IMAGE=\"quay.io/example/image:latest\""; |
| 75 | + assert_eq!(parse_str(content).unwrap(), "quay.io/example/image:latest"); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_parse_os_release_single_quotes() { |
| 80 | + let content = "BOOTC_IMAGE='quay.io/example/image:latest'"; |
| 81 | + assert_eq!(parse_str(content).unwrap(), "quay.io/example/image:latest"); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_parse_os_release_escaped() { |
| 86 | + let content = indoc! { r#" |
| 87 | + BOOTC_IMAGE="quay.io/img/with\"quote" |
| 88 | + "# }; |
| 89 | + assert_eq!(parse_str(content).unwrap(), "quay.io/img/with\"quote"); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_parse_os_release_missing() { |
| 94 | + let content = indoc! { " |
| 95 | + NAME=Fedora |
| 96 | + VERSION=39 |
| 97 | + " }; |
| 98 | + assert!(parse_str(content).is_none()); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_parse_os_release_comments_and_spaces() { |
| 103 | + let content = indoc! { " |
| 104 | + # comment |
| 105 | + BOOTC_IMAGE= \"quay.io/img\" |
| 106 | + " }; |
| 107 | + assert_eq!(parse_str(content).unwrap(), "quay.io/img"); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_parse_os_release_last_wins() { |
| 112 | + let content = indoc! { " |
| 113 | + BOOTC_IMAGE=quay.io/old/image |
| 114 | + BOOTC_IMAGE=quay.io/new/image |
| 115 | + " }; |
| 116 | + assert_eq!(parse_str(content).unwrap(), "quay.io/new/image"); |
| 117 | + } |
| 118 | +} |
0 commit comments