Skip to content

Commit

Permalink
hil: add option for reusing existing rts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKaravaev committed Oct 17, 2024
1 parent f13df5b commit 19d559c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion hil/src/commands/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ pub struct Flash {
#[arg(long)]
slow: bool,
/// If this flag is given, overwites any existing files when downloading the rts.
#[arg(long)]
#[arg(long, conflicts_with = "reuse_existing")]
overwrite_existing: bool,
/// If this flag is given, reuses rts if already exists on path.
#[arg(long, conflicts_with = "overwite_existing")]
reuse_existing: bool,
}

impl Flash {
pub async fn run(self) -> Result<()> {
let args = self;
let existing_file_behavior = if args.overwrite_existing {
ExistingFileBehavior::Overwrite
} else if args.reuse_existing {
ExistingFileBehavior::Reuse
} else {
ExistingFileBehavior::Abort
};
Expand Down
18 changes: 16 additions & 2 deletions hil/src/download_s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum ExistingFileBehavior {
Overwrite,
/// If a file exists, abort
Abort,
/// If a file exists, reuse it
Reuse,
}

struct ContentRange(RangeInclusive<u64>);
Expand All @@ -54,9 +56,21 @@ pub async fn download_url(
out_path: &Utf8Path,
existing_file_behavior: ExistingFileBehavior,
) -> Result<()> {
if existing_file_behavior == ExistingFileBehavior::Abort {
ensure!(!out_path.exists(), "{out_path:?} already exists!");
let path_exists = out_path.exists();

match existing_file_behavior {
ExistingFileBehavior::Abort => {
ensure!(!path_exists, "{out_path:?} already exists!");
}
ExistingFileBehavior::Reuse => {
if path_exists {
info!("{out_path:?} already exists, reusing it");
return Ok(());
}
}
ExistingFileBehavior::Overwrite => {}
}

let parent_dir = out_path
.parent()
.expect("please provide the path to a file")
Expand Down

0 comments on commit 19d559c

Please sign in to comment.