]> git.r.bdr.sh - rbdr/blog/blob - src/command/add.rs
dbcc512c5f343199345785eaaf1255f503c0f37b
[rbdr/blog] / src / command / add.rs
1 use std::fs::{create_dir_all, rename};
2 use std::io::Result;
3 use super::{
4 generate::Generate,
5 sync_down::SyncDown,
6 sync_up::SyncUp,
7 update::Update
8 };
9 use crate::configuration::Configuration;
10
11 pub struct Add;
12
13 impl Add {
14 pub fn new() -> Self {
15 Add
16 }
17 }
18
19 impl super::Command for Add {
20 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
21 vec![Box::new(SyncDown::new())]
22 }
23
24 fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
25 create_dir_all(&configuration.posts_directory)?;
26 for i in (0..configuration.max_posts).rev() {
27 let source = configuration.posts_directory.join(i.to_string());
28 let target = configuration.posts_directory.join((i + 1).to_string());
29
30 if source.exists() {
31 rename(&source, &target)?;
32 }
33 }
34 Ok(())
35 }
36
37 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
38 vec![
39 Box::new(Update::new()),
40 Box::new(Generate::new()),
41 Box::new(SyncUp::new())
42 ]
43 }
44
45 fn command(&self) -> &'static str {
46 "add"
47 }
48
49 fn help(&self) -> &'static str {
50 "<path_to_post>\t\t\tCreates new blog post"
51 }
52 }