]> git.r.bdr.sh - rbdr/blog/blob - src/command/add.rs
Do CI
[rbdr/blog] / src / command / add.rs
1 use std::fs::{create_dir_all, remove_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 - 1).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 target.exists() {
31 remove_dir_all(&target)?;
32 }
33 if source.exists() {
34 rename(&source, &target)?;
35 }
36 }
37 Ok(())
38 }
39
40 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
41 vec![
42 Box::new(Update::new()),
43 Box::new(Generate::new()),
44 Box::new(SyncUp::new())
45 ]
46 }
47
48 fn command(&self) -> &'static str {
49 "add"
50 }
51
52 fn help(&self) -> &'static str {
53 "<path_to_post>\t\t\tCreates new blog post"
54 }
55 }