1 use std::fs::{create_dir_all, remove_dir_all, rename};
2 use std::io::{Result, Error};
9 use crate::configuration::Configuration;
14 pub fn new() -> Self {
18 // moves posts to their next
19 fn shift(&self, configuration: &Configuration) -> Result<()> {
20 for i in (0..configuration.max_posts).rev() {
21 let source = configuration.posts_directory.join(i.to_string());
22 let target = configuration.posts_directory.join((i + 1).to_string());
24 println!("Moving {} source to {}", source.display(), target.display());
27 match rename(&source, &target) {
29 Err(e) => return Err(Error::new(e.kind(), format!("Could not shift post {} to {}", source.display(), target.display())))
37 impl super::Command for Add {
38 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
39 vec![Box::new(SyncDown::new())]
42 fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
43 match create_dir_all(&configuration.posts_directory) {
45 match self.shift(configuration) {
47 let first_directory = configuration.posts_directory.join("0");
48 let _ = remove_dir_all(&first_directory);
49 match create_dir_all(&configuration.posts_directory) {
51 Err(e) => Err(Error::new(e.kind(), format!("Could not create first post directory")))
57 Err(e) => Err(Error::new(e.kind(), format!("Could not create posts directory")))
61 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
63 Box::new(Update::new()),
64 Box::new(Generate::new()),
65 Box::new(SyncUp::new())
69 fn command(&self) -> &'static str {
73 fn help(&self) -> &'static str {
74 "<path_to_post>\t\t\tCreates new blog post"