Add --force/-f flag to init, for creation in non-empty dir (#1065)

This commit is contained in:
Per Lundberg 2020-06-18 22:15:46 +03:00 committed by GitHub
parent ade442a487
commit 530f918955
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 6 deletions

View file

@ -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

View file

@ -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(&[

View file

@ -56,10 +56,11 @@ pub fn is_directory_quasi_empty(path: &Path) -> Result<bool> {
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 {

View file

@ -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);