Skip to content

Commit

Permalink
feat: Check if project directory exists before creating it
Browse files Browse the repository at this point in the history
BREAKING CHANGE: We now abort the process if it turns out the directory
already exists
  • Loading branch information
Jan Schulte committed May 28, 2021
1 parent 72efeb3 commit d7b3b88
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extern crate clap;
use std::process;

use clap::{Arg, App};

extern crate serde;
Expand All @@ -11,6 +13,11 @@ mod inbox_file;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");

fn fatal(message: &str) {
println!("{}", message);
process::exit(1);
}

fn main() {
let matches = App::new("foam-up")
.version(VERSION)
Expand All @@ -28,7 +35,15 @@ fn main() {
let path = matches.value_of("path").expect("Expected a Path");
println!("Generating project at {}", path);
let project = project::Project::new(path);
project.create_directories().expect("FATAL: Could not create project directory");
project.create_files().expect("FATAL: Could not create project files");
println!("[x] Project created.");

match project.is_dirty() {
Some(project::Error::DirectoryExists) => {
fatal("Cannot create project -- Directory already exists");
}
None => {
project.create_directories().expect("FATAL: Could not create project directory");
project.create_files().expect("FATAL: Could not create project files");
println!("[x] Project created.");
}
}
}
12 changes: 12 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use crate::inbox_file::InboxFile;
use crate::todo_file::TodoFile;
use crate::vscode_templates;

pub enum Error {
DirectoryExists
}

pub struct Project {
path: String
}
Expand Down Expand Up @@ -42,4 +46,12 @@ impl Project {
blog_post_template.write_file(Path::new(&self.path))?;
Ok(())
}

pub fn is_dirty(&self) -> Option<Error> {
let local_dir = Path::new(&self.path);
if local_dir.exists() {
return Some(Error::DirectoryExists);
}
return None;
}
}

0 comments on commit d7b3b88

Please sign in to comment.