zola/src/cmd/new.rs

42 lines
950 B
Rust
Raw Normal View History

2016-12-06 05:51:33 +00:00
use std::io::prelude::*;
use std::fs::{create_dir, File};
use std::path::Path;
use errors::{Result, ErrorKind};
const CONFIG: &'static str = r#"
title = "My site"
base_url = "https://replace-this-with-your-url.com"
"#;
pub fn create_new_project<P: AsRef<Path>>(name: P) -> Result<()> {
let path = name.as_ref();
// Better error message than the rust default
if path.exists() && path.is_dir() {
2016-12-09 11:24:05 +00:00
bail!("Folder `{}` already exists", path.to_string_lossy().to_string());
2016-12-06 05:51:33 +00:00
}
// main folder
create_dir(path)?;
create_file(path.join("config.toml"), CONFIG.trim_left())?;
// content folder
create_dir(path.join("content"))?;
// layouts folder
create_dir(path.join("layouts"))?;
create_dir(path.join("static"))?;
Ok(())
}
fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
let mut file = File::create(&path)?;
file.write_all(content.as_bytes())?;
Ok(())
}