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