# HG changeset patch
# User Simon Sapin <simon.sapin@octobus.net>
# Date 1636111597 -3600
#      Fri Nov 05 12:26:37 2021 +0100
# Node ID be651cc500077b7c013e14358438f22d0e9465bf
# Parent  88251c94998351efaf63d3781b7cc4745e8e386c
Add --overwrite CLI option

diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,6 +18,10 @@
     #[structopt(long)]
     no_confirm: bool,
 
+    /// Overwrite a dataset archive with the same name if it exists
+    #[structopt(long)]
+    overwrite: bool,
+
     /// The directory to archive.
     /// The dataset name is taken from the last component of this path.
     directory: PathBuf,
@@ -32,7 +36,7 @@
 impl NewDataset {
     fn run(&self, megafine: &Megafine) -> Result<()> {
         println!(
-            "Creating a dataset from directory {}",
+            "New dataset from directory {}",
             style(self.directory.display()).bold()
         );
         if !std::fs::metadata(&self.directory)?.is_dir() {
@@ -65,10 +69,15 @@
 
         let archive_path = megafine.tmp_dir.join(&archive_name);
         if archive_path.exists() {
-            Err(format!("tmp/{} already exists", bold))?
+            if self.overwrite {
+                println!("Overwriting tmp/{}", bold)
+            } else {
+                Err(format!("tmp/{} already exists", bold))?
+            }
+        } else {
+            println!("Creating tmp/{}", bold);
         }
 
-        println!("About to create tmp/{}", bold);
         if !self.no_confirm {
             let proceed = Confirm::new()
                 .with_prompt("Continue?")
@@ -93,7 +102,11 @@
         let Tee(mut tmp_file, sha) = zstd.finish()?;
         let size = tmp_file.stream_position()?;
         tmp_file.as_file_mut().sync_all()?;
-        tmp_file.persist_noclobber(&archive_path)?;
+        if self.overwrite {
+            tmp_file.persist(&archive_path)?;
+        } else {
+            tmp_file.persist_noclobber(&archive_path)?;
+        }
         let hash = hex::encode(sha.finalize());
 
         println!("Compressed to {}", style(BinaryBytes(size)).bold());