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

Find and create a `tmp` directory in the source repo

parent 60d53e09
No related branches found
No related tags found
1 merge request!1Initial implementation
^target/
^tmp/
use dialoguer::console::style;
use dialoguer::Confirm;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt)]
......@@ -7,5 +8,9 @@
NewDataset { directory: String },
}
struct Megafine {
tmp_dir: PathBuf,
}
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
......@@ -10,10 +15,21 @@
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn new_dataset(directory: String) -> Result<()> {
println!(
"Creating a dataset from directory {}",
style(&directory).bold()
);
if !std::fs::metadata(&directory)?.is_dir() {
Err("Not a directory")?
impl Megafine {
fn new_dataset(&self, directory: String) -> Result<()> {
println!(
"Creating a dataset from directory {}",
style(&directory).bold()
);
if !std::fs::metadata(&directory)?.is_dir() {
Err("Not a directory")?
}
let proceed = Confirm::new()
.with_prompt("Continue?")
.default(false)
.interact()?;
if !proceed {
Err("")?
}
// TODO
Ok(())
}
......@@ -19,8 +35,27 @@
}
let proceed = Confirm::new()
.with_prompt("Continue?")
.default(false)
.interact()?;
if !proceed {
Err("")?
fn new() -> Result<Self> {
let exe = std::fs::canonicalize(std::env::current_exe()?)?;
// TODO: use a `try` block when available
// https://github.com/rust-lang/rust/issues/31436
let find_repo = || {
let parent_1 = exe.parent()?;
let parent_2 = parent_1.parent()?;
let parent_3 = parent_2.parent()?;
let name_1 = parent_1.file_name()?;
let name_2 = parent_2.file_name()?;
(name_2 == "target" && (name_1 == "debug" || name_1 == "release")).then(|| parent_3)
};
let repo = find_repo().ok_or_else(|| {
format!(
"Couldn’t find megafine repository, executable is '{}' \
instead of the expected target/{{debug,target}}/megafine",
exe.display()
)
})?;
let tmp_dir = repo.join("tmp");
std::fs::create_dir_all(&tmp_dir)?;
Ok(Self { tmp_dir })
}
......@@ -26,6 +61,11 @@
}
// TODO
Ok(())
fn main() -> Result<()> {
let megafine = Megafine::new()?;
match Args::from_args() {
Args::NewDataset { directory } => megafine.new_dataset(directory),
}
}
}
fn main() {
......@@ -29,10 +69,7 @@
}
fn main() {
let result = match Args::from_args() {
Args::NewDataset { directory } => new_dataset(directory),
};
match result {
match Megafine::main() {
Ok(()) => {}
Err(error) => {
let message = error.to_string();
......
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