Skip to content

Commit 2bacf68

Browse files
committed
fix: fall back to "space_image" when server filename is absent or generic
Addresses review comment: add explicit check for missing/placeholder filenames ("attachment") and substitute domain-specific default
1 parent 317e8e6 commit 2bacf68

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/cmd/space/image.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@ pub fn image(args: &SpaceImageArgs) -> Result<()> {
2121

2222
pub fn image_with(args: &SpaceImageArgs, api: &dyn BacklogApi) -> Result<()> {
2323
let (bytes, filename) = api.download_space_image()?;
24-
let path = args.output.clone().unwrap_or_else(|| {
25-
let base = std::path::Path::new(&filename)
26-
.file_name()
27-
.unwrap_or(std::ffi::OsStr::new("space_image"));
28-
PathBuf::from(base)
29-
});
24+
let path = args
25+
.output
26+
.clone()
27+
.unwrap_or_else(|| default_output_path(&filename));
3028
std::fs::write(&path, &bytes).with_context(|| format!("Failed to write {}", path.display()))?;
3129
println!("Saved: {} ({} bytes)", path.display(), bytes.len());
3230
Ok(())
3331
}
3432

33+
fn default_output_path(filename: &str) -> PathBuf {
34+
let effective = if filename.is_empty() || filename == "attachment" {
35+
"space_image"
36+
} else {
37+
filename
38+
};
39+
let base = std::path::Path::new(effective)
40+
.file_name()
41+
.unwrap_or(std::ffi::OsStr::new("space_image"));
42+
PathBuf::from(base)
43+
}
44+
3545
#[cfg(test)]
3646
mod tests {
3747
use super::*;
@@ -95,4 +105,25 @@ mod tests {
95105
let err = image_with(&args(None), &api).unwrap_err();
96106
assert!(err.to_string().contains("download failed"));
97107
}
108+
109+
#[test]
110+
fn default_output_path_uses_server_filename() {
111+
assert_eq!(
112+
default_output_path("space_image.png"),
113+
PathBuf::from("space_image.png")
114+
);
115+
}
116+
117+
#[test]
118+
fn default_output_path_falls_back_for_attachment() {
119+
assert_eq!(
120+
default_output_path("attachment"),
121+
PathBuf::from("space_image")
122+
);
123+
}
124+
125+
#[test]
126+
fn default_output_path_falls_back_for_empty() {
127+
assert_eq!(default_output_path(""), PathBuf::from("space_image"));
128+
}
98129
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ enum SpaceCommands {
338338
},
339339
/// Download the space icon image
340340
Image {
341-
/// Output file path (default: server-provided filename)
341+
/// Output file path (default: server-provided filename, or "space_image" if none)
342342
#[arg(long, short = 'o')]
343343
output: Option<std::path::PathBuf>,
344344
},

0 commit comments

Comments
 (0)