Skip to content

Commit

Permalink
fix(commands): only create attachments dir when needed (#290)
Browse files Browse the repository at this point in the history
* fix(commands): only create attachments dir when needed

Co-authored-by: Tom Milligan <[email protected]>

---------

Co-authored-by: Tom Milligan <[email protected]>
  • Loading branch information
joe-prosser and tommilligan authored Jul 25, 2024
1 parent b90a6b6 commit 5c3a3bb
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions cli/src/commands/get/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,36 @@ struct OutputLocations {
attachments_dir: Option<PathBuf>,
}

fn get_output_locations(path: &Option<PathBuf>) -> Result<OutputLocations> {
fn get_output_locations(path: &Option<PathBuf>, attachments: bool) -> Result<OutputLocations> {
if let Some(path) = path {
let jsonl_file = Some(
File::create(path)
.with_context(|| format!("Could not open file for writing `{}`", path.display()))
.map(BufWriter::new)?,
);

let attachments_dir = path
.parent()
.context("Could not get attachments directory")?
.join(format!(
"{0}.attachments",
path.file_name()
.context("Could not get output file name")?
.to_string_lossy()
));

if !attachments_dir.exists() {
create_dir(&attachments_dir)?;
}
let attachments_dir = if attachments {
let attachments_dir = path
.parent()
.context("Could not get attachments directory")?
.join(format!(
"{0}.attachments",
path.file_name()
.context("Could not get output file name")?
.to_string_lossy()
));

if !attachments_dir.exists() {
create_dir(&attachments_dir)?;
}
Some(attachments_dir)
} else {
None
};

Ok(OutputLocations {
jsonl_file,
attachments_dir: Some(attachments_dir),
attachments_dir,
})
} else {
Ok(OutputLocations::default())
Expand Down Expand Up @@ -360,7 +365,7 @@ pub fn get_many(client: &Client, args: &GetManyCommentsArgs) -> Result<()> {
let OutputLocations {
jsonl_file,
attachments_dir,
} = get_output_locations(path)?;
} = get_output_locations(path, include_attachment_content.unwrap_or_default())?;

let mut label_attribute_filter: Option<AttributeFilter> = None;
if let (Some(dataset_id), Some(filter)) = (dataset, label_filter) {
Expand Down

0 comments on commit 5c3a3bb

Please sign in to comment.