Skip to content

Commit 371156e

Browse files
authored
Merge pull request #696 from cgwalters/minor-bound-bits
boundimage: Drop duplicate filenames from errors
2 parents ffbce7d + 328cb01 commit 371156e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/src/boundimage.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ fn parse_spec_dir(root: &Dir, spec_dir: &str) -> Result<Vec<BoundImage>> {
6363
let file_ini = tini::Ini::from_string(&file_contents).context("Parse to ini")?;
6464
let file_extension = Utf8Path::new(file_name).extension();
6565
let bound_image = match file_extension {
66-
Some("image") => parse_image_file(file_name, &file_ini),
67-
Some("container") => parse_container_file(file_name, &file_ini),
66+
Some("image") => parse_image_file(&file_ini).with_context(|| format!("Parsing {path}")),
67+
Some("container") => {
68+
parse_container_file(&file_ini).with_context(|| format!("Parsing {path}"))
69+
}
6870
_ => anyhow::bail!("Invalid file extension: {file_name}"),
6971
}?;
7072

@@ -74,11 +76,10 @@ fn parse_spec_dir(root: &Dir, spec_dir: &str) -> Result<Vec<BoundImage>> {
7476
Ok(bound_images)
7577
}
7678

77-
#[context("parse image file {file_name}")]
78-
fn parse_image_file(file_name: &str, file_contents: &tini::Ini) -> Result<BoundImage> {
79+
fn parse_image_file(file_contents: &tini::Ini) -> Result<BoundImage> {
7980
let image: String = file_contents
8081
.get("Image", "Image")
81-
.ok_or_else(|| anyhow::anyhow!("Missing Image field in {file_name}"))?;
82+
.ok_or_else(|| anyhow::anyhow!("Missing Image field"))?;
8283

8384
//TODO: auth_files have some semi-complicated edge cases that we need to handle,
8485
// so for now let's bail out if we see one since the existence of an authfile
@@ -92,11 +93,10 @@ fn parse_image_file(file_name: &str, file_contents: &tini::Ini) -> Result<BoundI
9293
Ok(bound_image)
9394
}
9495

95-
#[context("parse container file {file_name}")]
96-
fn parse_container_file(file_name: &str, file_contents: &tini::Ini) -> Result<BoundImage> {
96+
fn parse_container_file(file_contents: &tini::Ini) -> Result<BoundImage> {
9797
let image: String = file_contents
9898
.get("Container", "Image")
99-
.ok_or_else(|| anyhow::anyhow!("Missing Image field in {file_name}"))?;
99+
.ok_or_else(|| anyhow::anyhow!("Missing Image field"))?;
100100

101101
let bound_image = BoundImage::new(image.to_string(), None)?;
102102
Ok(bound_image)
@@ -286,7 +286,7 @@ mod tests {
286286
//should return BoundImage when no auth_file is present
287287
let file_contents =
288288
tini::Ini::from_string("[Image]\nImage=quay.io/foo/foo:latest").unwrap();
289-
let bound_image = parse_image_file("foo.image", &file_contents).unwrap();
289+
let bound_image = parse_image_file(&file_contents).unwrap();
290290
assert_eq!(bound_image.image, "quay.io/foo/foo:latest");
291291
assert_eq!(bound_image.auth_file, None);
292292

@@ -295,11 +295,11 @@ mod tests {
295295
"[Image]\nImage=quay.io/foo/foo:latest\nAuthFile=/etc/containers/auth.json",
296296
)
297297
.unwrap();
298-
assert!(parse_image_file("foo.image", &file_contents).is_err());
298+
assert!(parse_image_file(&file_contents).is_err());
299299

300300
//should return error when missing image field
301301
let file_contents = tini::Ini::from_string("[Image]\n").unwrap();
302-
assert!(parse_image_file("foo.image", &file_contents).is_err());
302+
assert!(parse_image_file(&file_contents).is_err());
303303

304304
Ok(())
305305
}
@@ -309,13 +309,13 @@ mod tests {
309309
//should return BoundImage
310310
let file_contents =
311311
tini::Ini::from_string("[Container]\nImage=quay.io/foo/foo:latest").unwrap();
312-
let bound_image = parse_container_file("foo.container", &file_contents).unwrap();
312+
let bound_image = parse_container_file(&file_contents).unwrap();
313313
assert_eq!(bound_image.image, "quay.io/foo/foo:latest");
314314
assert_eq!(bound_image.auth_file, None);
315315

316316
//should return error when missing image field
317317
let file_contents = tini::Ini::from_string("[Container]\n").unwrap();
318-
assert!(parse_container_file("foo.container", &file_contents).is_err());
318+
assert!(parse_container_file(&file_contents).is_err());
319319

320320
Ok(())
321321
}

0 commit comments

Comments
 (0)