From 530f9189559e1305c8cfc18844d7ceb08bd84f32 Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Thu, 18 Jun 2020 22:15:46 +0300 Subject: [PATCH] Add --force/-f flag to init, for creation in non-empty dir (#1065) --- .../content/documentation/getting-started/cli-usage.md | 2 ++ src/cli.rs | 10 +++++++--- src/cmd/init.rs | 5 +++-- src/main.rs | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index 284eb575..3120f993 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -20,6 +20,8 @@ $ zola init If the `my_site` directory already exists, Zola will only populate it if it contains only hidden files (dotfiles are ignored). If no `my_site` argument is passed, Zola will try to populate the current directory. +In case you want to attempt to populate a non-empty directory and are brave, you can use `zola init --force`. Note that this will _not_ overwrite existing folders or files; in those cases you will get a `File exists (os error 17)` error or similar. + You can initialize a git repository and a Zola site directly from within a new folder: ```bash diff --git a/src/cli.rs b/src/cli.rs index 79d758ce..147a32d5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -24,11 +24,15 @@ pub fn build_cli() -> App<'static, 'static> { .subcommands(vec![ SubCommand::with_name("init") .about("Create a new Zola project") - .arg( + .args(&[ Arg::with_name("name") .default_value(".") - .help("Name of the project. Will create a new directory with that name in the current directory") - ), + .help("Name of the project. Will create a new directory with that name in the current directory"), + Arg::with_name("force") + .short("f") + .takes_value(false) + .help("Force creation of project even if directory is non-empty") + ]), SubCommand::with_name("build") .about("Deletes the output directory if there is one and builds the site") .args(&[ diff --git a/src/cmd/init.rs b/src/cmd/init.rs index ad22bb46..942f6839 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -56,10 +56,11 @@ pub fn is_directory_quasi_empty(path: &Path) -> Result { Ok(false) } -pub fn create_new_project(name: &str) -> Result<()> { +pub fn create_new_project(name: &str, force: bool) -> Result<()> { let path = Path::new(name); + // Better error message than the rust default - if path.exists() && !is_directory_quasi_empty(&path)? { + if path.exists() && !is_directory_quasi_empty(&path)? && !force { if name == "." { bail!("The current directory is not an empty folder (hidden files are ignored)."); } else { diff --git a/src/main.rs b/src/main.rs index 28c192f3..8676db6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,8 @@ fn main() { match matches.subcommand() { ("init", Some(matches)) => { - match cmd::create_new_project(matches.value_of("name").unwrap()) { + let force = matches.is_present("force"); + match cmd::create_new_project(matches.value_of("name").unwrap(), force) { Ok(()) => (), Err(e) => { console::unravel_errors("Failed to create the project", &e);