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