Add --force/-f flag to init, for creation in non-empty dir (#1065)
This commit is contained in:
		
							parent
							
								
									ade442a487
								
							
						
					
					
						commit
						530f918955
					
				@ -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.
 | 
					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:
 | 
					You can initialize a git repository and a Zola site directly from within a new folder:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/cli.rs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/cli.rs
									
									
									
									
									
								
							@ -24,11 +24,15 @@ pub fn build_cli() -> App<'static, 'static> {
 | 
				
			|||||||
        .subcommands(vec![
 | 
					        .subcommands(vec![
 | 
				
			||||||
            SubCommand::with_name("init")
 | 
					            SubCommand::with_name("init")
 | 
				
			||||||
                .about("Create a new Zola project")
 | 
					                .about("Create a new Zola project")
 | 
				
			||||||
                .arg(
 | 
					                .args(&[
 | 
				
			||||||
                    Arg::with_name("name")
 | 
					                    Arg::with_name("name")
 | 
				
			||||||
                        .default_value(".")
 | 
					                        .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")
 | 
					            SubCommand::with_name("build")
 | 
				
			||||||
                .about("Deletes the output directory if there is one and builds the site")
 | 
					                .about("Deletes the output directory if there is one and builds the site")
 | 
				
			||||||
                .args(&[
 | 
					                .args(&[
 | 
				
			||||||
 | 
				
			|||||||
@ -56,10 +56,11 @@ pub fn is_directory_quasi_empty(path: &Path) -> Result<bool> {
 | 
				
			|||||||
    Ok(false)
 | 
					    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);
 | 
					    let path = Path::new(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Better error message than the rust default
 | 
					    // 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 == "." {
 | 
					        if name == "." {
 | 
				
			||||||
            bail!("The current directory is not an empty folder (hidden files are ignored).");
 | 
					            bail!("The current directory is not an empty folder (hidden files are ignored).");
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -23,7 +23,8 @@ fn main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    match matches.subcommand() {
 | 
					    match matches.subcommand() {
 | 
				
			||||||
        ("init", Some(matches)) => {
 | 
					        ("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(()) => (),
 | 
					                Ok(()) => (),
 | 
				
			||||||
                Err(e) => {
 | 
					                Err(e) => {
 | 
				
			||||||
                    console::unravel_errors("Failed to create the project", &e);
 | 
					                    console::unravel_errors("Failed to create the project", &e);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user