Skip to content
Snippets Groups Projects
Commit 88251c94 authored by Simon Sapin's avatar Simon Sapin
Browse files

Add --no-confirm CLI option

parent 7c47a31e
No related branches found
No related tags found
1 merge request!1Initial implementation
......@@ -4,8 +4,7 @@
use sha2::{Digest, Sha256};
use std::io::Seek;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt)]
......@@ -8,9 +7,20 @@
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt)]
enum Args {
NewDataset { directory: PathBuf },
enum Command {
NewDataset(NewDataset),
}
#[derive(StructOpt)]
struct NewDataset {
/// Skip confirmation prompts
#[structopt(long)]
no_confirm: bool,
/// The directory to archive.
/// The dataset name is taken from the last component of this path.
directory: PathBuf,
}
struct Megafine {
......@@ -19,7 +29,7 @@
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
impl Megafine {
fn new_dataset(&self, directory: &Path) -> Result<()> {
impl NewDataset {
fn run(&self, megafine: &Megafine) -> Result<()> {
println!(
"Creating a dataset from directory {}",
......@@ -24,4 +34,4 @@
println!(
"Creating a dataset from directory {}",
style(directory.display()).bold()
style(self.directory.display()).bold()
);
......@@ -27,5 +37,5 @@
);
if !std::fs::metadata(&directory)?.is_dir() {
if !std::fs::metadata(&self.directory)?.is_dir() {
Err("Not a directory")?
}
......@@ -29,7 +39,8 @@
Err("Not a directory")?
}
let base_name = directory
let base_name = self
.directory
.file_name()
.ok_or("No base name")?
.to_str()
......@@ -39,7 +50,7 @@
.arg("id")
.arg("--id")
.arg("-R")
.arg(&directory)
.arg(&self.directory)
.output()?;
let id = if hg_id_output.status.success() {
format!("_{}", std::str::from_utf8(&hg_id_output.stdout)?.trim())
......@@ -52,9 +63,9 @@
let archive_name = format!("{}_{}{}.tar.zst", base_name, today, id);
let bold = style(&archive_name).bold();
let archive_path = self.tmp_dir.join(&archive_name);
let archive_path = megafine.tmp_dir.join(&archive_name);
if archive_path.exists() {
Err(format!("tmp/{} already exists", bold))?
}
println!("About to create tmp/{}", bold);
......@@ -56,13 +67,15 @@
if archive_path.exists() {
Err(format!("tmp/{} already exists", bold))?
}
println!("About to create tmp/{}", bold);
let proceed = Confirm::new()
.with_prompt("Continue?")
.default(false)
.interact()?;
if !proceed {
Err("")?
if !self.no_confirm {
let proceed = Confirm::new()
.with_prompt("Continue?")
.default(false)
.interact()?;
if !proceed {
Err("")?
}
}
......@@ -67,6 +80,6 @@
}
let tmp_file = tempfile::NamedTempFile::new_in(&self.tmp_dir)?;
let tmp_file = tempfile::NamedTempFile::new_in(&megafine.tmp_dir)?;
let sha = Sha256::new();
let tee = Tee(tmp_file, sha);
let level = zstd::DEFAULT_COMPRESSION_LEVEL;
......@@ -74,7 +87,7 @@
zstd.multithread(num_cpus::get() as _)?;
let mut tar = tar::Builder::new(zstd);
tar.follow_symlinks(false);
tar.append_dir_all(base_name, directory)?;
tar.append_dir_all(base_name, &self.directory)?;
// Peel the onion
let zstd = tar.into_inner()?;
let Tee(mut tmp_file, sha) = zstd.finish()?;
......@@ -88,4 +101,5 @@
Ok(())
}
}
......@@ -91,4 +105,5 @@
impl Megafine {
fn new() -> Result<Self> {
let exe = std::fs::canonicalize(std::env::current_exe()?)?;
......@@ -117,8 +132,8 @@
fn main() -> Result<()> {
let megafine = Megafine::new()?;
match Args::from_args() {
Args::NewDataset { directory } => megafine.new_dataset(&directory),
match Command::from_args() {
Command::NewDataset(c) => c.run(&megafine),
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment